Implementation of linked list classes in C ++

Source: Internet
Author: User

Question requirements:

1. Define a linear table class linearlist composed of int elements. It has the following member functions:

Bool insert (int x, int POS); // insert an element x after position POS.
// When POS is 0, insert it before the first element.
// If the operation succeeds, true is returned. Otherwise, false is returned.
Bool remove (Int & X, int POS); // Delete the element at the position POS.
// If the operation succeeds, true is returned. Otherwise, false is returned.
Int element (int pos) const; // returns the element at the position POS.
Int search (int x) const;
// Find the element whose value is X and return the position of the element (The position of the first element is 1 ). If no value is found, 0 is returned.

Int length () const; // returns the number of elements.

For the first time, I wrote my own code. Although I had some help from Wang,

# Include <iostream>
Using namespace STD;
Struct Node
{
Int element;
Node * next;
};

Class linerlist {
Node * head;
Int Len;
Public:
Linerlist () {Len = 0; head = NULL ;}
Bool insert (int x, int POS );
Bool remove (Int & X, int POS );
Int element (int pos) const;
Int search (int x) const;
Int length () const;
Void show ();
};
Bool linerlist: insert (int x, int POS ){
If (Pos <0)
{
Cout <"enter the correct POS value (requires> 0)" <Endl;
Return false;
} Else if (Pos = 0)
{
Node * P = new node;
P-> element = X;
If (Head! = NULL)
{

P-> next = head;
Head = P;
P-> next = NULL;
Len ++;
Return true;
} Else {
Head = P;
P-> next = NULL;
Len ++;
Return true;
}

} Else if (Pos> 0 & Pos <Len)
{
If (Head! = NULL)
{
Node * P = new node;
P-> element = X;
Node * P1 = head, * P2 = head;
For (INT I = 0; I <Pos; I ++)
{
P2 = p1;
P1 = p1-> next;
}
P2-> next = P;
P-> next = p1;
Len ++;
Return true;
} Else {
Node * P = new node;
P-> element = X;
Head = P;
P-> next = NULL;
Len ++;
Return true;
}

} Else {
Node * P = new node;
P-> element = X;
Node * P1 = head;
While (P1-> next! = NULL)
{
P1 = p1-> next;
}
P1-> next = P;
P-> next = NULL;
Len ++;
Return true;
}
}

Bool linerlist: Remove (Int & X, int POS ){
If (Pos <0 | POS> = Len)
{
Cout <"enter the correct POS value! "<Endl;
Return false;
} Else if (Pos = 0)
{
Node * P = head;
X = p-> element;
Head = p-> next;
Len --;
Return true;
} Else if (Pos> 0 & Pos <len-1 ){
Node * P1 = head, * P2 = head;
For (INT I = 0; I <Pos; I ++)
{
P2 = p1;
P1 = p1-> next;
}
P2-> next = NULL;
X = p1-> element;
P1 = p1-> next;
P2-> next = p1;
Len --;
Return true;
} Else
{
Node * P1 = head, * P2 = head;
For (INT I = 0; I <Pos; I ++)
{
P2 = p1;
P1 = p1-> next;
}
P2-> next = NULL;
Len --;
Return true;
}
}

Int linerlist: element (int pos) const {

If (Pos> = Len | POS <0)
{
Cout <"enter the correct POS value" <Endl;
Return-1;
} Else {
Node * P1 = head;
For (INT I = 0; I <Pos; I ++)
{
P1 = p1-> next;
}
Return P1-> element;
}
}

Int linerlist: Search (int x) const {
Node * P = head;
For (INT I = 0; I <Len; I ++)
{
If (p-> element = X)
{
Return I + 1;
}
P = p-> next;
}
Return 0;
}

Int linerlist: length () const {
Return Len;
}

Void linerlist: Show (){
Node * P = head;
While (P! = NULL)
{
Cout <p-> element <"";
P = p-> next;
}
}

Void main (){
Linerlist lList;
LList. insert (1, 0 );
LList. insert (2, 1 );
LList. insert (3, 1 );
LList. insert (4, 5 );
LList. Show ();
Cout <lList. Length () <"";
Int x = lList. Search (2 );
Cout <X;
}

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.