Data structure - linked list (5)

data structure

`


foreword

Next, learn about linked lists, which are used more than arrays.

1. What is a linked list

Concept: Linked list is a non-sequential and non-sequential storage structure in physical storage structure. The logical structure of data elements is realized through pointer links in the linked list.
What do you mean? An array is a continuous storage structure. If you know one, you can find the next one. A linked list is not, but it is linear. How is it realized? The
structure of a single linked list:
insert image description here
it divides a block into two areas, the data field And the pointer field, the pointer field is used to store the address of the next node.
The first node is called the head node, which generally does not save data. The first node starts to save data, which is 2 in the above figure.
There are 8 linked list structures:
(1) One-way, two-way (whether it supports forward access)
(2) Take the lead , No head (whether there is a head node that does not store data at the beginning of the linked list)
(3) Loop, acyclic (whether you can directly access the first node of the linked list through the tail node of the linked list)
Although there are many kinds, However, the two most commonly used structures are as follows:
1. One-way acyclic linked list without a leader.
insert image description here
Each node is a structure, which is divided into two parts. The former part stores data (data field), and the latter part stores data pointing to the next one. Pointer to the node (pointer field).
2.
insert image description here
Each node of the leading circular doubly linked list is a structure, which is divided into three parts. The first part stores the pointer to the previous node, the middle part stores the data, and the last part stores the pointer to the next node. pointer.

Second, realize the singly linked list

1. The structure of the singly linked list

typedef int DataType;
typedef struct ListNode
{
    
    
	DataType a;
	struct ListNode* next;
}ListNode;

2. Initialization of the singly linked list

//初始化 
ListNode* BuyListNode(DataType x)
{
    
    
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	newnode->a = x;
	newnode->next = NULL;
	return 1;
}

insert image description here

3. Insertion of single linked list

This code contains inserting any position of head plug and tail plug

int charu(ListNode *h,int p,int n)
{
    
    
	if(NULL==h)
	printf("NULL");
	//把指针移动到要插入位置的前一个位置
	ListNode *q=h; //q等于头指针 
	int k=1;
	while (k<p && q) //防止位置P太大,程序挂掉 
	{
    
    
	q=q->next;//头指针指向的下一个是要插入的前一个位置也就是指针域 
	k++;
	}
  //判断位置是否合法
  if(q==NULL||k>p)
  {
    
    
  	 printf("位置不合法");
  }
  ListNode *m=(ListNode*)malloc(sizeof(ListNode));  //申请一个新的节点
  //插入数据 
  m->a=n;  //新节点的数据域等于插入的数 
  m->next=q->next;//新节点的指针域等于上一个节点的指针域也就是q->next 
  q->next=m;//这个时候最开始节点的指针域要修改成现在的节点 
  printf("在第%d个位置插入了%d\n",p,n); 
}

insert image description here

4. Traverse the linked list

void listprintf(ListNode *h) 
{
    
    
	ListNode *q=h->next; //q指向第一个节点 
	while (q)
	{
    
    
		printf("%d\n",q->a);
		q=q->next;
	 } 
}

insert image description here

5. Length of linked list

//判断链表是否为空,不为空计算出长度 
void listlen(ListNode *h)
{
    
    
	if(h==NULL) 
	printf("链表为空\n");
	else
	printf("链表不为空\n"); 
	ListNode *q=h->next;
	int len=0;
	while(q)
	{
    
    
		len++;
		q=q->next;
	}
	printf("长度为%d\n",len);
}

insert image description here

Summarize

There is still some knowledge about the linked list, take a look at the blog, and attach the complete code

#include<stdio.h>
typedef int DataType;
typedef struct ListNode
{
    
    
	DataType a;
	struct ListNode *next;
}ListNode;
//初始化 
int BuyListNode(ListNode **h)
{
    
    
	*h = (ListNode*)malloc(sizeof(ListNode));
	(*h)->next = NULL;
	return 1;
}
int charu(ListNode *h,int p,int n)
{
    
    
	if(NULL==h)
	printf("NULL");
	//把指针移动到要插入位置的前一个位置
	ListNode *q=h; //q等于头指针 
	int k=1;
	while (k<p && q) //防止位置P太大,程序挂掉 
	{
    
    
	q=q->next;//头指针指向的下一个是要插入的前一个位置也就是指针域 
	k++;
	}
  //判断位置是否合法
  if(q==NULL||k>p)
  {
    
    
  	 printf("位置不合法");
  }
  ListNode *m=(ListNode*)malloc(sizeof(ListNode));  //申请一个新的节点
  //插入数据 
  m->a=n;  //新节点的数据域等于插入的数 
  m->next=q->next;//新节点的指针域等于上一个节点的指针域也就是q->next 
  q->next=m;//这个时候最开始节点的指针域要修改成现在的节点 
  printf("在第%d个位置插入了%d\n",p,n); 
}
void listprintf(ListNode *h) 
{
    
    
	ListNode *q=h->next; //q指向第一个节点 
	while (q)
	{
    
    
		printf("%d\n",q->a);
		q=q->next;
	 } 
}
//判断链表是否为空,不为空计算出长度 
void listlen(ListNode *h)
{
    
    
	if(h==NULL) 
	printf("链表为空\n");
	else
	printf("链表不为空\n"); 
	ListNode *q=h->next;
	int len=0;
	while(q)
	{
    
    
		len++;
		q=q->next;
	}
	printf("长度为%d\n",len);
}
int main()
{
    
    
   //定义一个头指针
	ListNode *head=NULL; 
	int ret=BuyListNode(&head);
	if(ret==1)
	printf("单链表初始化成功\n");
	charu(head,1,1);
	charu(head,2,2);
	charu(head,3,3);
	charu(head,4,4);
	listprintf(head);
	listlen(head);
} 

Guess you like

Origin blog.csdn.net/qq_51963216/article/details/128800763