The realization of double-linked list and its function Daquan!!!

Source: Internet
Author: User

Header file seqlist.h#include<iostream> using namespace std; #include <assert.h> #define Elemtype inttypedef  struct node{elemtype data;  struct Node * NEXT; struct Node * prio;}  node,*pnode;typedef struct seqlist{pnodefirst;      Pnode last; size_t size;}   Implementation of the list;/////////////////////////function void Initlist (List *list) {node *s = (node *) malloc (sizeof (node));   ASSERT (s! = NULL);   List->first = List->last =s;   List->first->next =s;   List->first->prio =s;   S->prio =list->first;      S->next = list->first; list->size = 0;}   BOOL Push_back (List *list,elemtype x) {Node *s = (node *) malloc (sizeof (node));     if (s = = NULL) return false;   S->data = x;   List->last->next = s;   S->prio = list->last;   S->next = list->first;   S->next->prio = s;      List->last =s;  List->size + +; return true;}   BOOL Push_front (List *list,elemtype x) {Node *s = (node *) malloc (sizeof (node));   if (s = = NULL) return false; S-&gT;data = x;   S->next = list->first->next;   S->next->prio = s;   S->prio = list->first;      List->first->next = s;   if (list->size = = 0) {List->last = s;  } list->size++; return true;}  void Show_seqlist (List *list) {Node *s = list->first->next;  while (s! = List->first) {cout<<s->data<< "--";s=s->next; } cout<< "End" &LT;&LT;ENDL;}   BOOL Pop_back (List *list) {if (list->size = = 0) return false;      Node *p = list->last;   List->last = p->prio;   P->prio->next = list->first;   P->next->prio = p->prio;   Free (p);  list->size--; return true;}  BOOL Pop_front (List *list) {if (list->size = = 0) return false;  Node *p = list->first->next;  P->prio->next = p->next;    P->next->prio = p->prio;  if (list->size = = 1) {list->last = list->first;  } list->size--; Free (p); return true;} BOOL Insert_val (List *list,elemtype key) {if (list-> size = = 0 | |   Key>list->last->data) {push_back (List,key);    return true;}    Node *s = (node *) malloc (sizeof (node)); if (s = = NULL) return false;  S->data = key; Node *pre =list->first->next;while (Key > Pre->data) {pre = Pre->next;} Pre->prio->next = S; S->prio = pre->prio; S->next = pre; Pre->prio = s;   list->size++; return true;   }bool delete_val (List * List,elemtype key) {if (list->size = = 0) return false;      Node *pre = list->first->next;   while (key = Pre->data) {pre = pre->next;   if (pre = = List->first) {cout<< "not exit" <<endl;   return false;}   Pre->prio->next = pre->next;   Pre->next->prio = pre->prio;   if (pre = = list->last) list->last =pre->prio;   Free (pre);  list->size--; return true;}   BOOL Sort (List *list) {if (list->size = = 0 | | list->size ==1) return false;   Node * Pre =list->first->next;      Node *pro = pre->next; pre-&Gt;next =list->first;   List->first->prio =pre;      List->last = pre; while (pro! = List->first) {if (Pro->data > List->last->data) {list->last->next =pro;pro->pri  o =list->last;list->last = Pro;pro = pro->next; Continue         } pre = List->first->next;while (Pro->data > Pre->data) pre=pre->next; Node * SS =pro; Pro = pro->next; Pre->prio->next = SS; Ss->next = pre; Ss->prio =pre->prio;   Ss->next =pre;   } List->last->next = list->first;   List->first->prio =list->last; return true;}   BOOL Find (List *list,elemtype key) {if (list->size = = 0) return false;  Node *pre= list->first->next;   while (Key!=pre->data) {if (pre = = NULL) {cout<< "Not exit" <<endl;}  Pre = pre->next; } cout<< "Exit:" <<pre->data<<endl; return true;}  BOOL Modify (List *list,elemtype Key,elemtype x) {if (list->size = = 0) return false; Node *pre= list->first->next;    while (Key!=pre->data) {if (pre = = NULL) {cout<< "not exit" &LT;&LT;ENDL;}  Pre = pre->next;  } cout<< "before modification:";  Show_seqlist (list);  Pre->data = x;  cout<< "Modified:";   Show_seqlist (list); return true;} BOOL Clear (List *list) {if (list->size = = 0) return false; Node *pre =list->first->next;   while (pre! = List->first) {pre->data = 0; pre=pre->next; } return true;  void Destroy (List *list) {if (list->size = = 0) return;    Node *pre =list->first->next;   while (pre! = List->first) {Node *pro = pre;   Pre = pre->next;  Free (PRO);  } list->last = list->first;  List->last->next = list->first;  list->last->prio=list->first;  List->first->prio =list->first; List->first->next =list->first;} void Length (list * list) {Cout<<list->size<<endl;}   void Next (List *list,elemtype key) {if (list->size = = 0) return; Node *pre= List->first->next    while (Key!=pre->data) {if (pre = = NULL) {cout<< "Not Exit" <<endl;}  Pre = pre->next; } cout<< "exit:" &LT;&LT;PRE-&GT;NEXT-&GT;DATA&LT;&LT;ENDL;}   void Prio (List *list,elemtype key) {if (list->size = = 0) return;  Node *pre= list->first->next;    while (Key!=pre->data) {if (pre = = NULL) {cout<< "not Exit" &LT;&LT;ENDL;}  Pre = pre->next; } cout<< "exit:" &LT;&LT;PRE-&GT;PRIO-&GT;DATA&LT;&LT;ENDL;}
#include "seqlist.h" void Main () {int Select;int pos = 0; List MyList; Elemtype x; Elemtype item;  Node *p =null;initlist (&mylist); while (select) {cout<< "**********************************" <<endl;cout<< "* [0] quit_system [1] Push _back * "<<endl;cout<<" * [2] push_front [3] show_seqlist* "<<endl;cout<<" * [4] pop_back [5] Po P_front * "<<endl;cout<<" * [6] sort [7]insert_val * "<<endl;cout<<" * [8] delete_val [9] Find * "<<endl;cout<<" * [10]modify [11]clear * "<<endl;cout<<" * [12]destroy [1] 3]length * "<<endl;cout<<" * [14]next [15]prio * "<<endl;cout<<" ****************** "<<endl;cout<<" Please select:> "; cin>>select;switch (select) {Case 1:cout<<" Please enter the value you want to insert (ends with-1): "<<endl; while (Cin>>item,item! =-1) push_back (&mylist,item); break;case 2:cout<< "Enter the value you want to insert (end with-1):" << Endl while (cIn>>item,item! =-1) push_front (&mylist,item); Break;case 3:show_seqlist (&mylist); Break;case 4:pop_bac   K (&mylist); Break;case 5:pop_front (&mylist); break;case 7:cout<< "Please enter the value you want to insert";   cin>>item;   Insert_val (&mylist,item); break;case 8:cout<< "Please enter the value you want to delete";   cin>>item; Delete_val (&mylist,item); Break;case 6:sort (&mylist); break;case 9:cout<< "Please enter the value you want to find"; Cin>>item ; find (&mylist,item); break;case 10:cout<< "Please enter the values you want to modify and the modified values separately:" Cin>>item>>x;modify (& MYLIST,ITEM,X); break;case 11:clear (&mylist); Break;case 12:destroy (&mylist); Break;case 13:length (&     amp;mylist); break;case 14:cout<< "Please enter the number of successors you require:";cin>>item;     Next (&mylist,item); break;case 15:cout<< "Please enter the number of precursors you require:";cin>>item;  Prio (&mylist,item); break;default:break;}}


The realization of double-linked list and its function Daquan!!!

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.