Simple Linked List Operation

Source: Internet
Author: User

// After the data is learned, the chain table is written for the second time. It feels a little better than the first time.

# Include <iostream>
# Include <cstdlib>
Using namespace STD;

Typedef int datatype;

Class node // node class
{
PRIVATE:

Datatype data;

Node * next;

Public:
Friend class linklist;

Node (datatype d) // construct a node
{
Data = D;

Next = NULL;
}

Node () // construct the header Node
{
Next = NULL;
}

};

Class linklist
{
Friend class node;
PRIVATE:
Node * head;

Public:
Linklist ();

~ Linklist ();

Void insert (datatype D); // insert

Void insert (int n, datatype D); // Insert D after N

Void deteledata (int n); // delete data

Void sort (); // sort, large-> small

Void reversal (); // reverse local configuration

Void print (); // display

};

Linklist: linklist ()
{
Head = new node ();
}

Void linklist: insert (datatype d) // insert
{
Node * P = new node (d );
 
P-> next = head-> next;
 
Head-> next = P;

}
Void linklist: insert (int n, datatype D)
{
Node * P = head;

While (n! = 0)
{
P = p-> next; // point to N nodes

N --;
}

If (P = NULL)
{
Cout <"wrong:" <n <"not have data" <Endl;

Return;
}
Else
{
Node * q = new node (d );

Q-> next = p-> next; // insert P after

P-> next = Q;
}
}

Void linklist: deteledata (int n)
{
Node * P = head;

While (n-1 )! = 0)
{
P = p-> next; // P points to the previous node of N.

N --;
}
If (P = NULL)
{
Cout <"wrong: there is no data Delete" <Endl;

Return;
}

Else
{Node * q = p-> next;

P-> next = p-> next;

Delete Q;
}
}

Void linklist: Sort () // sort
{
Node * P, * q;

Node * qnext;

P = head;

Q = p-> next;

P-> next = NULL; // disconnect the first node

Qnext = Q;

While (Q! = NULL)
{
P = head;
 
While (p-> next-> DATA> q-> data)
{
P = p-> next; // P indicates the node before the number of inserts.

If (p-> next = NULL) break;
}
Qnext = Q-> next;

Q-> next = p-> next; // insert P after

P-> next = Q;

Q = qnext;
}

}
Void linklist: Reversal ()
{
Node * P = head-> next;

Node * pvalue;

Head-> next = NULL; // disconnect the header Node

While (pvalue! = NULL)
{
Pvalue = p-> next;

P-> next = head-> next; // insert data to the header Node

Head-> next = P;
 
P = pvalue;
}
}
Void linklist: Print () // display
{
Node * P;

P = head-> next;

If (P = NULL) // if it is null, return
{
Cout <"wrong: No data" <Endl;

Return;
}

While (P! = NULL) // display until it is null
{
Cout <p-> data <"";

P = p-> next;
}
Cout <Endl;

}
Linklist ::~ Linklist ()
{
Node * P;

While (Head! = NULL)
{
P = head;

Head = head-> next; // delete all nodes

Delete P;
}
}

Void main ()
{
Linklist link;

Int data;

For (INT I = 0; I <10; I ++)
{

Data = rand () % 15 + 1; // generate random number 1 ~ 15

Link. insert (data );

}

Link. insert (2, 7 );

Link. deteledata (6 );

Link. reversal ();

Link. Print ();

Link. Sort ();

Link. Print ();
}

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.