(Including the head pointer and tail pointer) implementation of various functions of the circular two-way linked list, pointer

Source: Internet
Author: User

(Including the head pointer and tail pointer) implementation of various functions of the circular two-way linked list, pointer

Implement the following functions for cyclic double-stranded tables:


Void meau (); // menu function void Initlist (List * list); // initialize void show (List * list); // print bool Push_back (List * list, elemType x); // bool Push_front (List * list, ElemType x); // bool Isempty (List * list ); // determine whether the linked List is empty bool Pop_back (list * List); // bool Pop_front (list * List) by means of tail deletion ); // Delete the Node * Find_val (List * list, ElemType x) by header; // query bool Delete_val (List * list, ElemType x) by value ); // Delete bool modify (List * list, ElemType x) by value; // modify void clear (List * list); // clear the void destory (List * list) of the linked List ); // destroy the linked List void reverse (list * List); // reverse the linked list Node * prio (List * list, ElemType x ); // Node * next (List * list, ElemType x) that is used to evaluate a value; // evaluate the successor bool Insert_val (List * list, ElemType x) of a value ); // 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 the content of the linked List bool Push_back (list * List, ElemType x); // insert bool Push_front (list * List, elemType x); // bool Isempty (List * list); // determines whether the linked List is empty bool Pop_back (list * List ); // bool Pop_front (List * list) by tail deletion; // Node * Find_val (List * list, ElemType x) by header deletion ); // search for bool Delete_val (List * list, ElemType x) by value; // Delete bool modify (List * list, ElemType x) by value ); // modify void clear (List * list); // clear the void destory (List * list); // destroy the linked table void reverse (List * list ); // reverse linked List Node * prio (list * List, ElemType x); // The Node * next (list * List, ElemType x) that is used to evaluate a value ); // find the successor bool Insert_val (List * list, ElemType x) of a value; // 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_back *" <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] p Rio * "<endl; cout <" * [14] next [15] sort * "<endl; cout <"* [16] Insert_val *" <endl;}/* initialize linked List */void Initlist (list * List) {Node * s = (Node *) malloc (sizeof (Node); assert (s! = NULL); s-> next = s-> pre = s; list-> first = list-> last = s; list-> size = 0 ;} /* tail plug method */bool Push_back (List * list, ElemType x) {Node * s = (Node *) malloc (sizeof (Node); 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 linked List content */void show (list * list) {Node * s = list-> first-> next; while (s! = List-> first) {cout <s-> data <"-->"; s = s-> next;} cout <"NULL" <endl ;} /* Header Insertion Method */bool Push_front (List * list, ElemType x) {Node * s = (Node *) malloc (sizeof (Node); if (s! = NULL) {s-> data = x;/* connects to the following elements (the original first element) */s-> next = list-> first-> next; list-> first-> next-> pre = s;/* connect to the header node */list-> first-> next = s; s-> pre = list-> first;/* if it is the first node, the tail Pointer Points to it */if (list-> size = 0) {list-> last = s;} list-> size ++; return true;} else {cout <"An error occurred while applying for a space! "<Endl; return false ;}/ * determines whether the linked List is empty */bool Isempty (list * list) {return (List-> size = 0 );} /* tail deletion method */bool Pop_back (List * list) {if (Isempty (list) {cout <"The linked List is empty! "<Endl; return false;} Node * s = list-> last-> pre; // find the precursor free (list-> last) of the last Node ); // release the last node/* connection */s-> next = list-> first; list-> last = s; list-> size --; return true ;} bool Pop_front (List * list) {if (Isempty (list) {cout <"The linked List is empty! "<Endl; return false;} Node * s = list-> first-> next; // locate the first Node (to be released) /* connect */list-> first-> next = s-> next; s-> next-> pre = list-> first; /* if the node to be deleted is the first node, the tail pointer must be changed to */if (list-> size = 1) {list-> last = list-> first;} free (s); list-> size --; return true;}/* search function: Find the specified element: returns the pointer to it. If not found, 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 deletion and tail deletion are applicable to */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, and the rest of the cases do not need to be changed */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 is an error occurred when deleting the header node if else is not added ??? {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 ;}} * // * modify the specified value in the linked 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 ;}/ * clear the linked List */void clear (list * list) {Node * s = List-> first-> next; // s always points to the first node in the linked list while (s! = List-> first) {list-> first-> next = s-> next; // empty the first node in the linked list for free (s ); // release the first node s = list-> first-> next; // point to the new first node again} list-> last = list-> first; list-> size = 0;}/* destroy linked List */void destory (list * list) {clear (list); free (list-> first ); list-> first = list-> last = NULL;}/* reverse linked list: retain the first node and remove the remaining nodes, then insert the headers to the reserved nodes in sequence */void reverse (List * list) {Node * s = list-> first-> next; // The first Node * p = s-> next; // separate the remaining Node/* The first Node is reversed and becomes the last Node */s-> n Ext = list-> first; list-> last = s; while (p! = List-> first) {s = p; // Save the first node freed from p = p-> next; // prepare for the next plug-in./* Insert the first node that is detached. ---> plug-in-> enter */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 to automatically create a node, so you have to release the original Node */}/* To find the precursor 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 ;}/ * Find the successor of the specified Element */Node * next (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, NULL is returned. Otherwise, the value is inserted (assuming that the Linked List data is sorted in ascending order) */bool Insert_val (List * list, ElemType x) {Node * s = Find_val (list, x); if (s! = NULL) {cout <"the item is already exist! "<Endl; return false;}/* Create the Node to be inserted */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; // locate the position to be inserted (insert after s) elses = s-> next;}/* Insert */p-> next = s-> next; s-> next-> pre = p; p-> pre = s; s-> next = p;/* if you want to insert the position, the first node is the last node, that is, the end node, the tail pointer needs to be changed to */if (s = list-> last) {list-> last = p;} list-> size ++; return true ;} /* ascending */void sort (List * list) {Node * s = list-> first-> next; // The first Node * p = s-> next; // separate 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;/* Insert the first node to the save node by value */Insert_val (list, s-> data); free (s); // call Insert_val () to create 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"<<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 you want to modify:"<<endl;cin>>item;modify(&mylist,item);break;case 9:cout<<"input the item you 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 you want to find it's prio:"<<endl;cin>>item;s = prio(&mylist,item);if(s != 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 you want to insert:"<<endl;cin>>item;Insert_val(&mylist,item);break;default:break;}}destory(&mylist);return 0;}


Specific functions: We hope you can test it by yourself. If you have any errors, please submit your suggestions for modification. -----> zyh_helen



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.