本文共 4495 字,大约阅读时间需要 14 分钟。
#include#include #include typedef int DataType;
typedef struct Node { DataType data; struct Node *next; }Node;
void SListInit(Node **ppfirst) { assert(ppfirst); *ppfirst = NULL; }
void SListDestory(Node **ppfirst) { assert(ppfirst); Node *cur, *next; for (cur = *ppfirst; cur != NULL; cur = next) { next = cur->next; free(cur); } }
注:销毁链表时一定要把每一个结点都释放
static Node *CreateNewNode(DataType data) { Node *node = (Node *)malloc(sizeof(Node)); node->data = data; node->next = NULL; return node; } void SListPushFront(Node **ppfirst, DataType data) { assert(ppfirst); Node *newNode = CreateNewNode(data); newNode->next = *ppfirst; *ppfirst = newNode; }
void SListPushBack(Node **ppfirst, DataType data) { assert(ppfirst); Node *newNode = CreateNewNode(data); if (*ppfirst == NULL) { *ppfirst = newNode; return; } Node *cur = *ppfirst; while (cur->next != NULL) { cur = cur->next; } cur->next = newNode; }
void SListInsert(Node **ppfirst, Node *pos, DataType data) { assert(ppfirst); Node *newNode = CreateNewNode(data); if (pos == *ppfirst) { SListPushFront(ppfirst, data); return; } Node *cur = *ppfirst; while (cur->next != pos) { cur = cur->next; } newNode->next = pos; cur->next = newNode; }
void SListPopFront(Node **ppfirst) { assert(ppfirst); //断言不是空指针 assert(*ppfirst); //断言不是空链表 Node *first = *ppfirst; *ppfirst = (*ppfirst)->next; free(first); }
void SListPopBack(Node **ppfirst) { assert(ppfirst); assert(*ppfirst); if ((*ppfirst)->next == NULL) { free(*ppfirst); *ppfirst = NULL; return; } Node *cur = *ppfirst; while (cur->next->next != NULL) { cur = cur->next; } free(cur->next); cur->next = NULL; }
void SListRemove(Node **ppfirst, Node *pos) { assert(ppfirst); assert(*ppfirst); if (*ppfirst == pos) { SListPopFront(ppfirst); return; } Node *cur = *ppfirst; while (cur->next != pos) { cur = cur->next; } cur->next = pos->next; free(pos); }
Node *SListFind(Node **ppfirst, DataType data) { assert(ppfirst); Node *cur = *ppfirst; while (cur->data != data || cur->next != NULL) { cur = cur->next; } if (cur->data == data) { return cur; } return NULL; //找不到返回NULL }
void SListRemoveFirst(Node **ppfirst, DataType data) { assert(ppfirst); assert(*ppfirst); Node *cur = *ppfirst; Node *node = *ppfirst; //记录cur的前一个 while (cur->data != data && cur->next != NULL) { cur = cur->next; } if (cur->next == NULL) { printf("不存在\n"); return; } if (cur == *ppfirst) { SListPopFront(ppfirst); return; } while (node->next != cur) { node = node->next; } node->next = cur->next; free(cur); }
void SListRemoveAll(Node **ppfirst, DataType data) { assert(ppfirst); assert(*ppfirst); Node *cur = *ppfirst; Node *node = *ppfirst; while (cur->next != NULL) { node = *ppfirst; if (cur->data == data) { if (cur == *ppfirst) { cur = cur->next; SListPopFront(ppfirst); continue; } while (node->next != cur) { node = node->next; } node->next = cur->next; free(cur); cur = node->next; continue; } cur = cur->next; } if (cur == *ppfirst && cur->data == data) { SListPopFront(ppfirst); } }
注:在链表中,插入元素一定要先申请空间,删除元素一个要记得释放
转载地址:http://gbwdb.baihongyu.com/