Manual implementation of single-chain table Class C ++ at the leading Node

Source: Internet
Author: User

I recently reviewed the data structure and saw this part of the Single-chain table. Most of the teaching materials used now are the C language version of Yan Weimin. I have referenced some books, manually compiled the implementation of the c ++ order linked list class. The environment is Visual Studio 2010, and the function is selected and written against the textbook declaration. The Code is as follows:
/* Manual Implementation of the Single-chain table Class C ++ of the leading Node
Author: Many */
# Include <iostream>
Using namespace std;
Class MyList; // declare in advance
Class LNode {// node class
Friend MyList;
Private:
Int data;
LNode * next;
};
Class MyList {// linked list class
Public:
Void InitList (int n); // The table whose header is used to initialize the leading node. The length is n, which is not counted as the header node.
Void DestroyList (); // destroy the table
Void ClearList (); // clear the table
Bool ListEmpty (); // empty
Int ListLength (); // obtain the table length.
Int GetElem (int I); // obtain the value of node I
Int LocateElem (int e); // find the element whose value is e.
Bool ListInsert (int I, int e); // insert element e at position I
Bool ListDelete (int I); // deletes the I-th element.
Void PrintList (); // output linked list
Private:
LNode * head; // header pointer
};
Void MyList: InitList (int n)
{
Int x;
Head = new LNode;
Head-> next = NULL;
For (int I = n; I> 0; -- I)
{
Cin> x;
LNode * p = new LNode;
P-> data = x;
P-> next = head-> next;
Head-> next = p;
}
}
Void MyList: DestroyList ()
{
Delete head;
Cout <"Table destroyed! "<Endl;
}
Void MyList: ClearList ()
{
Head-> next = NULL;
Cout <"cleared! "<Endl;
}
Bool MyList: ListEmpty ()
{
If (head-> next = NULL) return true;
Else return false;
}
Int MyList: ListLength ()
{
LNode * p = head;
Int j = 0;
While (p-> next)
{
P = p-> next;
J ++;
}
Return j;
}
Int MyList: GetElem (int I)
{
Int j = 1;
LNode * p = head-> next; // The first node
If (I <1) return NULL;
While (p & j <I)
{
P = p-> next;
J ++;
}
Return p-> data;
}
Int MyList: LocateElem (int e)
{
Int I = 0;
LNode * p = head-> next;
While (p! = NULL & p-> data! = E)
{
P = p-> next;
I ++;
}
If (p = NULL) return 0;
Else return I + 1;
}
Bool MyList: ListInsert (int I, int e)
{
LNode * p, * s;
Int j = 1;
P = head;
While (p & j <I) {p = p-> next; j ++ ;}
If (p = NULL) return false;
If (s = new LNode) = NULL) return false;
S-> data = e;
S-> next = p-> next;
P-> next = s;
Return true;
}
Bool MyList: ListDelete (int I)
{
LNode * p, * q;
Int j = 1;
P = head;
While (p & j <I) {p = p-> next; j ++ ;}
If (p = NULL) return false;
Q = p-> next;
P-> next = q-> next;
Delete q;
Return true;
}
Void MyList: PrintList ()
{
If (ListEmpty () {cout <"table is empty! "<Endl ;}
Else
{
Cout <"the table element is :";
LNode * p = head-> next;
While (p! = NULL)
{
Cout <p-> data <"";
P = p-> next;
}
Cout <endl;
}
}
 
This article is from the "happy long time" blog

Related Article

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.