Data structure C Language & amp; gt; 3 basic linked list & amp; gt; Delete nodes of the 3-5 linked list

Source: Internet
Author: User
Tags key string

Duplicate linked list creation, linked list output, linked list node traversal,

Of course, the main character is to delete linked list nodes.

You can delete linked list nodes in three cases:

Case 1: Delete the header node. Just point the linked list structure pointer to the second node.


Case 2: Delete the last node. You only need to point the next pointer of the last and second nodes to NULL.

Case 3: delete the intermediate node. You only need to point the next pointer of the previous node of the intermediate node to the next pointer of the intermediate node.

 

The linked list node traversal is used to find the previous node of the node to be deleted.

The Code is as follows:

 

/* ===================================================== = */
/* Program instance: 3_5.c */
/* Delete a node in the Link string column */
/* ===================================================== = */
# Include <stdlib. h>

Struct llist/* declaration of string Column Structure */
{
Int num;/* mailing Number */
Struct llist * next;/* points to the next tag */
};
Typedef struct llist node;/* define new state */
Typedef node * llink;/* define new state indicator */

/*----------------------------------------*/
/* Print the link string column */
/*----------------------------------------*/
Void printllist (llink ptr)
{
While (ptr! = NULL)/* string-column access loop */
{
Printf ("[% d]", ptr-> num);/* print node information */
Ptr = ptr-> next;/* point to next node */
}
Printf ("");/* line feed */
}

/*----------------------------------------*/
/* Create a key-knot column */
/*----------------------------------------*/
Llink createllist (int * array, int len)
{
Llink head;/* Start indicator of the string column */
Llink ptr, ptr1;
Int I;

/* Create the first node */
Head = (llink) malloc (sizeof (node);/* configure memory */
If (! Head)/* Check metrics */
Return NULL;
Head-> num = array [0];/* create node content */
Head-> next = NULL;/* set the initial value of the indicator */
Ptr = head;/* point ptr to the start of the string column */
For (I = 1; I <len; I ++)/* create other node circuits */
{
Ptr1 = (llink) malloc (sizeof (node ));
If (! Ptr1)
Return NULL;
Ptr1-> num = array [I];/* create node content */
Ptr1-> next = NULL;/* set the initial value of the indicator */
Ptr-> next = ptr1;/* link node */
Ptr = ptr-> next;/* point to next node */
}
Return head;
}

/*----------------------------------------*/
/* Node access to the link string column */
/*----------------------------------------*/
Llink findnode (llink head, int num)
{
Llink ptr;

Ptr = head;/* points to the start of the string column */
While (ptr! = NULL)/* access string column */
{
If (ptr-> num = num)/* search for numbers */
Return ptr;
Ptr = ptr-> next;/* point to next node */
}
Return ptr;
}

/*----------------------------------------*/
/* Delete a node in the key string column */
/*----------------------------------------*/
Llink deletenode (llink head, llink ptr)
{
Llink previous;/* point to the previous node */

If (ptr = head)/* whether it is the start of a string column */
/* First case: Delete the first node */
Return head-> next;/* return the second vertex indicator */
Else
{
Previous = head;
While (previous-> next! = Ptr)/* Find the front node of the node ptr */
Previous = previous-> next;

If (ptr-> next = NULL)/* whether the end of the string column */
/* Case 2: Delete the last node */
Previous-> next = NULL;/* Last node */
Else
/* Case 3: delete an intermediate node */
Previous-> next = ptr-> next;/* intermediate node */
}
Return head;
}

/*----------------------------------------*/
/* Main program: Find the mailing number and delete it .*/
/*----------------------------------------*/
Void main ()
{
Int llist1 [6] = {1, 2, 3, 4, 5, 6};/* array content */
Llink head;/* points to the start of the string column */
Llink ptr;
Int num;/* mailing number variable */

Head = createllist (llist1, 6);/* Create a string column */
If (! Head)
{
Printf ("memory configuration failed! ");
Exit (1 );
}
Printf ("original linked list :");
Printllist (head);/* print the original string column */
While (1)
{
Printf ("Enter the mailing number to delete => ");
Scanf ("% d", & num);/* read the mailing Number */
If (num! =-1)
{
Ptr = findnode (head, num);/* search for the mailing Number */
If (! Ptr)/* Whether to find */
Printf ("not found ");
Else
{
Head = deletenode (head, ptr);/* Delete this node */
Printf ("deleted linked list :");
Printllist (head);/* print the deleted string column */
}
}
Else
Exit (1);/* end and exit */
}
}

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.