数据结构C语言 单链表(插入、删除、查找)

本文详细介绍了如何使用C语言实现单链表的基本操作,包括插入、删除和查找元素的具体步骤与代码实现。通过示例代码,读者可以深入理解单链表数据结构,并掌握其核心算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构C语言 单链表(插入、删除、查找)

1、插入在这里插入图片描述

假设_A_的临时指针为 “p”C_的临时指针为 “q“
步骤1: _ 删除这条连接线;

步骤2: 将p->next给q->next;
步骤3: 将q给p->next;
插入代码

q->next = p->next;
p->next = q;
2、删除在这里插入图片描述

假设_A_的临时指针为 “p” ,_B_的临时指针为 “q“
步骤1: 删除这条连接线;
步骤2: 删除这条连接线;
步骤3: 将p->next给q,接着将q->next给p->next,最后free(q);
删除代码

q = p->next;
p->next = q->next;
free(q);
3、具体代码(插入、删除、查找)
typedef struct node{
	int data;					//结点的数据域
	struct node *next;			//结点的指针域
}NODE,*LinkList;


/*
Function:	Find_List
Description:	在链表中查找第k个元素,若找到,返回该元素结点的指针;否则,返回空指针。
Others:		查找代码
*/
LinkList Find_List(LinkList L, int k)
{
	LinkList p;
	int i;
	i = 1;
	p = L->next;
	
	while(p && i < k)
	{
		p = p->next;
		i++;
	}
	
	if(p && i == k)
	{
		return p;
	}
	
	return NULL;
}


/*
Function:	Insert_List
Description:	将元素newElem插入到第k-1个元素之后
Others:		插入代码
*/
int Insert_List(LinkList L, int k, int newElem)
{
	LinkList p, q;						//p,q为临时指针
	
	if(k == 1)							//元素newElem插入到第一个元素之前时
	{
		p = L;
	}
	else
	{
		p = Find_List(L, k-1);			//查找链表中的第k-1个元素并令p指向该元素结点
	}
	
	if(!p)								//不存在k-1元素
	{
		return -1;
	}

	q = (NODE *)malloc(sizeof(NODE));	//创建新元素的结点空间
	if(!q)
	{
		return -1;
	}

	// 元素newElem插入第k-1个元素之后
	q->data = newElem;
	q->next = p->next;
	p->next = q;
	
	return 0;
}

/*
Function:	Delete_List
Description:	删除第k个元素
Others:		删除代码
*/
int Delete_List(LinkList L, int k)
{
	LinkList p, q;						//p,q为临时指针
	
	if(k == 1)							//元素newElem插入到第一个元素之前时
	{
		p = L;
	}
	else
	{
		p = Find_List(L, k-1);			//查找链表中的第k-1个元素并令p指向该元素结点
	}
	
	if(!p || !p->next)					//不存在第k个元素
	{
		return -1;
	}

	//删除第k个元素
	q = p->next;
	p->next = q->next;
	free(q);
	
	return 0;
}
在C语言中,单链表是一种常见的数据结构,它由一系列节点组成,每个节点包含数据域和指向下一个节点的指针。以下是单链表插入删除查找操作的一些基本代码示例: 1. **插入操作** (在头部插入): ```c typedef struct Node { int data; struct Node* next; } Node; void insertAtBeginning(Node** head, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("Memory allocation failed.\n"); return; } newNode->data = value; newNode->next = *head; *head = newNode; } ``` 2. **插入操作** (在特定位置插入): ```c void insertAfter(Node** head, int target, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("Memory allocation failed.\n"); return; } newNode->data = value; newNode->next = NULL; Node* current = *head; while (current != NULL && current->data != target) { current = current->next; } if (current == NULL) { printf("Target not found in the list.\n"); } else { newNode->next = current->next; current->next = newNode; } } ``` 3. **删除操作** (根据值删除节点): ```c void deleteNode(Node** head, int value) { Node* temp = *head; Node* prev = NULL; if (temp != NULL && temp->data == value) { *head = temp->next; free(temp); return; } while (temp != NULL && temp->data != value) { prev = temp; temp = temp->next; } if (temp == NULL) { printf("Value not found in the list.\n"); } else { prev->next = temp->next; free(temp); } } ``` 4. **查找操作** (按值查找节点): ```c Node* searchNode(Node* head, int value) { Node* current = head; while (current != NULL) { if (current->data == value) return current; current = current->next; } return NULL; // 如果未找到则返回NULL } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值