C ++ Data Structure Single-chain table (Template Class)
Using the template class to implement a single-chain table and its Functions
Operations to be implemented:
[1] push_back [2] push_front
[3] show_list [0] quit_system
[4] pop_back [5] pop_front
[6] insert_val [7] delete_val
[8] find [9] length
[10] clear [11] destroy
[12] reserv [13] sort
Header file source code:
# Ifndef LIST_H_INCLUDED # define LIST_H_INCLUDED # include
Using namespace std; template
Class List; template
Class ListNode {friend class List
; Public: ListNode (): data (Type (), next (NULL) {} ListNode (Type d, ListNode
* N = NULL): data (d), next (n ){}~ ListNode () {} public: void SetData (Type d) {data = d;} Type GetData () const {return data;} private: Type data; ListNode
* Next;}; template
Class List {public: List () {first = last = Buynode ();}~ List () {List
: Destroy ();} public: void push_back (const Type & x); void push_front (const Type & x); void show_list () const; void pop_back (); void pop_front (); void insert_val (const Type & x); void delete_val (const Type & x); bool find (const Type & x); Type length (); void clear (); void destroy (); // destroy the sequence table void reserv (); // reverse void sort (); protected: ListNode
* Buynode (Type x = Type () {ListNode
* P = new ListNode
(X); return p;} private: ListNode
* First; ListNode
* Last;}; template
Void List
: Push_back (const Type & x) {ListNode
* S = Buynode (x); last-> next = s; last = s; first-> data ++;} template
Void List
: Push_front (const Type & x) {ListNode
* S = Buynode (x); s-> next = first-> next; first-> next = s; if (first = last) last = s; first-> data ++;} template
Void List
: Show_list () const {ListNode
* P = first-> next; while (p! = NULL) {cout <
Data <"; p = p-> next;} cout <
Void List
: Pop_back () {if (first-> data = 0) return; ListNode
* P = first; while (p-> next! = Last) p = p-> next; ListNode
* Q = p-> next; p-> next = NULL; last = p; delete q; first-> data --;} template
Void List
: Pop_front () {ListNode
* P = first-> next; first-> next = p-> next; p = NULL; delete p; if (first-> data = 1) last = first; first-> data --;} template
Void List
: Insert_val (const Type & x) {ListNode
* S = Buynode (x); ListNode
* P = first-> next; if (p-> data> s-> data) {push_front (s-> data );} else if (last-> data <s-> data) {push_back (s-> data);} else {while (p-> data <x) & (p-> next-> data
Next;} s-> next = p-> next; p-> next = s; first-> data ++;} template
Void List
: Delete_val (const Type & x) {ListNode
* P = first-> next; ListNode
* S = Buynode (x); if (x = p-> data) {pop_front ();} else {while (p-> data! = X) & (p-> next-> data! = X) {p = p-> next;} p-> next = p-> next; ListNode
* Q = p-> next; delete q; first-> data --;} template
Bool List
: Find (const Type & x) {ListNode
* P = first-> next; while (p! = NULL & p-> data! = X) p = p-> next; return p;} template
Type List
: Length () {cout <"length =" <
Data <
Data;} template
Void List
: Clear () {while (first-> data> 0) pop_front ();} template
Void List
: Destroy () // destroy this sequence table {clear (); delete first; first = last = NULL;} template
Void List
: Reserv () // reverse, setting the entire tablespace to {ListNode
* P = first-> next; ListNode
* Curr = p-> next; ListNode
* Tmp = NULL; first-> next = NULL; while (curr) // direct the cursor to next {tmp = curr-> next; curr-> next = p; p = curr; curr = tmp;} first-> next = p;} template
Void List
: Sort () {ListNode
* S = first-> next; while (s-> next) {ListNode
* P = s; while (p-> next) {if (s-> data> p-> next-> data) {Type tmp = s-> data; s-> data = p-> next-> data; p-> next-> data = tmp;} p = p-> next ;} s = s-> next ;}# endif // LIST_H_INCLUDED
Main function:
# Include "List. h" int main () {List
Mylist; int select = 1; int Item; int pos; while (select) {cout <"*********************************** * ** "<
"; Cin> select; switch (select) {case 1: cout <" Enter the value to be inserted (-1 ends):> "; while (cin> Item, Item! =-1) {mylist. push_back (Item);} break; case 2: cout <"Enter the value to be inserted (-1 ended):>"; 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 <"Enter the value to insert:>"; cin> Item; mylist. insert_val (Item); break; case 7: cout <"Enter the value to delete:>"; cin> Item; mylist. delete_val (Item); break; case 8: cout <"Enter the value to be searched:>"; cin> Item; mylist. find (Item); break; case 9: mylist. length (); break; case 10: mylist. clear (); break; case 11: mylist. destroy (); break; case 12: mylist. reserv (); break; case 13: mylist. sort (); break; default: break ;}}}