The first time to write a blog, if there are any mistakes, please correct...
This afternoon, I inverted the linked list of c language. I feel that the linked list is a small database. It is very convenient to store and manage user information of simple small programs.
For convenience, take student information as an example:
The linked list uses a dynamic allocation method to allocate memory space for a structure. Each time a space is allocated to store a student's data, we can call it a node. How many students should apply for the allocation of memory space, that is to say, how many nodes should be established. Of course, the above work can also be accomplished with structured arrays, but if the number of students can not be accurately grasped in advance, the size of the array can not be determined. Moreover, the space occupied by this element can not be released from the array after the students repeat or drop out of school. Dynamic storage can solve these problems well. One student assigns a node without having to determine the exact number of students in advance. If a student drops out of school, the node can be deleted and the storage space occupied by the node can be released. Consequently, precious memory resources are saved. On the other hand, the method of using arrays must occupy a contiguous memory area. When using dynamic allocation, each node can be discontinuous (within the node is continuous). The connection between nodes can be achieved by pointers. That is to define a member item in the node structure to store the first address of the next node. The member used to store the address is often called the pointer field.
The first address of the second node can be stored in the pointer field of the first node, and the first address of the third node can be stored in the pointer field of the second node, so that the first address of the third node can be connected in series until the last node. Because the last node has no subsequent node connection, its pointer field can be assigned to 0. Such a connection is called a linked list in the data structure.
Here is a simple example, including the creation of linked lists.
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof (struct stu)
/*
head For (pseudo) header pointers, pbefore is a pointer variable pointing to the previous node of two adjacent nodes. pafter is the pointer variable of the latter node.
*/
//Define the number of students
int n=4;
//Define linked list structure
typedef struct stu{
int num;
int age;
struct stu *next;
}TYPE;
/*Define deletion functions: delete selected nodes*/
void delete(TYPE *head,int number)
{
int i;
TYPE *list=head;
TYPE *tp=NULL;
//Here we loop n+1 because there are n+1 nodes in total, including the header node.
for(i=0;i<(n+1);i++){
printf("the next:%ld\n",(long)list->next);
if(list->num==number){
if(list->next==0){
tp->next=0;
break;
}else{
tp->next=list->next;
break;
}
}
tp=list;// The temporary pointer points to the previous node
list=list->next; //Continue to determine the next node
}
}
/*Define the query function: Display all information in the linked list*/
void query(TYPE *head)
{
TYPE *list=head;
while(list)
{
printf("%d,%d\n",list->num,list->age);
printf("the next:%ld\n",(long)list->next);
list=list->next;
}
}
int main()
{
//This is the true header node defined
struct stu *head;
head->num=0;
head->age=0;
struct stu *thead,*pbefore,*pafter;
int i;
for(i=0;i<n;i++){
pafter=(TYPE*) malloc(LEN); //The pafter defined is also equivalent to a temporary node
printf("input Number and Age\n");
scanf("%d%d",&pafter->num,&pafter->age);
if(i==0) pbefore=thead=pafter; //In the first loop, the header node is set directly
else pbefore->next=pafter;
//The pbefore s here are actually given after the last round of loops, so they are "the former node"
//Note: The change here is not pbefore, but data in memory pointed to by pbefore.
pafter->next=NULL; // Clear the last pointer field and assign it to pbefore, the front node
pbefore=pafter;// Here pbefore points to new memory, the next node
// To emphasize, pbefore is just a pointer
}
head->next=thead;
query(head);
int number;
printf("you want to delete:");
scanf("%d",&number);
delete(head,number);
query(head);
return 0;
}
The above program contains a linked list with n+1 nodes (n student nodes and 1 header node, because the header node has no data domain). If no header node is defined in advance, the first student's node is the header node, which results in the header node can not be deleted.
As can be seen from the figure above, if the header node is deleted directly, a segment error will occur.
Now we add a node at the end of the list.
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof (struct stu)
typedef struct stu{
int num;
int age;
struct stu *next;
}TYPE;
int n=4;
/*Added Node Function: Returns a new Tail Node Address*/
TYPE *add(TYPE* tail)
{
TYPE *new;
new=(TYPE*) malloc(LEN);
printf("input the new num and age\n");
scanf("%d%d",&new->num,&new->age);
tail->next=new;
printf("tail:%ld",(long)tail);
new->next=NULL;//Without this sentence, the new node data will not be displayed.
tail=new;
printf("new tail:%ld",(long)tail);
return tail;
}
/*Delete Node Functions*/
void delete(TYPE *head,TYPE* tail,int number)
{
int i;
TYPE *list=head;
TYPE *tp=NULL;
for(i=0;i<(n+1);i++){
// printf("the next:%ld\n",(long)list->next);
if(list->num==number){
if(list->next==0){
tp->next=0;
tail=tp;//If the tail node is deleted, the tail node is updated
break;
}else{
tp->next=list->next;
break;
}
}
tp=list;// The temporary pointer points to the previous node
list=list->next; //Continue to determine the next node
}
}
/*Query function: Display all node data*/
void query(TYPE *head)
{
TYPE *list=head;
while(list)
{
printf("%d,%d\n",list->num,list->age);
//printf("the next:%ld\n",(long)list->next);
list=list->next;
}
}
int main()
{
//Initialize header and tail nodes
struct stu *head;
struct stu *tail;
head->num=0;
head->age=0;
struct stu *thead,*pbefore,*pafter;
int i;
for(i=0;i<n;i++){
pafter=(TYPE*) malloc(LEN);
printf("input Number and Age\n");
scanf("%d%d",&pafter->num,&pafter->age);
if(i==0) pbefore=thead=pafter;
else pbefore->next=pafter;
pafter->next=NULL;
pbefore=pafter;
}
tail=pafter;
printf(" tail:%ld",(long)tail);
head->next=thead;
query(head);
int number;
printf("you want to delete:");
scanf("%d",&number);
delete(head,tail,number);
query(head);
tail=add(tail);
query(head);
tail=add(tail);
query(head);
return 0;
}
Here we need to define the tail node, and update the tail node after adding, so that we can insert the node after the list again. Note: Don't forget to allocate memory to the new node, otherwise it will be segmented incorrectly, because you assign value to the structure that has not allocated memory.
new=(TYPE*) malloc(LEN);
Also, don't forget to set the pointer field of the new tail node to NULL.
new->next=NULL;
The above is an example of adding two nodes. The results are as follows.
As you can see, the additional nodes are already displayed.