【資料結構】雙向連結串列的實現
阿新 • • 發佈:2018-11-25
文章目錄
LinkList.h
#ifndef __LINKLIST_H__ #define __LINKLIST_H__ #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef int DataType; typedef struct ListNode { struct ListNode* next; struct ListNode* prev; DataType data; }ListNode; typedef struct List { struct ListNode* head; }List; void ListInit(List* lt); void ListPushFront(List* lt, DataType x); void ListPopBack(List* lt); void ListPushBack(List* lt, DataType x); void ListPrint(List* lt); ListNode* BuyListNode(DataType x); void ListPopFront(List* lt); void ListInsert(ListNode* pos, DataType x); void ListErase(ListNode* pos); void ListDestory(List* lt); ListNode* ListFind(List* lt, DataType x); int ListSize(List* lt); #endif
LinkLish.c
#include"LinkList.h" void ListInit(List* lt) { assert(lt); lt->head->data = 0; lt->head->next = lt->head; lt->head->prev = lt->head; } void ListPushBack(List* lt, DataType x) { assert(lt); ListNode* cur = lt->head->next; ListNode* NewNode = BuyListNode(x); while (cur->next != lt->head->next) { cur = cur->next; } cur->next = NewNode; NewNode->next = lt->head; NewNode->prev = cur; lt->head->prev = NewNode; } void ListPopBack(List* lt) { assert(lt); ListNode* del = lt->head->next; ListNode* prev = lt->head; while (del->next != lt->head) { prev = del; del = del->next; } prev->next = lt->head; lt->head = prev; } void ListPushFront(List* lt, DataType x) { assert(lt); ListNode* NewNode = BuyListNode(x); NewNode->next = lt->head->next; NewNode->prev = lt->head; lt->head->next->prev = NewNode; lt->head->next = NewNode; } void ListPopFront(List* lt) { assert(lt); if (lt->head->next == lt->head) { return; } ListNode* del = lt->head->next; lt->head->next = del->next; del->next->prev = lt->head; free(del); } ListNode* BuyListNode(DataType x) { ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));//開闢新節點 if (NULL == newNode) { exit(EXIT_FAILURE); } newNode->data = x; newNode->prev = NULL; newNode->next = NULL; return newNode; } void ListPrint(List* lt) { ListNode* cur = lt->head->next; while (cur != lt->head) { printf("%d->\n", cur->data); cur = cur->next; } } void ListInsert(ListNode* pos, DataType x) { assert(pos); ListNode* newnode = BuyListNode(x); newnode->data = x; newnode->next = pos->next; newnode->prev = pos; pos->next->prev = newnode; pos->next = newnode; } void ListErase(ListNode* pos) { if (pos->next == pos) { return; } ListNode* del = pos->next; pos->data = pos->next->data; pos->next = del->next; del->next->prev = pos; free(del); } void ListDestory(List* lt) { assert(lt); ListNode* cur = lt->head->next; ListNode* del = cur; while (cur->next != lt->head) { del = cur; cur = cur->next; free(del); } free(cur); } int ListSize(List* lt) { ListNode* cur = lt->head; int count = 0; while (cur) { count++; cur = cur->next; } return count; } ListNode* ListFind(List* lt, DataType x) { assert(lt); ListNode* cur = lt->head->next; while (cur != lt->head) { if (cur->data == x) { return cur; } else cur = cur->next; } return NULL; }