Basic operations of two-way linked list in C Language
// Header file # pragma once typedef int DataType; typedef struct LinkList {DataType _ data; struct LinkList * _ next; struct LinkList * _ prev;} LinkList, * pLinkList; void InitList (pLinkList pNode); pLinkList _ BuyNode (pLinkList & pNode, DataType x); void PrintList (pLinkList pHead); void PushBack (pLinkList & pHead, DataType x ); void PopBack (pLinkList & pHead); void PushFront (pLinkList & pHead, DataType x); void PopFront (pLinkLis T & pHead); pLinkList Find (pLinkList pHead, DataType x); void Insert (pLinkList pos, DataType x); void Erase (pLinkList & pHead, pLinkList pos ); void Reverse (pLinkList & pHead); void DestroyList (pLinkList & pHead); // function file # include <stdio. h> # include <assert. h> # include "LinkList. h "# include <malloc. h >/// initialize void InitList (pLinkList pNode) {assert (pNode); if (pNode-> _ next = NULL) {pNode-> _ prev = NULL ;}} // create a node pLinkLis T _ BuyNode (pLinkList & pNode, DataType x) {pNode = (pLinkList) malloc (sizeof (LinkList); pNode-> _ data = x; pNode-> _ next = NULL; pNode-> _ prev = NULL; return pNode;} // traverses the output linked list data field void PrintList (pLinkList pHead) {pLinkList head = pHead; if (pHead = NULL) {printf ("the linked list is empty! \ N "); return;} while (head) {printf (" % d ", head-> _ data); head = head-> _ next ;} printf ("\ n");} // void PushBack (pLinkList & pHead, DataType x) {pLinkList head = pHead; if (head = NULL) {_ BuyNode (pHead, x); return;} while (head-> _ next) {head = head-> _ next ;} head-> _ next = _ BuyNode (head-> _ next, x); head-> _ next-> _ prev = head ;} // Delete void PopBack (pLinkList & pHead) {pLinkList head = pHead; pLinkList tmp = NULL; if (pHead = NULL) {printf ("linked list already empty! \ N "); return;} if (head-> _ next = NULL) {free (head); pHead = NULL; return;} while (head-> _ next) {head = head-> _ next;} tmp = head-> _ prev; tmp-> _ next = NULL; free (head );} // void PushFront (pLinkList & pHead, DataType x) {pLinkList head = pHead; pLinkList tmp = pHead; if (pHead = NULL) {_ BuyNode (pHead, x ); return;} pHead = _ BuyNode (head-> _ prev, x); pHead-> _ next = tmp;} // Delete void PopFront (pLinkList & PHead) {pLinkList tmp = pHead; if (pHead = NULL) {printf ("the linked list is empty! \ N "); return;} pHead = pHead-> _ next; if (pHead) {pHead-> _ prev = NULL;} free (tmp );} // Find pLinkList Find (pLinkList pHead, DataType x) {pLinkList head = pHead; assert (pHead); while (head) {if (head-> _ data = x) return head; head = head-> _ next;} return NULL;} // void Insert (pLinkList pos, DataType x) after _ is inserted in the middle) {pLinkList tmp = pos-> _ next; assert (pos); _ BuyNode (pos-> _ next, x); pos-> _ next-> _ prev = pos; pos-> _ next = tmp;} // Delete void Erase (pLinkList & pHead, pLinkList pos) {pLinkList tmp = pos; assert (pos ); if (pos-> _ next = NULL) {pos-> _ prev-> _ next = NULL; free (tmp); return;} if (pos = pHead) {pHead = pHead-> _ next; pHead-> _ prev = NULL; free (pos); return ;} tmp-> _ prev-> _ next = tmp-> _ next; tmp-> _ next-> _ prev = tmp-> _ prev; free (pos );} // Reverse void Reverse (pLinkList & pHead) {pLinkList head = pHead; pLinkList tmp = pHead; if (pHead = NULL & pHead-> _ next = NULL) return; while (head) {tmp = head-> _ next; head-> _ next = head-> _ prev; head-> _ prev = tmp; if (head-> _ prev = NULL) pHead = head; head = tmp; }}// destroy void DestroyList (pLinkList & pHead) {while (pHead) {PopFront (pHead) ;}// test case main function # include "LinkList. h "# include <stdio. h> void test1 () {pLinkList pHead = NULL; // PushBack (pHead, 1); // PushBack (pHead, 2); // PushBack (pHead, 3 ); // PushBack (pHead, 4); // PushBack (pHead, 5); // PrintList (pHead); // PopBack (pHead ); // PopBack (pHead); PushFront (pHead, 1); PushFront (pHead, 2); PushFront (pHead, 3); PushFront (pHead, 4); PushFront (pHead, 5); PrintList (pHead); // PopFront (pHead ); // PopFront (pHead ); // printf ("% d \ n", Find (pHead, 6); Insert (Find (pHead, 1), 6); Erase (pHead, Find (pHead, 2); PrintList (pHead); Reverse (pHead); PrintList (pHead); DestroyList (pHead); PrintList (pHead);} int main () {test1 (); return 0 ;}