Simple operations on linked lists

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;
Typedef struct node {
Int num;
Node * next;
} Node;

Int insert (node * head, int m ){
Int flag = 1;
Node * n = head, * N1;
Node * P = new node;
P-> num = m;
P-> next = NULL;
If (n-> num> P-> num)
{
N-> num = p-> num-1;
}

While (n! = NULL & N-> num <p-> num ){
N1 = N;
N = N-> next;
}
N1-> next = P;
P-> next = N;

Return flag;
}

Int findlist (node * head, int m)
{
Node * n = head;
While (n-> next! = NULL)
{
If (n-> next-> num = m)
Return 1;
N = N-> next;
}
Return 0;
}

Int Delete _ (node * head, int m)
{

Node * n = head, * N1;
Int flag = 0;
If (Head-> next = NULL)
{
Cout <"This linked list is an empty linked list and cannot be deleted ";
}
While (n! = NULL & N-> num! = M ){
N1 = N;
N = N-> next;
}
If (n! = NULL ){
If (n-> num = m ){
N1-> next = N-> next;
Delete N;
Flag = 1;
Cout <m <"has been deleted! "<Endl;
}
Else
Flag = 0;
}
Return flag;
}
/* Print the menu command */
Void instructions (void)
{
Cout <"Enter your choice:" <Endl
<"1 insert an element to the linked list./N"
<"2 delete a number from the linked list./N"
<"3 enter a number to search for existence./N"
<"4 view the length of the linked list./N"
<"5 quit./N" <Endl;

}

Void print (node * head ){
Node * No = head-> next;
Cout <"current number of linked lists:" <Endl;
While (no-> next! = NULL ){
Cout <no-> num <"--> ";
No = No-> next;
}
Cout <no-> num <Endl;
}
Int isempty (node * head ){
If (Head-> next = NULL)
Return 1;
Else return 0;

}
Int listlength (node * head ){
Int Len = 0;
Node * n = head;
While (n-> next! = NULL)
{
N = N-> next;
Len ++;
}
Return Len;
}

Int main ()
{
System ("color 4E ");
Node * head = new node;
Head-> next = NULL;
Int choice;
Int item, Item1;
Instructions ();/* display menu function */
Cout <"select :";
Cin> choice;
While (choice! = 5)
{
Switch (choice)
{
Case 1:
Cout <"Enter the number of linked lists you want to insert :";
Cin> item;
If (insert (Head, item ))
{
Cout <item <"has been successfully inserted to the linked list in sequence! "<Endl;
}
Print (head );
Break;
Case 2:
If (! Isempty (head )){
Cout <"Enter the quantity you want to delete ";
Cin> item;
If (delete _ (Head, item )){
Cout <item <"deleted! "<Endl;
Print (head );
}
Else {
Cout <item <"not found! "<Endl;
Print (head );
}
}
Else cout <"The linked list is empty. Please enter the number of linked lists before searching! "<Endl;
Break;
Case 3:
If (! Isempty (head )){
Cout <"Enter character to be found :";
Cin> Item1;
If (findlist (Head, Item1 ))
Cout <"congratulations, search successful! "<Endl;
Else cout <Item1 <"not found! "<Endl;
}
Else cout <"The linked list is empty." <Endl;
Break;
Case 4:
Cout <"the length of the linked list is:" <listlength (head) <Endl;
Break;
Case 5:
Cout <"stops running! ";
Exit (0 );
Break;

Default:
Cout <"invalid choice." <Endl;
Instructions ();
}
Cout <"select :";
Cin> choice;
}
Cout <"stop running." <Endl;
Return 0;
}

It took some time to write, and I always felt rewarded! But you don't know what you get. My data structure is still to be learned! Come on!

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.