A linked list is a common and important data structure that dynamically allocates memory.
When using arrays to store data, you must define a fixed length (that is, the number of elements) in advance. However, if it is difficult to determine the number of elements in advance, you must define an array large enough, to ensure success.
Undoubtedly, this will cause a waste of memory. However, the linked list does not have this disadvantage. It can dynamically open up memory units as needed.
Each element in the linked list can not be stored continuously in the memory. However, to find an element, you must know its address. In this case, the linked list must have a head pointer ).
Today, I will introduce a series of linked list operations, including creating a linked list, outputting a linked list, deleting a linked list, and inserting a linked list.
This process is presented by student ID and score.
Create a linked list:
# Include "stdlib. h"
# Include "stdio. h"
# Define NULL 0
# Define LEN sizeof (struct student)
Struct student
{
Long num;
Float score;
Struct student * next;
};
Int n;
Struct student * creat (void)
{
Struct student * head;
Struct student * p1, * p2;
N = 0;
P1 = p2 = (struct student *) malloc (LEN );
Scanf ("% ld, % f", & p1-> num, & p1-> score );
Head = NULL;
While (p1-> num! = 0)
{
N = n + 1;
If (n = 1)
Head = p1;
Else
P2-> next = p1;
P2 = p1;
P1 = (struct student *) malloc (LEN );
Scanf ("% ld, % f", & p1-> num, & p1-> score );
}
P2-> next = NULL;
Return (head );
}
Void main ()
{
Creat ();
}
In this way, you can create a linked list,
At this point, you can create a linked list.
Please support Li Mu Space