Home > Common Problem > body text

How to understand linked lists in c language

清浅
Release: 2020-09-10 13:17:13
Original
19762 people have browsed it

C language linked list is a data structure in which storage can be allocated dynamically, node data types can be defined, and node additions, deletions, modifications, etc. can be implemented.

Linked list is a common basic data structure, and structure pointers are fully utilized here. The linked list can dynamically allocate storage. In other words, the linked list is an extremely powerful array. It can define multiple data types in nodes, and nodes can be added, deleted, and inserted as needed. Next, I will introduce the linked list in C language to you in detail in the article. I hope it will be helpful to you.

How to understand linked lists in c language

【Recommended course: C Language Tutorial

The linked list has a head pointer, generally represented by head, which stores an address. The nodes in the linked list are divided into two categories, head nodes and general nodes. The head node has no data field. Each node in the linked list is divided into two parts, a data field and a pointer field. A linked list is like a chain. The head points to the first element: the first element points to the second element;... until the last element, the element no longer points to other elements. It is called the "tail of the list". The address part stores a "NULL" (meaning "empty address"), and the linked list ends here. As a linked list with powerful functions, there are of course many operations on it, such as: linked list creation, modification, deletion, insertion, output, sorting, reverse order, clearing elements of the linked list, finding the length of the linked list, etc. Beginners to learning linked lists usually start with a one-way linked list

Empty linked list

--->NULL
head
Copy after login

A linked list with n nodes

 ---->[p1]---->[p2]...---->[pn]---->[NULL]
head   p1->next  p2->next   pn->next
Copy after login

Create linked list

Generally, we use typedef struct to create linked lists, because when defining structure variables, we can directly use LinkList *a; to define structure type variables.

typedef struct student{
int score;
struct student *next;
} LinkList;
Copy after login

Initialize a linked list, n is the number of linked list nodes

LinkList *creat(int n){
LinkList *head, *node, *end;//定义头节点,普通节点,尾部节点;
head = (LinkList*)malloc(sizeof(LinkList));//分配地址
end = head;         //若是空链表则头尾节点一样
for (int i = 0; i < n; i++) {
node = (LinkList*)malloc(sizeof(LinkList));
scanf("%d", &node->score);
end->next = node;
end = node;
}
end->next = NULL;//结束创建
return head;
}
Copy after login

Modify the linked list node value

Modify The linked list node value is very simple. Below is a function that passes in a linked list and the node to be modified to modify the value.

void change(LinkList *list,int n) {//n为第n个节点
LinkList *t = list;
int i = 0;
while (i < n && t != NULL) {
t = t->next;
i++;
}
if (t != NULL) {
puts("输入要修改的值");
scanf("%d", &t->score);
}
else {
puts("节点不存在");
}
}
Copy after login

Deleting a linked list node

Deleting an element of a linked list means passing the pointer field of the previous node to the next node past the node to be deleted. That is: p->next = q->next; and then release the space of node q, that is, free(q);

How to understand linked lists in c language##

void delet(LinkList *list, int n) {
	LinkList *t = list, *in;
	int i = 0;
	while (i < n && t != NULL) {
		in = t;
		t = t->next;
		i++;
	}
	if (t != NULL) {
		in->next = t->next;
		free(t);
	}
	else {
		puts("节点不存在");
	}
}
Copy after login

Insert linked list node

How to understand linked lists in c language

We can see that inserting a node is to use the pointer field of the node before inserting to link the data field of the inserted node, and then insert the node The pointer field link is inserted into the data field of the subsequent node. According to the figure, inserting a node is: e->next = head->next; head->next = e;

Adding a linked list node uses two structure pointers and an int data.

void insert(LinkList *list, int n) {
	LinkList *t = list, *in;
	int i = 0;
	while (i < n && t != NULL) {
		t = t->next;
		i++;
	}
	if (t != NULL) {
		in = (LinkList*)malloc(sizeof(LinkList));
		puts("输入要插入的值");
		scanf("%d", &in->score);
		in->next = t->next;//填充in节点的指针域,也就是说把in的指针域指向t的下一个节点
		t->next = in;//填充t节点的指针域,把t的指针域重新指向in
	}
	else {
		puts("节点不存在");
	}
}
Copy after login

Output linked list

The output linked list is very simple, just output while traversing it

        while (h->next != NULL) {
		h = h->next;
		printf("%d  ", h->score);
	}
Copy after login
Summary: The above is the entire content of this article ,I hope to be helpful.

The above is the detailed content of How to understand linked lists in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template