Data structure experiment-single-chain table (C ++ implementation)

Source: Internet
Author: User

The source code is as follows:

-----------------------------------------------------------

# Include <iostream>
# Define OK 1
# Define error 0
# Define elems 10

# Define Link (x) static_cast <link> (X)

Using namespace STD;

Typedef struct lnode
{
Int data;
Struct lnode * next;
} * Link, * position;

Typedef int status;

//////////////////////////////////////// ////////////
// Linklist class declaration
//
Class linklist
{
PRIVATE:
Link head, tail, cur;
Int Len;
Int sumofelem (link S) const;
Public:
Linklist ();
~ Linklist ();
Status initelem ();
Status initelem (INT number );
Status clearlist ();
Status insfirst (link & S );
Status delfirst ();
Status append (link & S );
Status remove (link & Q );
Status insbefor (link & P, link & S );
Status insafter (link P, link & S );
Status setcurelem (link P, int e );
Status getcurelem (link & P );
Status listempty ();
Status locatepos (link P );
// Status listtraverse (); what does it mean?
Int listlength ();
Position newlnode (); // auto-Increment Function
Position gethead () const;
Position getlast ();
Position priorpos (link & P );
Position nextpos (link & P );
Void putlist ();
};

 

//////////////////////////////////////// ////////////
// Implement the linklist class | modify Len + = sumofelem (s );
//
Int linklist: sumofelem (link S) const
{
Int sum = 0;
While (s! = NULL)
{
S = s-> next;
Sum ++;
}
Return sum;
}

Linklist: linklist ()
{
Head = (Link) New lnode;
Head-> next = NULL;
Tail = cur = head;
Len = 0;
}

Linklist ::~ Linklist ()
{
Cur = head;
While (Head! = NULL)
{
Head = head-> next;
Delete cur;
Cur = head;
}
}

Status linklist: initelem ()
{
Int I;
Link temp;
Cout <"initialize a list with" <elems <"Numbers:" <Endl;
For (I = 0; I <elems; I ++)
{
Temp = newlnode ();
Cin> temp-> data;
Insfirst (temp );
}
If (! Listempty ())
Return OK;
Else
Return Error;
}

Status linklist: initelem (INT number)
{
Link temp;
Cout <"initialize a list with" <number <"Numbers:" <Endl;
For (INT I = 0; I <elems; I ++)
{
Temp = newlnode ();
Cin> temp-> data;
Insfirst (temp );
}
If (! Listempty ())
Return OK;
Else
Return Error;
}

Status linklist: clearlist ()
{
Link temp;
Temp = head-> next;
Cur = temp;
Head-> next = NULL;
While (temp! = NULL)
{
Temp = temp-> next;
Delete cur;
Cur = temp;
}
Tail = head;
Len = 0;
Return OK;
}

Status linklist: insfirst (link & S)
{
Int length = sumofelem (s );
If (Head = tail)
Tail = s;
S-> next = head-> next;
Head-> next = s;
Len + = length;
Return OK;
}

Status linklist: delfirst ()
{
If (Head! = Tail & Len> 0)
{
Cur = head-> next;
Head-> next = cur-> next;
Len --;
}
Else
{
Cout <"error in delfirst: Len <0" <Endl;
Return Error;
}
Return OK;
}

Status linklist: append (link & S)
{
Int length = sumofelem (s );
If (tail-> next = NULL)
Tail-> next = s;
Else
{
Cout <"error in append: tail-> next! = NULL "<Endl;
Return Error;
}
While (tail-> next! = NULL)
Tail = tail-> next;
Len + = length;
Return OK;
}

Status linklist: Remove (link & Q)
{
Cur = priorpos (tail );
Q = tail;
Tail = cur;
Tail-> next = NULL;
Len --;
If (LEN <0)
{
Cout <"error in remove: Len <0! "<Endl;
Return Error;
}
Else
Return OK;
}

Status linklist: insbefor (link & P, link & S)
{
Int length = sumofelem (s );
If (P = head)
{
Cout <"can't insert in front of head pointer! "<Endl;
Return Error;
}
Else if (P = tail & head = tail)
{
Insfirst (s );
}
Else
{
Cur = priorpos (P );
}
S-> next = P;
Cur-> next = s;
Len + = length;
Return OK;
}

Status linklist: insafter (link P, link & S)
{
Int length = sumofelem (s );
If (Head = tail)
Insfirst (s );
S-> next = p-> next;
P-> next = s;
Len + = length;
Return OK;
}

Status linklist: setcurelem (link P, int e)
{
If (P = head)
{
Cout <"can't set the head pointer." <Endl;
Return Error;
}
Else if (P! = NULL)
P-> DATA = E;
Else
{
Cout <"Unknown error! "<Endl;
Return Error;
}
Return OK;
}

Status linklist: getcurelem (link & P)
{
If (P = head)
{
Cout <"can't visit the head pointer." <Endl;
Return Error;
}
Else if (P! = NULL)
Return p-> data;
Else
{
Cout <"Unknown error! "<Endl;
Return Error;
}
Return OK;
}

Status linklist: listempty ()
{
Return head = tail;
}

Status linklist: locatepos (link P)
{
Int location = 0;
Cur = head;
While (cur! = P)
{
Cur = cur-> next;
Location ++;
}
If (location> 0 & location <= Len)
Return location;
Else
{
Cout <"error in locatepos: location is a wrong number." <Endl;
Return Error;
}
}

Int linklist: listlength () // checked
{
Return Len;
}

Position linklist: newlnode ()
{
Link temp;
Temp = (Link) New lnode;
Temp-> DATA = 0;
Temp-> next = NULL;
Return temp;
}

Position linklist: gethead () const // checked
{
If (head)
Return head;
Else
{
Cout <"error in gethead: Head = NULL! ";
Return NULL;
}
}

Position linklist: getlast () // checked
{
If (tail)
Return tail;
Else
{
Cout <"error in getlast: tail = NULL! ";
Return NULL;
}
}

Position linklist: priorpos (link & P)
{
Cur = head;
While (cur-> next! = P & cur! = NULL)
Cur = cur-> next;
If (cur = NULL)
Return NULL;
Else
Return cur;
}

Position linklist: nextpos (link & P)
{
If (P! = NULL)
Return p-> next;
Else
Return NULL;
}

Void linklist: putlist ()
{
Int I = 0;
Cur = head-> next;
While (cur! = NULL)
{
Cout <cur-> data <"";
Cur = cur-> next;
I ++;
}
Cout <Endl;
Cout <"totally putout" <I <"numbers, and totally recorded" <Len <"numbers! "<Endl;
}

//////////////////////////////////////// ////////////
// Main Function
//
Int main ()
{
Linklist nlink, nlink1;
Nlink. initelem ();
Nlink. putlist ();
Nlink1.initelem ();
Nlink1.putlist ();
Nlink1.delfirst ();
Nlink1.putlist ();
Return 0;
}

 

 

 

// After testing, the program runs normally and no obvious errors are found, but potential errors have not been debugged in depth. It is awaiting further testing ~!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.