赞
踩
#include<stdio.h> #include<stdlib.h> #include<assert.h> #include<stdbool.h> typedef int LTDataType; typedef struct ListNode { struct ListNode* prev; struct ListNode* next; LTDataType data; }LTNode; //开哨兵位的头节点 LTNode*LTInit(); //头插 void LTPushFront(LTNode* phead, LTDataType x); //头删 void LTPopFront(LTNode* phead); //尾插 void LTPushBack(LTNode* phead, LTDataType x); //尾删 void LTPopBack(LTNode* phead); //pos之前位置插入 void LTNodeInsert(LTNode* pos, LTDataType x); //删除pos位置 void LTNodeErase(LTNode* pos); //打印 void LTPrint(LTNode* phead); //销毁 void LTDestory(LTNode* phead); //查找 LTNode* LTFind(LTNode* phead, LTDataType x);
想要快速实现一个链表,那么复用pos位置的插入删除即可
LTNode* BuyLTNode(LTDataType x) { LTNode* newnode = (LTNode*)malloc(sizeof(LTNode)); if (newnode == NULL) { perror("malloc fail"); return; } newnode->data = x; newnode->next = NULL; newnode->prev = NULL; return newnode; } bool LTEmpty(LTNode* phead) { assert(phead); return phead->next == phead; } LTNode* LTInit() { LTNode* phead = BuyLTNode(-1); phead->prev = phead; phead->next = phead; return phead; } //pos之前位置插入 void LTNodeInsert(LTNode* pos, LTDataType x) { assert(pos); LTNode* prev = pos->prev; LTNode* newnode = BuyLTNode(x); newnode->prev = prev; prev->next = newnode; newnode->next = pos; pos->prev = newnode; } //删除pos位置 void LTNodeErase(LTNode* pos) { assert(pos); assert(!LTEmpty(pos)); LTNode* prev = pos->prev; LTNode* next = pos->next; prev->next = next; next->prev = prev; free(pos); } void LTPrint(LTNode* phead) { assert(phead); LTNode* cur = phead->next; printf("sentry-->"); while (cur != phead) { printf("%d-->", cur->data); cur = cur->next; } printf("\n"); } //头插 void LTPushFront(LTNode* phead, LTDataType x) { assert(phead); LTNode* newnode = BuyLTNode(x); LTNode* next = phead->next; phead->next = newnode; newnode->prev = phead; newnode->next = next; next->prev = newnode; //如果复用也可以 //LTNodeInsert(phead->next, x); } //头删 void LTPopFront(LTNode* phead) { assert(phead); assert(!LTEmpty(phead)); LTNode* first = phead->next; LTNode* second = first->next; phead->next = second; second->prev = phead; free(first); //如果复用 //LTNodeErase(phead->next); } //尾插 void LTPushBack(LTNode* phead, LTDataType x) { assert(phead); LTNode* tail = phead->prev; LTNode* newnode = BuyLTNode(x); tail->next = newnode; newnode->prev = tail; newnode->next = phead; phead->prev = newnode; //如果复用 //LTNodeInsert(phead,x); } //尾删 void LTPopBack(LTNode* phead) { assert(phead); assert(!LTEmpty(phead)); LTNode* tail = phead->prev; LTNode* prevtail = tail->prev; prevtail->next = phead; phead->prev = prevtail; free(tail); //如果复用 //LTNodeErase(phead->prev); } LTNode* LTFind(LTNode* phead, LTDataType x) { assert(phead); LTNode* cur = phead->next; while (cur != phead) { if (cur->data == x) { return cur; } else { cur = cur->next; } } return NULL; } void LTDestory(LTNode* phead) { assert(phead); LTNode* cur = phead->next; while (cur != phead) { LTNode* next = cur->next; free(cur); cur = next; } free(cur); //使用者自己将phead置空 }
void Test2() { LTNode* phead = LTInit(); LTPushFront(phead, 1); LTPushFront(phead, 2); LTPushBack(phead, 3); LTPushBack(phead, 4); LTPrint(phead); LTPopBack(phead); LTPrint(phead); LTPopFront(phead); LTPopFront(phead); LTPrint(phead); LTPopBack(phead); LTPrint(phead); LTDestory(phead); phead = NULL; } int main() { Test2(); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。