(with head and tail pointers) the realization of various functions of cyclic doubly linked list

Source: Internet
Author: User

The following functions are implemented for circular doubly linked lists:


void Meau ();                           menu function void Initlist (List *list);             initialize void Show (List *list);                 Print List contents bool Push_back (list *list,elemtype x); Tail interpolation method bool Push_front (list *list,elemtype x);//head interpolation bool Isempty (list *list);              Determines whether the linked list is an empty bool Pop_back (list *list);             Tail-censored bool Pop_front (List *list);            Head Delete node *find_val (List *list,elemtype x); find bool Delete_val by value (list *list,elemtype x);//delete bool Modify by value (list *list,elemtype x);    Modify void Clear (List *list);                Empty list void Destory (list *list);              Destroy linked list void reverse (list *list);              Reverse linked list node *prio (list *list,elemtype x);     The predecessor Node *next (List *list,elemtype x) that asks for a value;     The subsequent bool Insert_val (list *list,elemtype x) for a value, and//insert void sort (list *list) by value;                 Sort (Ascending)


DCList.h:

#ifndef __dclist_h__#define __dclist_h__#include<assert.h> #include <iostream>using namespace std; typedef int ELEMTYPE;TYPEDEF struct NODE{STRUCT node *pre;struct node *next; Elemtype data;} node;typedef struct List{node *first; Node *last;int size;}                           List;void Meau ();             menu function void Initlist (List *list);                 initialize void Show (List *list); Print List contents bool Push_back (list *list,elemtype x);              Tail interpolation method bool Push_front (list *list,elemtype x);//head interpolation bool Isempty (list *list);             Determines whether the linked list is an empty bool Pop_back (list *list);            Tail-censored bool Pop_front (List *list); Head Delete node *find_val (List *list,elemtype x);    find bool Delete_val by value (list *list,elemtype x);//delete bool Modify by value (list *list,elemtype x);                Modify void Clear (List *list);              Empty list void Destory (list *list);              Destroy linked list void reverse (list *list);     Reverse linked list node *prio (list *list,elemtype x);     The predecessor Node *next (List *list,elemtype x) that asks for a value; The subsequent bool insert_v of a valueAl (list *list,elemtype x);//insert void sort (list *list) by value; Sort (Ascending) #endif
DCList.cpp:

#include "DCList.h"/* Menu function */void Meau () {cout<< "*****************seqlist********************" <<endl; cout<< "---zyh_helen" <<endl;cout<< "*[1]initseqlist [2]push_b                ACK * "<<endl;cout<<" *[3]push_front [4]pop_back * "<<endl;cout<<" *[5]pop_front [6]                   Find_val * "<<endl;cout<<" *[7]show [0]quit_syatem* "<<endl;cout<<" *[8]modify [9] Delete_val * "<<endl; cout<< "*[10]clear [11]destory *" <<endl;      cout<< "*[12]reverse [13]prio *" <<endl;cout<< "*[14]next [15]sort * "<<endl;cout<<" *[16]insert_val * "&LT;&LT;ENDL;} /* Initialize linked list */void initlist (List *list) {Node *s = (node *) malloc (sizeof); assert (s! = NULL); s->next = S->pre = S;lis T->first = List->last = S;list->size = 0;} /* Tail interpolation method*/bool push_back (List *list,elemtype x) {Node *s = (node *) malloc (sizeof); if (s! = NULL) {s->data = X;list->last ->next = S;s->pre = List->last;list->last = S;list->last->next = list->first;list->first-> Pre = List->last;//s->next = List->first;//list->last = S;list->size++;return true;} Elsereturn false;} /* Print List Contents */void Show (List *list) {Node *s = List->first->next;while (s! = List->first) {Cout<<s->data << "--"; s = s->next;} cout<< "NULL" &LT;&LT;ENDL;} /* Header interpolation */bool push_front (List *list,elemtype x) {Node *s = (node *) malloc (sizeof (node)), if (s! = NULL) {S->data = x;/* and later elements (Original first element) connection */s->next = List->first->next;list->first->next->pre = s;/* and head node connection */list->first- >next = S;s->pre = list->first;/* If the first node, the tail pointer should point to it */if (list->size = = 0) {List->last = s;} List->size++;return true;} else{cout<< "Application space failed! "<<endl;return false;}} /* Determine if the linked list is empty */bool Isempty (List *list) {return (LIST->size = = 0);} /* Tail-Erase */bool pop_back (list *list) {if (Isempty (list)) {cout<< "linked list is empty! "<<endl;return false;} Node *s = list->last->pre;//Find the last node's precursor free (list->last);//Release last node/* Connection */s->next = list->first;list- >last = S;list->size--;return true;} BOOL Pop_front (list *list) {if (Isempty (list)) {cout<< "linked list is empty! "<<endl;return false;} Node *s = list->first->next;//Find the first node (to be freed)/* Connection */list->first->next = S->next;s->next->pre = list->first;/* if the node to be deleted is the first one, the tail pointer changes to */if (list->size = = 1) {list->last = List->first;} Free (s); List->size--;return true;} /* Lookup function: Find the specified element: Returns a pointer to it, not found: Returns Null*/node *find_val (List *list,elemtype x) {Node *s = List->first->next;while (s! = List->first) {if (x = = S->data) return s;elses = S->next;} return NULL;} /* Delete nodes by value */bool delete_val (List *list,elemtype x) {Node *s = Find_val (list,x), if (s! = NULL) {/* Connection: Header Delete, tail delete, all applicable */s->pre- >next = S->next;s->next->pre = s->pre;/* release */free (s); list->size--;/* if the last node is released, the tail pointer needs to be changed to point, the rest does not need to change */if (s = = list->last) {list->last = S->pre;} return true;} else{cout<< "The item is not exist!" <<endl;return false;}} /*bool delete_val (List *list,elemtype x) {Node *s = Find_val (list,x); if (s = NULL) {if (s = = List->first->next) {Pop_ Front (list);} else if (s = = list->last)//Why not add else, delete the head node will be wrong??? {pop_back (list);} Else{s->pre->next = S->next;s->next->pre = S->pre;free (s); list->size--;} return true;} else{cout<< "The item is not exist!" <<endl;return false;}} *//* modifies the value specified in the list */bool modify (List *list,elemtype x) {Node *s = Find_val (list,x); Elemtype item;if (s! = NULL) {cout<< "Please input a new item:"; cin>>item;s->data = Item;return true;} else{cout<< "The item is not exist!" <<endl;return false;}} /* Empty the list */void clear (List *list) {node *s = list->first->next;//s always point to the first node in the list while (s! = List->first) {list-> First->next = s->next;//to empty the first node in the list free (s);//Release first nodes s = list->first->next;//re-points to the new first node}list->last = list->first;list->size = 0;} /* Destroy linked list */void destory (list *list) {clear (list); free (list->first); list->first = List->last = NULL;} /* List inverse: Keep the first node, leave the remaining nodes free, then head into the reserved node */void reverse (list *list) {node *s = list->first->next;//The first node node *p = s->next;//isolate the remaining nodes/* The first node is reversed and becomes the last node */s->next = List->first;list->last = S;while (P! = list->first) {s =      P Save Free the first node P = p->next;//for the next header insert/* will free out the first node---> Head plug-in */s->next = list->first->next;list- >first->next->pre = S;s->pre = List->first;list->first->next = S;/*Push_front (List,s->data); Free (s); Call the Push_front function, the node is created automatically, so you have to release the original node */}}/* the predecessor of the specified element */node *prio (List *list,elemtype x) {Node *s = Find_val (list,x ); if (s = NULL) {if (s = = List->first->next) {cout<< "It doesn ' t have prio!" <<endl;} Else{return S->pre;}} else{cout<< "The item is not exist!" <<endl;return NULL;}} /* For subsequent */node *next of the specified element (List *list,elemtype x{Node *s = Find_val (list,x); if (s = NULL) {if (s = = List->last) {cout<< "It doesn ' t have next!" <<endl;} Else{return S->next;}} else{cout<< "The item is not exist!" <<endl;return NULL;}} /* Insert by Value: If the inserted value already exists, return NULL, otherwise insert the value (assuming the linked list data is ordered: Ascending) */bool insert_val (List *list,elemtype x) {Node *s = Find_val (list,x); = NULL) {cout<< "The item is already exist!" <<endl;return false;} /* Create the node to insert */node *p = (node *) malloc (sizeof (node));p->data = x;/* Find the precursor to insert position */s = List->first;while (s! = list-> Last) {if (S->next->data > x) break;//to be inserted (inserted after s) elses = S->next;} /* Insert */p->next = S->next;s->next->pre = P;p->pre = S;s->next = p;/* To insert the position of the precursor is the last node, that is, the tail of the pointer needs to change the point */ if (s = = list->last) {list->last = P;} List->size++;return true;} /* Ascending */void sort (List *list) {node *s = list->first->next;//First node node *p = s->next;//Isolate the remaining nodes/* The first node is reversed and becomes the last node. */s->next = List->first;list->last = S;while (P! = list->first) {s = p;p = P->next/* * The first node that is freed is inserted by value into the Save node */insert_val (list,s->data); free (s);//Call Insert_val () creates a node, so the original node should be released}} 


Main.cpp:

#include "DCList.h" int main () {List mylist; Node *s;initlist (&mylist); Elemtype item;int choice = 1;while (choice) {Meau ();cout<< "Input You choice:" <<endl;cin>>choice; Switch (choice) {case 1:initlist (&mylist), break;case 2:cout<< "input the item you want to push_back:-1 as a end" &L T;<endl;while (Cin>>item,item! =-1) push_back (&mylist,item); break;case 3:cout<< "Input the item you Want to Push_back:-1 as a End "<<endl;while (cin>>item,item! =-1) push_front (&mylist,item); Break;case 4: Pop_back (&mylist); Break;case 5:pop_front (&mylist); break;case 6:cout<< "Input the item you want to find:" <<endl;cin>>item; Find_val (&mylist,item); if (Find_val (&mylist,item)! = NULL) cout<< "The item is found!" <<endl;elsecout<< "The item is not exist:" <<endl;break;case 7:show (&mylist); Break;case 8:cout << "Input the item want to modify:" <<endl;cin>>item;modify (&mylist,item);Case 9:cout<< "Input of the item want to delete:" <<endl;cin>>item;delete_val (&mylist,item); Break;case 10:clear (&mylist); Break;case 11:destory (&mylist); Break;case 12:reverse (&mylist); break;case 13:cout<< "Input the item want to find it ' s prio:" <<endl;cin>>item;s = Prio (&mylist,item); = NULL) cout<< "It ' s prio is:" <<s->data<<endl;break;case 14:cout<< "input the item you want to Find it's next: "<<endl;cin>>item;s = Next (&mylist,item);; if (s! = NULL) cout<< "It ' s Next is:" <<s->data<<endl;break;case 15:sort (&mylist); Break;case 16 :cout<< "Input the item want to insert:" <<endl;cin>>item;insert_val (&mylist,item); Default:break;}} Destory (&mylist); return 0;}


Specific functions: hope that the reader self-test, if there is a mistake welcome to propose amendments----->>>zyh_helen



(with head and tail pointers) the realization of various functions of cyclic 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.