Linked list creation, deletion, printing function (novice)

// complete code

#include<stdio.h>
#include<stdlib.h>
typedef struct lianbiao
{
    int score;
    struct lianbiao*next;
}Linklist;

//create node 

Linklist*creat(int n)
{     Linklist *head,*node,*end;     head=(Linklist*)malloc(sizeof(Linklist));     end=head;//Make the tail node equal to the head node      printf("please Enter the value to create:\n");     for(int i=0;i<n;i++)     {         node=(Linklist*)malloc(sizeof(Linklist));         scanf("%d",&node->score );         end->next=node;//Point the pointer field of the tail/head node to the normal node just created.          end=node;//Make the end node equal to the current normal node to realize the connection to the next node.      }     end->next=NULL;//After all nodes are created, let the pointer field of the last node be empty.      return head;//return head node (address)  }














 

//delete (in order to make it easier and easier to understand, I didn't write empty judgment here, and there is no free)
void delete(Linklist*list,int score,int n)
{     Linklist *t=list->next,*m= list,*p;//Define two pointers, the address of the next node pointed to by the pointer field of the head node carried by one. The address of the head node carried by      for(int i=0;i<n;i++)     {         if(t->score==score)         {             m->next=t->next;         }         else         m=t;/ /Move m pointer          t=t->next;//Move t pointer      } }

    





           





//Print (mainly understand how to use another pointer to advance (traverse)) 
void print(Linklist*list)
{     Linklist *t=list;     while(t->next!=NULL)     {         printf("%d ", t->next->score);         t=t->next;     } } //main function  int main() {     printf("Please select the number of created nodes (excluding the head node):\n");     int n ;     scanf("%d",&n);      Linklist*list=creat(n);//Create a linked list (here use the list pointer to receive the address of the head node)      printf("Please input the value to be deleted:\n" );      int s;      scanf("%d",&s);      delete(list,s,n);      printf("The value of the current node:\n");     print(list);     return 0;     }





















  • How to create a linked list

#include<stdio.h>

#include<stdlib.h>

typedef struct lianbiao

{

int score;

struct lianbiao*next;

}Linklist;

Explanation:
  First, "struct lianbiao" after typedef is a structure. Many novice comrades don't know how to understand the structure. In fact, it is very simple. You can understand that this is a type.
  For example, if we use int to define a variable a, then a is an integer.
  If char is used, then a is a character type.
  And if we use a structure to define a, then a is a structure type, such as struct lianbiao a. The advantage of this type is that variable a can have multiple types of attributes.
  Second, the typedef here is not too much to explain, if you want to know more, you can search. To help you understand, you can think of it as an "aliased" identifier. For example, when we do not use typedef, we define variable a like this:
    struct lianbiao a; we can find that this definition is too long, so for simplicity, we give struct lianbiao an alias Linklist, and then define a as: Linklist a;


//create node

Linklist*creat(int n)

{

Linklist *head,*node,*end;

head=(Linklist*)malloc(sizeof(Linklist));

end=head;//Make the end node equal to the head node

printf("Please enter the value to be created:\n");

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

{

node=(Linklist*)malloc(sizeof(Linklist));

scanf("%d",&node->score);

end->next=node;//Point the pointer field of the tail/head node to the normal node just created.

end=node;//Make the end node equal to the current normal node to realize the connection to the next node.

}

end->next=NULL;//After all nodes are created, let the pointer field of the last node be empty.

return head;//return head node (address)

}
Difficulties in creating nodes:
1. Linklist*creat(int n)
    can be understood in this way, this is a creat function defined by the type of Linklist*, and the parameter is n. Why is it
    the type of Linklist* instead of the Linklist type? Because what we will return later is the address of the head node, so use
    the Linklist* structure pointer type to receive it.


2. head=(Linklist*)malloc(sizeof(Linklist));
This is dynamic memory allocation, use the head pointer to receive an address whose size is the space size of the structure type Linklist.

3. The purpose of creating head pointer (node), common pointer (node) and tail pointer (node).
(Note: Here is an explanation of why it is also called a pointer and a node, because the node we created needs to be returned as an address, so a pointer is used to receive the address) 1. Create a head
node, because it will be used as a return value, We know the head node, and we can access the entire linked list through it, and the data field (score) of the head node does not need to be assigned.
2. Ordinary node, this is the node we need to assign a value to the data field (score).
3. Tail node, or pointer, we use it to string up the linked list (see code comments for specific steps)

//删除
void delet(Linklist*list,int score,int n)

{

Linklist *t=list->next,*m=list,*p;//Define two pointers, the address of the next node pointed to by the pointer field of the head node carried by one. The address of the head node of the bearer

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

{

if(t->score==score)

{

m->next=t->next;

}

else

m=t;//move m pointer

t=t->next;//Move the t pointer

}

}

Difficulties in the deletion function:
1. How to delete
Here, you can directly point the pointer field of the previous node of the node to be deleted to the next node of the node to be deleted.
Linklist *t=list->next,*m=list;
      m->next=t->next;
to achieve a logical deletion.
(This node space has not actually been deleted, so I said logically delete. If you want to release the space for deleting nodes, you can use free. Comrades who are new to you only need to understand this logic and delete them.) 2. How to make the search

target The pointer of the node advances (or traverses)
m=t;
t=t->next;

//Print (mainly understand how to use another pointer to advance (traverse))

void print(Linklist*list)

{

Linklist *t=list;

while(t->next!=NULL)

{

printf("%d ",t->next->score);

t=t->next;

}

}

I am new to programming, so please correct me if I make any mistakes.

Guess you like

Origin blog.csdn.net/qq_58136559/article/details/125135051