Sort and reverse view of insertion/deletion of a linked list

Source: Internet
Author: User

[Cpp] view plaincopy
# Include <iostream>
Using namespace std;
# Include <stdio. h>
# Include <stdexcept>
# Include <conio. h>
# Include <string. h>
# Include <stack>
 
 
Struct Node
{
Int data;
Struct Node * next;
};
 
Node * Creat ()
{
Node * head, * p, * s;
Head = (Node *) malloc (sizeof (Node ));
P = head;
Int n;
While (scanf ("% d", & n)
{
S = (Node *) malloc (sizeof (Node ));
S-> data = n;
P-> next = s;
P = s;
}
Head = head-> next;
P-> next = NULL;
Return head;
}
 
Int Length (Node * head)
{
Node * p;
P = head;
Int len = 0;
While (NULL! = P)
{
P = p-> next;
++ Len;
}
Return len;
}
 
Node * Del (Node * & head, int num)
{
Node * p1, * p2;
P1 = head;
While (num! = P1-> data & NULL! = P1-> next)
{
P2 = p1;
P1 = p1-> next;
}
If (num = p1-> data)
{
If (p1 = head)
{
Head = p1-> next;
Free (p1 );
}
Else
{
P2-> next = p1-> next;
}
}
Else
{
Printf ("% d cound not be found \ n", num );
}
Return head;
}
// When the header is inserted, this is a change of the header node. Therefore, note the difference between the reference and pointer.
// How can I modify the value of a variable in a function ??? Baidu
// This situation is always easy to forget. Please pay attention to it.
Node * Insert (Node * & head, int num)
{
Node * p0, * p1, * p2;
P1 = head;
P0 = (Node *) malloc (sizeof (Node ));
P0-> data = num;
While (num> p1-> data & NULL! = P1-> next)
{
P2 = p1;
P1 = p1-> next;
}
If (num <= p1-> data)
{
If (head = p1)
{
P0-> next = head;
Head = p0;
}
Else
{
P2-> next = p0;
P0-> next = p1;
}
}
Else
{
P1-> next = p0;
P1-> next = NULL;
}
Return head;
}
 
 
Node * Sorted (Node * head) // simple Bubble sorting method
{
Node * p1, * p2;
P1 = head;
P2 = head;
Int len = Length (head );
If (NULL = head | NULL = head-> next)
Return head;
For (int I = 0; I <len; ++ I)
{
P2 = p1;
For (int j = I + 1; j <len; ++ j)
{
If (p1-> data> p2-> next-> data)
{
P1-> data ^ = p2-> next-> data;
P2-> next-> data ^ = p1-> data;
P1-> data ^ = p2-> next-> data;
}
P2 = p2-> next;
}
P1 = p1-> next;
}
Return head;
}
 
 
Node * Reverse (Node * & head)
{
Node * p1, * p2, * p3;
P1 = head;
If (NULL = head | NULL = head-> next)
{
Return head;
}
P2 = p1-> next;
While (p2)
{
P3 = p2-> next;
P2-> next = p1;
P1 = p2;
P2 = p3;
}
Head-> next = NULL;
Head = p1;
Return head;
}
 
 
Void Print (Node * head)
{
Node * p;
P = head;
While (NULL! = P)
{
Printf ("% d", p-> data );
P = p-> next;
}
Printf ("\ n ");
}
[Cpp]

[Cpp]
<Pre class = "cpp" name = "code"> # include "list. h"
 
Int main ()
{
Node * head;
Int del_num, insert_num;
Head = Creat ();
Print (head );
Cout <"\ n ";
Cout <"Length of the linked list:" <Length (head) <"\ n ";
Cin> del_num;
Del (head, del_num );
Cout <"deleted linked list :";
Print (head );
 
While (cin> insert_num & insert_num)
{
Cout <"inserted linked list :";
Insert (head, insert_num );
Print (head );
}

Reverse (head );
Cout <"reverse output :";
Print (head );


Sorted (head );
Cout <"sorted linked list :";
Print (head );

System ("pause ");
 
Return 0;
} </Pre> <br>
<Pre> </pre>
<Br>

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.