Single-chain table implementation
# Include <iostream. h>
Struct Node
{Int daTa;
Node * next;
};
Class linklist
{
Public:
Linklist (){};
Linklist (int A [], int N); // create a single-chain table with n elements
Void printlist (); // traverses a single-chain table and outputs each element by sequence number.
Int get (int I); // query the element value x
Int locate (int x); // query element position I
Void insert (int I, int X); // insert
Int Delete (int I); // Delete
Int length (); // Length
PRIVATE:
Node * First; // the header pointer of a single-chain table
};
Linklist: linklist (int A [], int N) // create a single-chain table with n elements
{
First = new node; // generate a header Node
Node * r, * s;
R = first; // tail pointer Initialization
For (INT I = 0; I <n; I ++)
{
S = new node; s-> daTa = A [I]; // create a node for each array element
R-> next = s; r = s;} // insert it to the terminal node
R-> next = NULL; // after a single-chain table is created, empty the pointer field of the terminal node.
}
Void linklist: printlist ()
{
Node * P;
P = first-> next;
While (P)
{
Cout <p-> daTa <Endl;
P = p-> next;
}
}
Int linklist: Get (int I)
{
Node * P; Int J;
P = first-> next; j = 1; // or P = first; j = 0;
While (P & J <I)
{
P = p-> next; // The Working pointer P moves backward.
J ++;
}
If (! P) throw "location ";
Else return p-> daTa;
}
Int linklist: locate (int x)
{
Node * P; Int J;
P = first-> next; j = 1;
If (P & P-> next ){
While (p-> daTa! = X)
{
P = p-> next;
J ++;
}
Return J;
}
Else throw "location ";
}
Void linklist: insert (int I, int X)
{
Node * P; Int J;
P = first; j = 0; // work pointer P Initialization
While (P & J <i-1)
{
P = p-> next; // The Working pointer P moves backward.
J ++;
}
If (! P) throw "location ";
Else {
Node * s;
S = new node;
S-> daTa = x; // apply for a node s from the memory, whose data domain is X
S-> next = p-> next; // insert node s after node P
P-> next = s;
}
Cout <x <Endl;
}
Int linklist: delete (int I)
{
Node * P; Int J;
P = first; j = 0; // work pointer P Initialization
While (P & J <i-1) // find the I-1 Node
{
P = p-> next;
J ++;
}
If (! P |! P-> next) throw "location"; // node P does not exist or node P's successor node does not exist
Else {
Node * q; int X;
Q = p-> next; X = Q-> daTa; // Save the deleted Node
P-> next = Q-> next; // link Extraction
Delete Q;
Return X;
}
Cout <I <Endl;
}
Int linklist: length ()
{
Node * P = first-> next;
Int I = 0;
While (P)
{
P = p-> next;
I ++;
}
Return I;
}
Void main ()
{
Int B [] = {1, 2, 4, 5 };
Linklist A (B, 5 );
Cout <"the element of single-chain table A is:" <Endl;
A. printlist (); // display all elements in the linked list
Cout <"query the second element by bit:" <Endl;
Cout <"the second element is :";
Cout <A. Get (2) <Endl; // search for the second element in the linked list
Cout <Endl;
Cout <"search by value 5" <Endl;
Cout <"the position of the element with the value of 5 is :";
Cout <A. Locate (5) <Endl; // search for element 5 and return the location in the single-link table
Cout <Endl;
Cout <"Number of inserts:" <Endl;
A. insert (3,9 );
Cout <"after the insert operation is executed, the single-chain table A is:" <Endl;
A. printlist (); // outputs all elements of a single-chain table B.
Cout <"delete" <
A. Delete (4 );
Cout <"Number" <Endl;
Cout <"after the delete operation is executed, the single-chain table A is:" <Endl;
A. printlist ();
Cout <Endl;
Cout <"the length of the last single-chain table A is :";
Cout <A. Length () <Endl;
}