赞
踩
- ***结构最复杂,一般用在单独存储数据。***
- ***实际中使用的链表数据结构,都是带头双向循环链表。***
- ***结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单。***
LTNode* ListInit() {
//哨兵位的头节点
LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListDestroy(ListNode* &phead) {
assert(phead);
LTNode* cur = phead->next;
while (cur != phead) {
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
phead = NULL;
}
void ListPushBack(LTNode* phead, LDateType x) {
assert(phead);
LTNode* tail = phead->prev;
LTNode* newNode = (LTNode*)malloc(sizeof(LTNode));
newNode->data = x;
tail->next = newNode;
newNode->prev = tail;
newNode->next = phead;
phead->prev = newNode;
}
void ListPopBack(LTNode* phead) {
assert(phead);
if (phead->next!=NULL) {
LTNode* oldNote = phead->prev;
LTNode* newTail = oldNote->prev;
phead->prev = newTail;
newTail->next = phead;
delete(oldNote);
}
//ListErase(phead->prev);
}
void ListPushFront(LTNode* phead, LDateType x) {
assert(phead);
LTNode* newNode = (LTNode*)malloc(sizeof(LTNode));
newNode->data = x;
newNode->next = phead->next;
phead->next->prev = newNode;
phead->next = newNode;
newNode->prev = phead;
}
void ListPopFront(LTNode* phead) {
assert(phead);
assert(phead->next!=phead);
LTNode* oldNote = phead->next;
LTNode* newHead = oldNote->next;
phead->next = newHead;
newHead->prev = phead;
delete(oldNote);
//ListErase(phead->next);
}
void ListInsert(LTNode* pos, LDateType x) {
assert(pos);
LTNode* posPrev = pos->prev;
LTNode* newNode = (LTNode*)malloc(sizeof(LTNode));
newNode->data = x;
newNode->prev = posPrev;
newNode->next = pos;
posPrev->next = newNode;
pos->prev = newNode;
}
//删除pos位置的节点
void ListErase(LTNode* pos) {
assert(pos);
LTNode* posPrev = pos->prev;
LTNode* posNext = pos->next;
free(pos);
posPrev->next = posNext;
posNext->prev = posPrev;
}
#pragma once #include<iostream> #include<assert.h> using namespace std; typedef int LDateType; typedef struct ListNode { LDateType data; struct ListNode* next; struct ListNode* prev; } LTNode; LTNode* ListInit(); void ListDestroy(ListNode* &phead); void ListPrint(LTNode* phead); void ListPushBack(LTNode* phead,LDateType x); void ListPopBack(LTNode* phead); void ListPushFront(LTNode* phead,LDateType x); void ListPopFront(LTNode* phead); LTNode* ListFind(LTNode* phead,LDateType x); void ListInsert(LTNode* pos, LDateType x); void ListErase(LTNode* pos);
#define _CRT_SECURE_NO_WARNINGS #include"DoubleList.h" LTNode* ListInit() { //哨兵位的头节点 LTNode* phead = (LTNode*)malloc(sizeof(LTNode)); phead->next = phead; phead->prev = phead; return phead; } void ListDestroy(ListNode* &phead) { assert(phead); LTNode* cur = phead->next; while (cur != phead) { LTNode* next = cur->next; free(cur); cur = next; } free(phead); phead = NULL; } void ListPushBack(LTNode* phead, LDateType x) { assert(phead); LTNode* tail = phead->prev; LTNode* newNode = (LTNode*)malloc(sizeof(LTNode)); newNode->data = x; tail->next = newNode; newNode->prev = tail; newNode->next = phead; phead->prev = newNode; } void ListPrint(LTNode* phead) { assert(phead); LTNode* cur = phead->next; while (cur != phead) { cout << cur->data; if (cur->next != phead) { cout << "->"; } cur = cur->next; } cout << endl; } void ListPopBack(LTNode* phead) { assert(phead); if (phead->next!=NULL) { LTNode* oldNote = phead->prev; LTNode* newTail = oldNote->prev; phead->prev = newTail; newTail->next = phead; delete(oldNote); } //ListErase(phead->prev); } void ListPushFront(LTNode* phead, LDateType x) { assert(phead); LTNode* newNode = (LTNode*)malloc(sizeof(LTNode)); newNode->data = x; newNode->next = phead->next; phead->next->prev = newNode; phead->next = newNode; newNode->prev = phead; } void ListPopFront(LTNode* phead) { assert(phead); assert(phead->next!=phead); LTNode* oldNote = phead->next; LTNode* newHead = oldNote->next; phead->next = newHead; newHead->prev = phead; delete(oldNote); //ListErase(phead->next); } LTNode* ListFind(LTNode* phead, LDateType x) { if (phead) { ListNode* cur = phead->next; while (cur!=phead) { if (cur->data == x) { return cur; } cur = cur->next; } } else { return NULL; } } void ListInsert(LTNode* pos, LDateType x) { assert(pos); LTNode* posPrev = pos->prev; LTNode* newNode = (LTNode*)malloc(sizeof(LTNode)); newNode->data = x; newNode->prev = posPrev; newNode->next = pos; posPrev->next = newNode; pos->prev = newNode; } //删除pos位置的节点 void ListErase(LTNode* pos) { assert(pos); LTNode* posPrev = pos->prev; LTNode* posNext = pos->next; free(pos); posPrev->next = posNext; posNext->prev = posPrev; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。