# dynamic chain

2021-11-25 18:02:23

Define the structure of the linked list ：

Each element in the linked list is called a node , A node contains a data field and a pointer field .

1. Dynamically create linked lists ：

The tail node has no subsequent nodes , Therefore, the pointer field is generally set to NULL, Indicates the end condition of linked list operation

2. Print out the data in the linked list ：

Insert ：

1 Create table for sequence , Create a table in reverse order as follows ：

3. step 1 and 2 combination ：

Now enter the insertion of linked list ：

Insert... In the header node of the new node ：

Insert the new node after the footer node ：

summary ： Find the footer node first , Then directly use the pointer at the end of the table to point to insert p,p For the new tail node

Insert at the specified position ：

summary ： Insert the node at the specified location , First find a condition to move , Then according to the judgment p Insert

Delete a node ：

2. Delete the tail node ：

summary ： Find the tail node first , Then put the following q2 Pointing empty , Indicates the end of the linked list , Release q1.

3. Delete the node at the specified location ：

summary ： First find a judgment condition to move , Then judge to delete a node .

practice ： First create a student information table , The table is sorted in descending order according to students' grades , Insert the new student's information into the table , Keep the table in order , And delete the student information of the specified student number .

#include<stdio.h>

#include<string.h>

#include<malloc.h>

struct student

{

int num;

char name[20];

float score;

struct student* next;

};

// Create a list

struct student* fun(int n)  // establish n Nodes

{

struct student* head, * p, * q;

q = NULL;

int i;

for (i = 0; i < n; i++)

{

p = (struct student*)malloc(sizeof(struct student));

printf(" Enter the student id , fraction ：");

scanf("%d,%f",& p->num, &p->score);

rewind(stdin);

printf(" Enter a name ：");

gets(p->name);

p->next = NULL;

{

q = p;

}

else

{

q->next = p;

q = p;

}

}

}

{

struct student* p;

while (p != NULL)

{

printf(" Output student number , full name , fraction ：%d,%s,%.2f\n", p->num, p->name, p->score);

p = p->next;

}

}

// Node insertion

struct student* fun1(struct student* head, struct student* p)

{

struct student* q1, * q2;

q2 = NULL;

while (q1 != NULL && q1->score >= p->score)

{

q2 = q1;

q1 = q1->next;

}

if (q2 == NULL)  // Header insert

{

p->next = q1;

}

else if (q1 == NULL)  // Insert at the end of the watch

q2->next = p;

else  // stay q1 Before node ,q2 Insert after

{

p->next = q1;

q2->next = p;

}

}

// Node delete

struct student* fun2(struct student* head, int num)

{

struct student* q1, * q2;

q2 = NULL;

while (q1 != NULL && q1->num != num)

{

q2 = q1;

q1 = q1->next;

}

if (q2 == NULL)

else if (q1 != NULL)

q2->next = q1->next;

free(q1);

}

int main()

{

struct student* p, * p1, * p2 ,*p3,* q;

q = (struct student*)malloc(sizeof(struct student));

q->num = 103;

printf(" Enter the name of this classmate ：");

gets(q->name);

q->score = 85;

q->next = NULL;

printf(" Enter the number of information you want to enter ：");

int n=0,n1=0;

scanf("%d", &n);

p = fun(n);

func(p);

printf(" Print the linked list after insertion ：\n");

p1 = fun1(p,q); // Node insertion

func(p1);

printf(" Enter the student number you want to delete the student information ：");

scanf("%d", &n1);

printf(" Print the deleted linked list ：\n");

p2 = fun2(p,n1); // Delete student ID as n1 Information about my classmates

func(p2);

printf(" Print the linked list deleted after insertion ：\n");

p3 = fun2(p1, n1);

func(p3);

return 0;

}

If there is any wrong , Welcome to correct .

https://chowdera.com/2021/11/20211109095357336h.html