双向链表的基本操作大全(增删改查...)-C语言

本文详细介绍了如何使用C语言实现双向链表的基本操作,包括创建链表、在指定位置插入元素、删除元素、修改节点数据以及查找元素。示例代码展示了在不同位置插入、删除节点的过程,并提供了查找元素的索引。

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

//双向链表的基本操作(增删改查...)
//新创建双链表为  41->67->34->0->69->24->78
//在表中第 4 的位置插入元素 1     41->67->34->1->0->69->24->78
//在表中第 2 的位置插入元素 8     41->8->67->34->1->0->69->24->78
//表中删除元素 8                  41->67->34->1->0->69->24->78
//元素 1 的位置是 :4
//表中第 4 个节点中的数据改为存储3        41->67->34->3->0->69->24->78
#include <stdio.h>
#include <stdlib.h>
//随机数的范围
#define Max 100
//节点结构体
typedef struct Node
{
	struct Node *pre;   //指针域-->指向前驱节点 
	int data;          //数据域 
	struct Node *next;//指针域-->指向后继节点 
}Node;
Node *CreatNode(Node *head);
Node *CreatList(Node *head, int length);
Node *InsertListHead(Node *head, int add, int data);
Node *InsertListEnd(Node *head, int add, int data);
void PrintList(Node *head);
Node *DeleteList(Node *head, int data);
Node *ModifyList(Node *head, int add, int newElem);
int FindList(Node *head, int elem);
int main() 
{
    Node * head=NULL;
    //创建双链表
    head=CreatList(head,7);
    printf("新创建双链表为\t");
    PrintList(head);
    //在表中第 4 的位置插入元素 1
    head=InsertListHead(head, 4,1);
    printf("在表中第 4 的位置插入元素 1\t");
    PrintList(head);
    //在表中第 2 的位置插入元素 8
    head=InsertListEnd(head, 2, 8);
    printf("在表中第 2 的位置插入元素 8\t");
    PrintList(head);
     //表中删除元素 8
    head=DeleteList(head, 8);
    printf("表中删除元素 8\t\t\t");
    PrintList(head);
    printf("元素 1 的位置是\t:%d\n",FindList(head,1));
    //表中第 4 个节点中的数据改为存储 3
    head = ModifyList(head,4,3);
    printf("表中第 4 个节点中的数据改为存储3\t");
    PrintList(head);
    return 0;
}

//创建双向链表 
Node *CreatNode(Node *head)
{
	head = (Node *)malloc(sizeof(Node));
	if(head == NULL)
	{
		printf("malloc error\r\n");
		return NULL;
	}
	head->pre = NULL;
	head->data = rand() % Max;
	head->next = NULL;
	return head; 
} 
Node *CreatList(Node *head, int length)
{
	if(length == 1)
		return (head = CreatNode(head));
	else
	{
		head = CreatNode(head);
		Node *list = head;
		for(int i = 1; i < length; i++)
		{
			Node *body = (Node *)malloc(sizeof(Node));
			body->pre = NULL;
			body->next = NULL;
			body->data = rand() % Max;
			//这样也可以创建 创建的第一个设为head 
//			if(i == 1)
//				head = body;
//			else
//			{
//				list->next = body;
//				body->pre = list;
//			}
//			list = body;
			list->next = body;
			body->pre = list;
			list = list->next; 
		}
	}
//	加上下面两句就是双向链表
//	list->next = head;
//	head->pre = list; 
	return head;
}

//双向链表的插入(头部、中部、尾部)
//add  向前插入数据 
Node *InsertListHead(Node *head, int add, int data)
{
	Node *temp = (Node *)malloc(sizeof(Node));
	if(temp == NULL)
	{
		printf("malloc error\r\n");
		return NULL;
	} 
	else
	{
		temp->data = data;
		temp->pre = NULL;
		temp->next = NULL;
	}
	//插入到链表表头 
	if(add == 1)
	{
		temp->next = head;
		head->pre = temp;
		head = temp;
	}
	else
	{
		Node *body = head;
		for(int i = 1; i < add; i++)
		{
			body = body->next;  //插入到第2个只需要找到第1个 head是第1个 
		} 
		body->pre->next = temp;
		temp->pre = body->pre;
		temp->next = body;
		body->pre = temp;
	}
	return head; 
} 
//add  向后插入数据 
Node *InsertListEnd(Node *head, int add, int data)
{
	int i = 1;
	Node *temp = (Node *)malloc(sizeof(Node));
	if(temp == NULL)
	{
		printf("malloc error\r\n");
		return NULL;
	} 
	else
	{
		temp->data = data;
		temp->pre = NULL;
		temp->next = NULL;
	}
	Node *body = head;
	while((body->next) && (i < add-1))
	{
		body = body->next;  //插入到第2个只需要找到第1个 head是第1个 
		i++;
	} 
	if(body->next == NULL)
	{
		body->next = temp;
		temp->pre = body;
	}
	else
	{
		//搭上四条线----建立链接 
		body->next->pre = temp;
		temp->next = body->next;
		body->next = temp;
		temp->pre = body;
	}
	return head;
}

//双向链表的删除
Node *DeleteList(Node *head, int data)
{
	Node *temp = head;
	while(temp)
	{
		if(temp->data == data)
		{
			if(temp->pre == NULL)
			{
				head = temp->next;
				temp->next = NULL;
			}
			else if(temp->next == NULL)
			{
				temp->pre->next = NULL;
			}
			else
			{
				temp->pre->next = temp->next;
				temp->next->pre = temp->pre;
			}
			free(temp);
			return head; 
		}
		temp = temp->next;
	}
	printf("No find %d\n", data);
	return head;
}
//双向链表修改节点数据 
Node *ModifyList(Node *head, int add, int newElem)
{
	Node *temp = head;
	for(int i = 1; i < add; i++)
	{
		temp = temp->next; 
	}
	temp->data = newElem;
	return head;
}
//双向链表的查找
int FindList(Node *head, int elem)
{
	Node *temp = head;
	int i = 1;
	while(temp)
	{
		if(temp->data == elem)
			return i;
		i++;
		temp = temp->next;
	}
	return -1;
} 
//双向链表的打印
void PrintList(Node *head)
{
	Node *temp = head;
	while(temp)
	{
		if(temp->next == NULL)
			printf("%d\n", temp->data);
		else
			printf("%d->", temp->data);
		temp = temp->next; 
	}
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脆订壳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值