Data Structure and algorithm series (1)-single-chain table class implementation (C ++)
By defining a C ++ class to encapsulate the data structure of a single-chain table,
The encapsulated methods include:
1. Create a single-chain table through input;
2. Obtain the number of data elements in a single-chain table;
3. Print the elements in the output single-link table;
4. Search for the position of an element in a single-chain table;
5. Insert a node after a certain position;
6. delete a node at a certain position;
7. Reverse placement of a single-chain table;
8. check whether a single-chain table has a loop;
9. Sort a single-chain table in ascending order;
10. merge two single-chain tables in ascending order;
11. merge two single-chain tables in descending order.
Note: the sorting of a single-chain table uses the quick sorting method.
The following is the program code written in C ++ and is attached to the running code.
# Include
# Include
# Include
Using namespace std; typedef struct node // node type {int data; // data field node * next; // pointer field} node; class LinkList // single-chain table class definition {public: LinkList (int N = 0) // but the parameter constructor generates a linked list containing N elements (excluding header nodes) {CreateList (N); len = GetLength ();} node * head; // head node pointer int len; // number of elements public: void CreateList (int n ); // create a single-chain table int GetLength () based on the input; // obtain the length (number of elements) of the single-chain table void PrintList (); // print the int SearchNode (int data) of each element in a single-chain table; // query the position of an element in a single-chain table, but it does not exist. 0 is returned. Bool InsertNode (int pos, int data); // bool DeleteNode (int pos), a node with data inserted after the pos position; // The first element after the pos position is deleted, returns true if the deletion is successful. void Reverse (); // Reverse the single-chain table to bool IsLoop (); // checks whether a single-chain table exists and returns true if it exists. If not, falsevoid SelfASOrder () is returned (); // sort the elements in the linked list in ascending order void ASCMerge (LinkList & L); // merge void DESCMerge (LinkList & L) with another linked list in ascending order ); // merge private with the L linked list in descending order: void QuikSort (node * start, node * end) ;}; void LinkList: CreateList (int n) // create a single-chain table {head = new node; head-> data = 0; head-> next = NULL; node * p, * q; int temp; based on the input; q = head; while (n --) {cin> temp; p = new node; p-> data = temp; p-> next = NULL; q-> next = p; q = p; p = NULL;} q = NULL; delete q;} void LinkList: PrintList () // print each element of a single-chain table {len = GetLength (); int l = len; node * p = head-> next; while (l --) {cout <
Data <"; p = p-> next;} p = NULL; delete p; cout <
Next )! = NULL) {l ++; p = p-> next;} return l;} int LinkList: SearchNode (int data) // query the position of an element in a single-chain table, if this element does not exist, 0 {int locate = 0; node * p = head-> next; while (len --) {if (p-> data = data) is returned) {locate = 10-len; break;} p = p-> next;} return locate;} bool LinkList: InsertNode (int pos, int data) // Insert the node {len = GetLength (); if (pos> len) // The insert position is out of the range: return false; node * p = head-> next; if (0 = pos) {p = head; node * q = new node; q-> data = data; q-> next = p-> next; p-> next = Q; p = NULL; q = NULL; delete p; delete q; len ++; return true;} else {pos = pos-1; while (pos --) {p = p-> next;} node * q = new node; q-> data = data; q-> next = p-> next; p-> next = q; p = NULL; q = NULL; delete p; delete q; len ++; return true ;}} bool LinkList: DeleteNode (int pos) {len = GetLength (); if (pos> = len) return false; node * p = head; pos = pos-1; while (pos --) {p = p-> next ;} node * q = p-> next; p-> next = p-> next; delete q; q = NULL; len ++; return true;} v Oid LinkList: Reverse () {node * p, * q, * r; if (head-> next = NULL) {return;} p = head-> next; q = p-> next; p-> next = NULL; while (q! = NULL) {r = q-> next; q-> next = p; p = q; q = r;} head-> next = p;} bool LinkList :: isLoop () {node * p = head-> next; node * q = head-> next; while (q! = NULL) & (p! = Q) {p = p-> next; q = q-> next;} if (p = q) return true; elsereturn false;} void LinkList:: SelfASOrder () // sort in ascending order, using the fast sorting method {node * start, * end; int len = GetLength (); start = head-> next; end = start; while (end-> next )! = NULL) end = end-> next; QuikSort (start, end);} void LinkList: QuikSort (node * start, node * end) {if (start = end) return; int Len = 0; node * st = start; node * en = end; while (st! = En) {st = st-> next; Len ++;} double x = Len/2.0; double xL = floor (x); double xU = ceil (x ); double HalfLen = x; int L = xL; int U = xU; node * mid = start; node * midl = start; while (U --) {mid = mid-> next;} while (L --) {midl = midl-> next;} node * s = start; node * m = mid; int flag = 0; for (int I = 0; I
Data)> (m-> data) {s-> data ^ = m-> data; // exchange m-> data ^ = (s-> data ); s-> data ^ = (m-> data);} s = s-> next; m = m-> next;} QuikSort (start, midl); QuikSort (mid, end);} void LinkList: ASCMerge (LinkList & L) {this-> SelfASOrder (); L. selfASOrder (); node * q = (L. head)-> next; node * p = head-> next; int position = 0; while (p! = NULL) & (q! = NULL) {if (q-> data
Data) {InsertNode (position, q-> data); q = q-> next; position ++;} else {p = p-> next; position ++ ;}} position = GetLength (); while (q! = NULL) {InsertNode (position, q-> data); q = q-> next; position ++ ;}} void LinkList: DESCMerge (LinkList & L) {ASCMerge (L); Reverse () ;}int main () {LinkList L (10); cout <"Input the L1 List:" <
> DataSearch; cout <
> DataInsert; cout <"Input the position to insert:" <
> PosInsert; if (L. InsertNode (PosInsert, DataInsert) {cout <"Insert successfully! The new list is: "; L. PrintList ();} elsecout <" Insert Failed! "<
> PosDel; if (L. DeleteNode (PosDel) {cout <"Delete successfully! The new list is: "; L. PrintList ();} else {cout <" Delete Failed! "<
Run