dynamic chain

Don't run, you 2021-11-25 18:02:23

Define the structure of the linked list :

 Dynamic list _ List insertion

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

 Dynamic list _ Insert and delete combination _02

Write a linked list :

1. Dynamically create linked lists :

 Dynamic list _ Linked list _03

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 :

 Dynamic list _ List delete _04

Insert :

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

 Dynamic list _ List insertion _05

3. step 1 and 2 combination :

 Dynamic list _ Linked list _06


Now enter the insertion of linked list :

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

p->next=head;  head=p;( When head It's empty time , Inserted p For the first node )

Insert the new node after the footer node :

 Dynamic list _ List insertion _07

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 :

 Dynamic list _ Linked list _08

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

Delete a node :

1. Delete header node :

q=head;  head=head->next;  free(q); --free(q) To release q Space

2. Delete the tail node :

 Dynamic list _ node _09

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 :

 Dynamic list _ List delete _10

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;

head = NULL;

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;

  if (head == NULL)

  {

   head = p;

   q = p;

  }

  else

  {

   q->next = p;

   q = p;

  }

}

return head;

}

//   Traversing the linked list

void func(struct student* head)

{

struct student* p;

p = head;

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;

q1 = head;

q2 = NULL;

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

{

 q2 = q1;

 q1 = q1->next;

}

if (q2 == NULL)  // Header insert

{

 p->next = q1;

 head = p;

}

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;

}

return head;

}

// Node delete

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

{

struct student* q1, * q2;

q1 = head;

q2 = NULL;

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

{

 q2 = q1;

 q1 = q1->next;

}

if (q2 == NULL)

 head = q1->next;

else if (q1 != NULL)

 q2->next = q1->next;

free(q1);

return head;

}

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 .


Please bring the original link to reprint ,thank
Similar articles

2021-11-25

2021-11-25

2021-11-25

2021-11-25