Insert and delete a single-chain table

Source: Internet
Author: User

Reference: http://www.itxyz.net/3jk/c/2010/0820/11016.html

Recently, I am not too busy. I may be able to organize some things and get some help from my work.

In the special data structure of the linked list, the length of the linked list needs to be set according to the specific situation. When you need to save the data, apply for a storage space from the system and connect the data to the linked list. For the linked list, the data in the table can be connected to the end of the table or the header, or inserted into the table as needed. For data that is no longer needed, delete it from the table and release its occupied space, but it cannot damage the structure of the linked list. The following describes how to insert and delete a linked list.

1. delete a linked list

If you create a single table of Student IDs and names, that is, the node includes student Student IDs, names, and pointers to the next node. The linked list is arranged by student Student IDs. Enter a student's name on the keyboard and delete it from the linked list.

First, define the structure of the linked list:

You can delete a node from a linked list in three cases:Delete the head node of the linked list, the intermediate node of the linked list, and the tail node of the linked list.. If the name of a student is given, the Student name should be searched from start to end in the linked list and compared with the Student name of each node. If the name is the same, the Student name is found successfully. Otherwise, node not found. Because the deleted node may be in the head of the linked list and may cause loss of the head pointer of the linked list, the return value of the function defining the delete node is defined

Returns the pointer of the struct type.

Struct node * delet (Head, pstr)/* use head as the header pointer to delete the node where pstr is located */struct node * head; char * pstr; {struct node * temp, * P; temp = head;/* head pointer of the linked list */If (Head = NULL)/* empty linked list */printf ("\ nlistis null! \ N "); else/* non-empty table */{temp = head; while (strcmp (temp-> STR, pstr )! = 0 & temp-> next! = NULL)/* If the node string is different from the input string and is not at the end of the linked list */{P = temp; temp = tmep-> next;/* track the growth of the linked list, that is, move the pointer behind */} If (strcmp (temp-> STR, pstr) = 0)/* Find the string */{If (temp = head) {/* Header node */printf ("delete string: % s \ n", temp-> Str); Head = head-> next; free (temp ); /* release the deleted node */} else {P-> next = temp-> next;/* Table node */printf ("delete string: % s \ n ", temp-> Str); free (temp) ;}} elseprintf ("\ Nno find string! \ N ");/* the string to be deleted is not found */} return (head );}

2. Insert a linked list

First, define the structure of the linked list:

Struct {int num;/* student ID */Char STR [20];/* name */struct node * Next ;};

In the created single-chain table, there are three scenarios for inserting nodes ,;

The inserted node can beHeader, table, or table tail. Assuming that we create a linked list in the order of student IDs, the inserted nodes are compared with the nodes in the table to find the Insertion Location. Because the inserted node may be in the head of the linked list, the head pointer of the linked list will be modified. Therefore, the return value of the function defining the inserted node is defined as a pointer of the return struct type. The insert function of the node is as follows:

Struct node * insert (Head, pstr, N)/* Insert a node whose student ID is N and whose name is p s t r */struct node * head; /* head pointer of the linked list */char * pstr; int N; {struct node * P1, * P2, * P3; P1 = (struct node *) malloc (sizeof (struct node)/* allocate a new node */strcpy (P1-> STR, pstr ); /* Name string of the write node */P1-> num = N;/* student ID */P2 = head; If (Head = NULL) /* empty table */{head = p1; P1-> next = NULL; /* Insert the header of the new node */} else/* non-empty table */{While (n> P2-> num & p2-> next! = NULL)/* The entered student ID is smaller than the student ID of the node and is not at the end of the table */{P3 = P2; P2 = P2-> next; /* track the growth of linked lists */} If (n <= P2-> num)/* locate the insertion position */If (Head = P2) /* The insert position is in the header */{head = p1; P1-> next = P2;} else {/* The insert position is in the table */P3-> next = p1; p1-> next = P2;} else {/* Insert position at the end of the table */P2-> next = p1; P1-> next = NULL;} return (head ); /* return the head pointer of the linked list */}

Instance:

Create a single-chain table containing student ID and Name node. There are any number of nodes in the table. The table is in the order of student numbers. The number of lower student numbers is in the front, the number of higher student numbers is in the back, and the input name is blank. In this linked list, you must delete a node with a given name and insert a node with a given student ID and name.

# Include <stdlib. h> # include <malloc. h> struct node/* node data structure */{int num; char STR [20]; struct node * Next ;}; /****************************/main () {/* function declaration */struct node * creat (); struct node * insert (); struct node * delet (); void print (); struct node * head; char STR [20]; int N; head = NULL;/* short table */head = creat (head ); /* call the function to create a head-oriented linked list */print (head);/* call the function output node */printf ("\ N input inserted n Um, name: \ n "); gets (STR);/* enter the student ID */N = atoi (STR); gets (STR ); /* enter the name */head = insert (Head, STR, n);/* Insert node to the linked list */print (head); printf ("\ N input deleted Name: \ n "); gets (STR);/* enter the deleted name */head = delet (Head, STR);/* use the function to delete nodes */print (head ); /* call the function output node */return ;} ********** **/struct node * creat (struct node * head) {char temp [30]; struct node * PL, * P2; PL = P2 = (struct Node *) malloc (sizeof (struct node); printf ("input num, name: \ n"); printf ("Exit: Double times enter! \ N "); gets (temp); gets (pl-> Str); PL-> num = atoi (temp); PL-> next = NULL; while (strlen (pl-> Str)> 0) {If (Head = NULL) // if the chain is null head = pl; // else P2-> next = pl; // set the P2-> next nullp2 = pl; // PL = (struct node *) malloc (sizeof (struct node )); printf ("input num, name: \ n"); printf ("Exit: Double times enter! \ N "); gets (temp); gets (pl-> Str); PL-> num = atoi (temp); PL-> next = NULL;} return head ;} /******************* // ************ insert a node ***** * ***/struct node * insert (Head, pstr, n) struct node * head; char * pstr; int N; {struct node * PL, * P2, * P3; PL = (struct node *) malloc (sizeof (struct node); strcpy (pl-> STR, pstr); PL-> num = N; P2 = head; If (Head = NULL) {head = pl; PL-> next = NULL;} else {W Hile (n> P2-> num & p2-> next! = NULL) {P3 = P2; P2 = P2-> next;} If (n <= P2-> num) if (Head = P2) {head = pl; pl-> next = P2;} else {P3-> next = pl; PL-> next = P2;} else {P2-> next = pl; pl-> next = NULL;} return (head );} /************************* // ******* delete a node ***** * ******/struct node * delet (Head, pstr) struct node * head; char * pstr; {struct node * temp, * P; temp = head; If (Head = NULL) printf ("\ NLIST is null! \ N "); else {temp = head; while (strcmp (temp-> STR, pstr )! = 0 & temp-> next! = NULL) {P = temp; temp = temp-> next;} If (strcmp (temp-> STR, pstr) = 0) {If (temp = head) {head = head-> next; free (temp);} else {P-> next = temp-> next; printf ("delete string: % s \ n ", temp-> Str); free (temp) ;}} else printf ("\ Nno find string! \ N ") ;}return (head );} /**********************************//*** * ******* output of each node in the linked list *********/void print (struct node * head) {struct node * temp; temp = head; printf ("\ N output strings: \ n"); While (temp! = NULL) {printf ("\ n % d ----- % s \ n", temp-> num, temp-> Str); temp = temp-> next;} return ;}

Running result:

root@android-virtual-machine:/uniteq_smb/test# ./test1input num, name: exit:double times Enter!11aainput num, name: exit:double times Enter!12bbinput num, name: exit:double times Enter! output strings:11-----aa12-----bb input inserted num,name:13cc output strings:11-----aa12-----bb13-----cc input deleted name:aa output strings:12-----bb13-----cc

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.