Linked list of data structure foundation (2)

Next to the previous article, this article talks about how to operate on the linked list.

Create a linked list

It is now possible to obtain several scattered nodes through malloc or new. The next step is to link these scattered nodes. Very simple, just point the next pointer of each node to the address of the next node. code show as below:

//定义结构体已经在上一篇文章中写过了,这里就直接用了
ndoe* node1 = new node;
ndoe* node2 = new node;
ndoe* node3 = new node;
ndoe* node4 = new node;
ndoe* node5 = new node;
node1->data = 5;//数据域赋值
node1->next = node2;
node2->data = 3;
node1->next = node3;
node3->data = 6;
node1->next = node4;
node4->data = 1;
node1->next = node5;
node5->data = 2;
node1->next = NULL;

Although this method seems very clear, it is a bit verbose, so let's implement it with a for loop.

#include<stdio.h>
#include<stdlib.h>
strcut node{
	int data;
	node* next;
};
//创建链表
node* create(int array[]){
	node *p,*pre,*head;
	head = new head;	//创建头结点
	head->next = NULL;	//指针域置为空
	pre = head;	//pre记录头结点
	for(int i=0;i<5;i++){
		p = new node;	//新建结点
		p->data = array[i];	//给其数据域赋值
		p->next = NULL;	//新建结点的指针域为NULL
		pre->next = p;	//前驱结点的指针域设置为当前新建结点的地址
		pre = p;	把pre设置为p,作为下个结点的前驱结点
	}
	return head;	//返回结点头指针
}

int main(){
	int array[5] = {5,3,6,1,2};
	node* L= create(array); //新建链表,返回头指针head赋值给L
	L= L->next;
	while(L != NULL){
		printf("%d",L->data);	//输出每个结点的数据域
		L = L->next;
	}
	return 0;
}

Finding elements
If there is a linked list, how to find whether there is a given element x? Very simple, just start from the first node, and constantly judge whether the data field of the current node is equal to x, if it is equal, then add 1 to the counter count, so that when the end of the list is reached, the value of count is the element x in the list The number.

//再以head为头结点的链表上计数元素x的个数
int search(node* head,int x){
	int count = 0;
	node* p = head->next;	//从一个结点开始
	while(p != NULL){ //只要没有到达链表末位
	if(p->data == x){
		count++; //相等则加一
		}
	p = p->next;
	}
	return count; //返回计数器count
}

The above part of the code can be directly written in the code in the "create linked list" part, and the head pointer returned by the create function can be directly passed as the first and parameter.
Inserting elements
For linked lists, inserting elements refers to inserting a node at a given position in the linked list. For example, inserting element 4 in the third position of linked list 5, 3, 6, 1, 2 will make the linked list become 5, 3, 4, 6, 1, 2. Some people will ask whether the so-called i-th position is inserted before or after this position, that is, the above example will eventually form 5,3,6,4,1,2 or 5,3,4,6,1,2 . In fact, in the third position of the insertion element 4 means that the insert element after the completion of the third position is 4, so it should be the beginning of the third position of the element to make it out to the need to insert a number, as shown
Insert picture description here
delete elements
For linked lists, deleting an element refers to deleting; all values ​​on the linked list are the given number x, for example, deleting 6 in 5,3,6,1,2 will make the linked list become 5,3,1,2 As shown in the following figure, the
Insert picture description here
delete operation is carried out as follows:
1. The node is enumerated by the pointer variable p, and another pointer variable pre indicates the precursor node where p points to the node.
2. When the node pointed to by p happens to be x, perform the following three operations:
(1) Make the pointer field next of the node pointed to by pre point to the next node pointed to by p
(2) Release the pointed to by p The memory space of the node
(3) makes p point to the next node of the node pointed to by pre

//删除head为头结点的链表中所有数据域为x的结点
void del(node* head,int x){
	node* p = head->next;	//	p从第一个结点开始枚举
	node* pre = head;
	while(p != NULL){
		if(p->data == x){
			pre->next = p->next;
			delet(p);
			p = pre->next;
		}else{
			pre = p;
			p = p->next;

		}
	}
}

Insert picture description here
This part of the code can be written directly in the code in the "create linked list" part, and the return pointer L of the create function can be directly passed as the first parameter,

Published 19 original articles · won 2 · views 734

Guess you like

Origin blog.csdn.net/zan1763921822/article/details/105602333