<pre name= "code" class= "CPP" > #ifndef _seqlist_#define _seqlist_#include<iostream>using namespace std;# Include<assert.h>template<class type> class slist;//node classes Template<class type>class Node{public:Node ():d ATA (Type ()), Next (NULL) {}node (type D, node<type> *n = NULL):d ata (d), Next (n) {}void SetData (type D) {data = D;} Type GetData () Const{return data;} ~node () {}private:type data; Node <Type> *next;friend class slist< type>;};/ /template<class type>class slist{public:slist () {node<type> *s= _buynode (0); first = last = S;size = 0;} ~slist () {}node<type>* _buynode (const Type &x) {node<type>* s = new node<type> (x); assert (S!=null) ; return s;} Private:node<type> *first; Node <type>*last;int size;public:bool push_back (const Type &x) {node<type>* s = _buynode (x);last-> Next = S;last = S;size++;return true;} BOOL Push_front (const Type &x) {node<type>* s = _buynode (x); s->next = First->next;fIrst->next = s;if (size ==0)//0{last = s;} Size++;return true;} BOOL Pop_back () {if (size==0)//Why write (0==size), the following call error; return false;{ node<type>* p = First; node<type>* q = p->next;while (q->next! = NULL) {q = Q->next;p = P->next;} Delete Last;last = P;last->next = null;//program crashes; size--; }return true;} BOOL Pop_front () {if (size = = 0) return false;if (size = = 1) pop_back (); else{node<type>* s = First->next;first-> ; next = S->next;delete s;size--;} return true;} /*bool insert_val2 (const Type &x) {node<type>* p = first;while (P->next->data < x && P->next ! = NULL) P =p->next; if (P->next = = NULL) {push_back (x); }else{node<type>* s = _buynode (x); s->next = P->next;p->next = s;size++; } return true; */bool insert_val (const Type &x) {if (size = = 0) push_back (x); node<type> *p = First;while (p->next! = NULL && p->next->data < x) p = p->next;if (p->next = = NULL) {Push_bACK (x);} else{node<type> *s = _buynode (x); s->next = P->next;p->next = s;size++;} return true;} Type Length () {return size;} node<type>* Find (const Type &key) {if (size = = 0) return NULL; node<type> *p = First->next;while (P! = NULL && p->data! = key) p = P->next;return p;} BOOL Delete_val (const Type &x) {if (size = = 0) return false; Node<type>*p = Find (x); if (p = = NULL) return false; if (p = = last) {Pop_back ();} else {node<type>*q = p->next; P->data = q->data; P->next = q->next; D Elete Q; size--; } return true; }void sort () {if (size = = 0 | | size = = 1) return; node<type>* s= first->next; node<type>* Q =s->next; last = s; S->next = NULL; while (q! = NULL) {s = q; q = q->next; Node<type>*p = First; while (p->next! = NULL && p->next->data < s->data) p = p->next; if (P->next = = null) {s->next = null; Last->next = s; last = s;} else {s->Next = p->next; P->next = s; }}}void Resver () {if (size = = 0 | | size = = 1) return; node<type>* s = first->next; node<type>* q = s->next;last = S;s->next = Null;while (q! = NULL) {s = q;q = Q->next;push_front (S->data); }}bool Next (const Type &x) {node<type>*p=find (x); if (p = = NULL) {cout << "failed to find" << x << Endl;ret Urn false;} Node<type>*q = First;while (q->next!= null&&q->data!=x) {q = Q->next;} if (Q->next = = NULL) {cout << x << "no successor" << Endl;return false;} if (q->data = x) cout << x << "successor is" << Q->next->data << Endl;return true; }bool prio (const type&x) {node<type>*p = find (x), if (p = = NULL) {cout << "failed to find" << x << Endl;ret Urn false;} Node<type>*q = First;while (Q->data! = x) {q = Q->next;} if (q = = First->next) {cout << x << "no precursor" << Endl;return false;} if (Q->data = x) {node<type>*s = First; while (s->next! = q) {s = s->next;} cout << x << "The precursor is" << s->data << Endl;} return true;} void Clear () {node<type>*p = First->next;while (P! = NULL) {first->next= P->next;delete p;p = First->nex t;} First->next = Null;last = first;size = 0; } void Show_list () {if (size = = 0) return; node<type> * p = first->next;while (P! = NULL) {cout << p->data << "--";p =p->next;} cout << "OK" << Endl;}; #endif
#include "SeqList.h" void Main () {slist<int> mylist;int Item;int select = 1; node<int>* p;while (Select) {cout << "************************************" << endl;cout << "* [ 1] push_back [2] Push_front * "<< endl;cout <<" * [3] show_list [4] pop_back * "<< endl;cout <<" * [5] pop_fornt [6] insert_val * "<< endl;cout <<" * [7] length [8] Find * "<< endl;cout <<" * [9] Merge [ten] delete_val* "<< endl;cout <<" * [one] sort [all] resver * << endl;cout << "* [] next [ Prio * "<< endl;cout <<" * [[] clear [0] quit_system* "<< endl;cout <<" ********************* << endl;cout << Select service Item:>, CIN >> select;switch (SELECT) {Case 1:cout << " Please enter the data to be inserted (-1 end):> "; while (Cin >> Item, item!=-1) {Mylist.push_back (item);} Break;case 2:cout << "Please enter the data to be inserted (-1 end):>"; while (CIN >> item, Item! =-1) {Mylist.push_front (item);}Break;case 3:mylist.show_list (); Break;case 4:mylist.pop_back (); break;case 5:mylist.pop_front (); Break;case 6:cout << "Please enter the value to insert:>"; cin > > Item;mylist.insert_val (Item), break;case 7:cout << "Sequential table length:>" << mylist.length () << Endl; Break;case 8:cout << "Please enter the value to find:>"; Cin >> Item; p = mylist.find (Item); if (p = = NULL) {cout << "the data to find does not exist." << Endl;} Break;case 9:break;case 10:cout << "Please enter the value to delete:>"; Cin >> Item;mylist.delete_val (Item); Break;case 11: Mylist.sort (); break;case 12:mylist.resver () break;case 13:cout << "Please enter the number of successors to be checked"; Cin >> Item;mylist.next ( Item); Break;case 14:cout << "Please enter the number of precursors to check"; Cin >> Item;mylist.prio (item); Break;case 15:mylist.clear (); break ;d Efault:break;} }system ("Pause");}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Single linked list (C + + lead node,)