What is a linked list?
A linked list is an important data structure, and its greatest advantage is that it can be dynamically allocated for storage. Linked list has a unidirectional linked list, a doubly linked list, a circular link list. For C, here we only discuss one-way linked list.
We know that memory is made up of stacks and heaps. The stack space is controlled by the operating system and the compilation system, for example, we define int A, and this is the one that opens up the memory unit in the stack. The heap space allows the user to provide virtual space in the heap
There is no variable name in this statement, only through the address to find things stored in memory.
Since it is dynamic memory allocation, there are, of course, special methods for dynamic allocation. is implemented as a function in C.
1.malloc function
Function prototype: void *malloc (unsigned int size)
The function is to create a contiguous space of size bytes in the dynamic storage area of the training, returning the first byte address of the allocated area.
As you can see, the function return value is a void pointer, note that the void pointer is not a pointer to any type of data, but rather that it does not point to any type of data, but simply provides an address.
So, you want this pointer to point to the INT data, the explicit type conversion (coercion type conversion), which is preceded by (int *). In general, implicit type conversions can be done automatically if not added.
2.calloc function
function prototype: void *calloc (unsigned n,unsigned size)
Function: Opens up n continuous space of size. Typically used to hold an array.
3.realloc function
prototype: void *realloc (void *p,unsigned int size)
role: Used to reallocate the size of the dynamic space that has been allocated.
4.free function
prototype: void free (void *p)
Role: Release the allocated dynamic space.
List of words returned:
A linked list is made up of a number of nodes. can be freely deleted, added. The pointer to the first node is called the head pointer, which is the only indicator of the linked list. Each node contains two parts, one for the data, and the other for the address of the next node. Through the address of the next node, the whole linked list is established.
How to build a linked list?
It is certainly fitting to build a linked list with a structure.
For example: Create a static linked list now.
struct S
{
int A;
struct S *next;
}S1,S2,S3;
struct S *head;
S1.a=1; s2.a=2; S3.a=3;
head=&s1;
s1.next=&s2;
s2.next=&s3;
S3.next=null; Note that the address where the most nodes are stored is null.
In this way, we set up a static linked list with three nodes.
How to build a dynamic linked list? And how to add, delete nodes, further, how to make the list is orderly, and delete add operation to make it still orderly?
the creation of a dynamic linked list is the ability to add nodes as needed at any time. Look at the code:
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof (struct S)
struct S
{
int A;
struct S *next;
};
int n;//global variable N, record number of nodes. Unassigned Initial value defaults to 0;
struct S *creat_autolist (void)//function to create a dynamic linked list
{
struct S *head;//head pointer;
struct S *p0,*p1;//p0 is the current node;
p0=p1= (struct s*) malloc (LEN);//Use the malloc function to open up a node space;
scanf ("%d", &p0->a);//input data;
Head=null;
while (p0->a!=0)//loop, the input A is not 0 to continue to enter;
{
n++;
if (n==1) head=p0;//is the first node;
Else p1=p0->next;
p1=p0;//Let P1 also point to the node that the P0 points to
p0= (struct s*) malloc (LEN);//In the opening up of a node;
scanf ("%d", &p0->a);
}
the pointer portion of the p1->next=null;//tail node is NULL
return (head);
}
void Main ()
{
struct S *head,*pt;
head=creat ();
Pt=head;
/*
This is how the list is exported.
*/
if (head!=null)
{
Do
{
printf ("%d\n", pt->a);
pt=pt->next;
}while (Pt!=null);
}
}
At this point, the creation and output of the dynamic linked list has been implemented.
How do I delete a node? is actually the point of changing the address in the two nodes before and after the node to be deleted.
How do I add a node? Actually also change points.
Make it orderly?
So, we're going to do a big-size-comparison process. However, there are three cases to consider at the top of the table, in the table.
A small chestnut is given below:
The nodes of the linked list are added and ordered.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof (struct Student)
struct Student
{
Long num;
Double score;
struct Student *next;
};
int n=0;
void print (struct Student *head)
{
struct Student *pt;
Pt=head;
printf ("There is%d records:\n");
if (head!=null)
{
do
{
printf ("%ld,%5.1lf", Pt->num,pt->score);
Pt=pt->next;
}while (pt!=null);
}
}
struct Student *creat ()
{
struct Student *head,*p0,*p1;
Head=null;
printf ("*****creat list*****");
printf ("Please input record (0,0 for exit) \ n");
p0= (struct Student *) malloc (LEN);
scanf ("%ld,%lf", &p0->num,&p0->score);
while (p0->num!=0)
{
Head=insert (HEAD,P0);
p0= (struct Student *) malloc (LEN);
scanf ("%ld,%lf", &p0->num,&p0->score);
}
return (head);
}
struct Student *insert (struct Student *head,struct Student *stu)
{
struct Student *p0,*p1,*p2;
P0=stu;
if (head==null)
{
head=p0;
p0->next=null;
}
Else
{
P1=head;
While (p0->num>p1->num&&p1->next!=null)//This is a traversal
{
P2=p1;
p1=p1->next;
}
if (p0->num<=p1->num)
{
if (HEAD==P1)
{
head=p0;
p0->next=p1;
}
Else
{
p2.next=p0;
P0.next=p1;
}
}
Else
{
p1.next=p0;
P0.next=null;
}
}
n++;
return (head);
}
This is only the simplest form and application of the list, and the data structure will also be delved into.
Dynamic memory allocation and linked list