Basic operations on linked lists

Source: Internet
Author: User

Basic operations on the linked list:

1 create a linked list Linknode * createLink (int n)

2. Calculate the length of the linked list int lenthLink (Linknode * head)

3 insert Linknode * insertNode (Linknode * head, int data, int key) into the linked list)

4. Delete the Linknode * deleteNode (Linknode * head, int num) node of the linked list );

5. Sort the Linknode * sortLink (Linknode * head) of the linked list );
6. Reverse Linknode * invertLink (Linknode * head) of the linked list );

7. Search for Linknode * locateN (Linknode * head, int num) for a node in the linked list );
8. Output linked table void printLink (Linknode * head)


1. Create a linked list

Linknode * createLink (int n)
{
Int xValue;
Linknode * head, * p, * pre; // establish a connection relationship between pre and p.

Cout <"enter 1st numbers :";
Cin> xValue;
P = (Linknode *) malloc (sizeof (Linknode ));
P-> data = xValue;
P-> next = NULL;
Head = p;
Pre = p;

For (int I = 1; I <n; I ++)
{
Cout <"Enter the" <I + 1 <"digit :";
Cin> xValue;
P = (Linknode *) malloc (sizeof (Linknode ));
P-> data = xValue;
P-> next = NULL;

Pre-> next = p;
Pre = p;
}

PrintLink (head );
Return head;
}


2. Calculate the length of the linked list

Int lenthLink (Linknode * head)
{
Assert (head );
Int num = 0;
Linknode * p = head;

While (p)
{
Num ++;
P = p-> next;
}

Return num;
}


3. Insert a linked list

Linknode * insertNode (Linknode * head, int data, int key)
{
Assert (head! = NULL );
Linknode * s;
S = (Linknode *) malloc (sizeof (Linknode ));
Assert (s! = NULL );
S-> data = data;
S-> next = NULL;

Linknode * p = head;
// Two results: 1. Insert execution is found.) 2. Insert execution is not found)
While (p & p-> data! = Key)
{
P = p-> next;
}
If (p! = NULL & p-> data = key)
{
S-> next = p-> next;
P-> next = s;
Cout <"inserted successfully !!! "<Endl;
}
Else
{
Cout <"insertion failed !!! "<Endl;
}

PrintLink (head );
Return head;
}


4. Delete the linked list Node

Linknode * deleteNode (Linknode * head, int num)
{
Assert (head! = NULL );
Linknode * p = head;
Linknode * pre = head;
Int n = 1; // n = 0 is incorrect.

While (p & n <num)
{
N ++;
Pre = p;
P = p-> next;
}
If (n! = Num)
{
Cout <"deletion failed !!! "<Endl;
}
Else
{
Pre-> next = p-> next;
Free (p );
P = NULL;
Cout <"deleted successfully !!! "<Endl;
PrintLink (head );
}
Return head;
}


5. Sort linked lists

// For sorting, you only need to change linknode-> data, and the link relationship is not required.
Linknode * sortLink (Linknode * head)
{
Assert (head! = NULL );
// Linknode * p = head;
Int tmp;
For (Linknode * p1 = head; p1! = NULL; p1 = p1-> next)
{
For (Linknode * p2 = p1-> next; p2! = NULL; p2 = p2-> next)
{
// Exchange sorting is actually a Bubble sorting)
If (p1-> data> p2-> data)
{
Tmp = p1-> data;
P1-> data = p2-> data;
P2-> data = tmp;
}
}
}

PrintLink (head );
Return head;
}


6. Reverse Order of the linked list

Linknode * invertLink (Linknode * head)
{
Assert (head! = NULL );
Linknode * p, * pre, * tmp; // establish a connection relationship between pre and p. q maintains the next moving node of p.
P = head;
Tmp = head;
Pre = NULL;

While (p! = NULL)
{
Tmp = p-> next;
P-> next = pre; // 1 establish a connection
Pre = p; // 2 pre and p) Move to the back
P = tmp;
}

Head = pre;
PrintLink (head );
Return head;
}


The following is a recursive method:

Linknode * invertLink_Recursive (Linknode * head) {Linknode * new_head = head; if (head = NULL | head-> next = NULL) return head; new_head = invertLink_Recursive (head-> next); cout <"output" 



The value of the output node in reverse order.

// 1 exit condition 2 recursion 3 final output void printInverseLink (Linknode * head) {if (head = NULL) return; // cout 


7. Search for a node in the linked list

Linknode * locateN (Linknode * head, int num)
{
Assert (head! = NULL );
Linknode * p = head;
Int n = 1; // int n = 0 is incorrect (take this into consideration)

While (p! = NULL & n <num)
{
N ++;
P = p-> next;
}
If (n = num)
{
Cout <"search successful !!! ";
Cout <"the value of this node is:" <p-> data <endl;
Return p;
}
Else
{
Cout <"failed to search !!! "<Endl;
Return NULL;
}
}

8. Output linked list

Void printLink (Linknode * head)
{
Linknode * pNode = head;

Cout <"the data in this linked list is:" <endl;
While (pNode)
{
Cout <pNode-> data <endl;
PNode = pNode-> next;
}
}



The test code is as follows:

Linknode * theLink;
Int numNode;
Cout <"Enter the number of created linked lists ";
Cin> numNode;
TheLink = createLink (numNode );
Cout <"the length of the linked list is:" <lenthLink (theLink) <endl;

// TheLink = insertNode (theLink, 6, 2 );
// TheLink = deleteNode (theLink, 3 );
// TheLink = sortLink (theLink );
// TheLink = invertLink (theLink );
Linknode * p = locateN (theLink, 3 );

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.