Linked list learning: static linked list

The concept of linked list:

The linked list is composed of a series of nodes. Each node includes two fields. One is a data field to store data, and the other is a pointer field to store the address of the next node. The linked list is non-contiguous in memory.
For example:

struct LinkNode
{
    
    
		int age;//定义数据域
		struct LinkNode*next;//定义指针节点,指向下一个节点首地址
};

Inserting and deleting elements in the linked list at the specified position does not need to move the elements, only the pointer needs to be modified, while the array deletion and adding elements need to move the following elements. Compared with the array, the linked list has more pointer domain space overhead. The first node of the linked list is equivalent to getting the entire linked list

The classification of the linked list:

The classification of the linked list: static linked list, dynamic linked
list, singly linked list, doubly linked list, circular linked list, one-way circular linked list, two-way circular linked list

The difference between linked list and array:

Array: Allocate a piece of contiguous storage space at a time.
Advantages: High efficiency of random access to elements.
Disadvantages: (1) Need to allocate a piece of contiguous storage area (large area, the allocation may fail)
(2) Deleting and inserting an element is inefficient
linked list : There is no need to allocate a contiguous memory area at one time, only the storage area of ​​n fast nodes is allocated, and the relationship is established through pointers
Advantages: (1) No contiguous storage area is required
(2) High efficiency in deleting and inserting an element
Disadvantages: Random Inefficient access to elements

Simple use of static linked list:

#include<stdio.h>
//链表结点定义
struct LinkNode
{
    
    
	int data;//定义链表数据
	struct LinkNode*next;//定义链表节点
};
void test()
{
    
    
	struct LinkNode node1 = {
    
     10,NULL };
	struct LinkNode node2 = {
    
     20,NULL };
	struct LinkNode node3 = {
    
     30,NULL };
	struct LinkNode node4 = {
    
     40,NULL };
	struct LinkNode node5 = {
    
     50,NULL };
	struct LinkNode node6 = {
    
     60,NULL };
	node1.next = &node2;
	node2.next = &node3;
	node3.next = &node4;
	node4.next = &node5;
	node5.next = &node6;
	//如何遍历链表:定义一个辅助指针
	struct LinkNode* pCurrent = &node1;
	while (pCurrent != NULL)
	{
    
    
		//打印数据
		printf("%d ", pCurrent->data);
		//指针移动到下一个元素的首地址
		pCurrent = pCurrent->next;
	}
	
}
int main()
{
    
    
	test();
	return 0;
}

(This article is a personal learning experience, if there are any mistakes, please point out more)

Guess you like

Origin blog.csdn.net/weixin_51476766/article/details/111403508