Zookeeper
There is no essential difference between a linked list and a single-chain table. Instead of being empty, the unique linked list points to the first pointer. Only in this way can we implement the Circular function of the linked list. The problem arises, in the following function, we just need to change the reuse name of the header pointer to first-> next, and the counter count also starts from 1, in this way, the first step in the while loop is avoided. Not much nonsense. For more information, see the code of Wo.
# Ifndef cirlinklist_h
# Define cirlinklist_h
# Include <iostream>
Using namespace STD;
Template <typename T>
Struct node {// Node
T data;
Node <t> * next;
};
Template <typename T>
Class cirlinklist {// a single-chain cyclic table with no Headers
Template <typename T>
Friend ostream & operator <(ostream &, cirlinklist <t> &);
Public:
Cirlinklist (); // create an empty cyclic single-chain table (that is, first points to a null pointer ).
Cirlinklist (t a [], int N); // create a Circular Single-chain table containing n elements.
~ Cirlinklist (); // destructor to clear all nodes.
Int length (); // calculates the table length.
T get (int I); // obtain the I-th element of the table. 10 points
Void insert (int I, T & X); // Insert a new element after the I node.
T Delete (int I); // Delete the I-th element.
Bool isempty (); // determines whether the table is empty.
Void delthesame (); // Delete the same element in the Table. Only one element is retained.
PRIVATE:
Node <t> * first;
};
Template <typename T>
Ostream & operator <(ostream & OS, cirlinklist <t> & L ){
Node <t> * P = L. First-> next;
If (P! = L. First ){
Do {
OS <p-> data <",";
P = p-> next;
} While (P! = L. First );
}
Else
OS <Endl;
Return OS;
}
Template <typename T>
Cirlinklist <t>: cirlinklist ()
{
Node <t> * first;
First = new node <t>;
First-> next = first; // initialize the cyclic single-chain table
}
Template <typename T>
Cirlinklist <t>: cirlinklist (t a [], int N)
{
Node <t> * r, * s;
First = new node <t>;
R = first;
For (INT I = 0; I <n; I ++)
{
S = new node <t>;
S-> DATA = A [I];
R-> next = s;
R = s;
}
R-> next = first; // The first has not changed
}
Template <typename T>
Cirlinklist <t> ::~ Cirlinklist ()
{
Node <t> * q = NULL;
While (first! = NULL)
{
Q = first;
First = first-> next;
Delete Q; // is there a problem?
}
}
Template <typename T>
Int cirlinklist <t >:: length ()
{
Node <t> * P = first-> next;
Int COUNT = 0;
While (P! = First)
{
P = p-> next;
Count ++;
}
Return count;
}
Template <typename T>
T cirlinklist <t>: Get (int I)
{
Node <t> * P = first-> next;
Int COUNT = 1;
While (P! = First & count <I)
{
P = p-> next;
Count ++;
}
If (P = first) throw "location ";
Else return p-> data;
}
Template <typename T>
Void cirlinklist <t >:: insert (int I, T & X)
{
Node <t> * P = first-> next;
Int COUNT = 1;
While (P! = First & count <I)
{
P = p-> next;
Count ++;
}
If (P = first) throw "location ";
Else
{
Node <t> * s;
S = new node <t>;
S-> DATA = X;
S-> next = p-> next;
P-> next = s;
}
}
Template <typename T>
T cirlinklist <t>: delete (int I)
{
Node <t> * P = first-> next;
T x; int COUNT = 1;
While (P! = First & count <i-1)
{
P = p-> next;
Count ++;
}
If (P = first | p-> next = first) throw "location ";
Else
{
Node <t> * q;
Q = p-> next;
X = Q-> data;
P-> next = Q-> next;
Delete Q;
Return X;
}
}
Template <typename T>
Bool cirlinklist <t >:: isempty ()
{
Node <t> * q;
If (first = first-> next)
Return 0;
}
Template <typename T>
Void cirlinklist <t >:: delthesame ()
{
Node <t> * q = first-> next;
While (Q! = First)
{
Node <t> * s = Q-> next;
Node <t> * R;
R = s;
If (q = r)
{
Delete S;
R = r-> next;
}
}
}
# Endif
**************************************** ************
# Include <iostream>
# Include "cirlinklist. H"
Using namespace STD;
Int main (){
Int ary [] = {2, 4, 6, 8, 12, 32, 6, 9, 2, 7}, x = 11;
Cirlinklist <int> mylink (ary, 11 );
Cout <"linked list content:" <mylink <Endl;
Cout <"chain table length:" <mylink. Length () <Endl;
Cout <"11th element:" <mylink. Get (11) <Endl;
Mylink. insert (3, X );
Cout <"insert 11 after element 3, content of the linked list:" <mylink <Endl;
Cout <"deleted element No. 5:" <mylink. Delete (5) <Endl;
Cout <"current content of the linked list:" <mylink <Endl;
Mylink. delthesame ();
Cout <"after deleting the same element, the content of the linked list:" <mylink <Endl;
If (mylink. isempty ())
Cout <"null :";
Return 0;
}
Cyclic linked list