The basic operation of C-language doubly linked list

Source: Internet
Author: User

Header file #pragma oncetypedef int datatype;typedef struct linklist{datatype _data; Struct linklist* _next;struct linklist* _prev;} Linklist,*plinklist;void initlist (Plinklist pnode);p Linklist _buynode (PLinkList& pNode , datatype x); Void printlist (Plinklist phead); Void pushback (Plinklist& phead, DATATYPE&NBSP;X); Void popback (Plinklist& phead); Void pushfront (PLinkList& pHead, DATATYPE&NBSP;X); Void popfront (Plinklist& phead);p Linklist find (pLinkList pHead,  DATATYPE&NBSP;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&nBSP; (pnode->_next == null) {pnode->_prev = null;}} Create node Plinklist _buynode (plinklist& pnode, datatype x) {pnode =  (pLinkList) malloc (sizeof (linklist));p node->_data = x;pnode->_next = null;pnode->_prev =  null;return pnode;} Traverse the output List data field Void printlist (Plinklist phead) {plinklist head = phead;if (pHead ==  null) {printf ("The list is empty!\n"); return;} while  (head) {printf ("%d ", head->_data); head = head->_next;} printf ("\ n");} Tail plug 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;} Tail Delete void popback (plinklist& phead) {Plinklist head = phead;plinklist tmp = null;if  (phead == null) {printf ("The linked list is empty!") \ n "); return;} if  (head->_next == null) {free (head);p Head = null;return;} while  (head->_next) {head = head->_next;} Tmp = head->_prev;tmp->_next = null;free (head);} Head plug Void pushfront (plinklist& phead, datatype x) {Plinklist head = phead ;p linklist tmp = phead;if  (phead == null) {_buynode (pHead, x); return;} Phead=_buynode (head->_prev, x);p head->_next = tmp;} Head 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;} After inserting _ Void insert (plinklist pos, datatype x) {Plinklist tmp = pos->_next ; assert (POS);     _buynode (pos->_next, x);p os->_next->_prev = pos; Pos->_next->_next = tmp;} 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);//PopBack (PHead );//popback (Phead);//popback (Phead);//popback (Phead); Pushfront (phead, 1); Pushfront (PHEAD,&NBSP;2); Pushfront (phead, 3); Pushfront (PHEAD,&NBSP;4); Pushfront (phead, 5); Printlist (Phead);//popfront (Phead);//popfront (Phead);//popfront (Phead);//popfront (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);D estroylist (phead);     printlist (Phead);} Int main () {test1 (); return 0;}



The basic operation of C-language doubly linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.