c语言链表的简单实现

这篇博客介绍了如何在C语言中实现单向链表的基本操作,包括创建空表、顺序插入元素、查找元素位置、获取前驱元素、随机插入及删除元素。在调试过程中注意到了查找前驱元时可能存在的问题,并提出了相应的解决方案。
摘要由CSDN通过智能技术生成

本例程默认,表头为空的单向链表
首先完成声明操作

#include <iostream>
struct Node;
typedef struct Node* Node_p;
typedef struct Node* list;
struct Node {
	int value;
	Node_p Naxt;
};

创建一个空表

list Makelist(){
	Node_p head = (Node_p)malloc(sizeof(struct Node));
	if (head == NULL) printf("内存不足");
	head->value = 0;
	head->Naxt = NULL;
	return head;
}

向表中顺序的插入元素

void Insert(int x,list L) {
	Node_p temp,p;
	temp = L;
	while (temp->Naxt != NULL)	temp = temp->Naxt;
	p= (Node_p)malloc(sizeof(struct Node));
	if (p == NULL) printf("内存不足");
	p->value = x;
	p->Naxt = NULL;
	temp->Naxt = p;
}

返回一个元素在表中的位置

Node_p Find(int i, list L) {
	Node_p temp;
	temp = L->Naxt;
	while (temp!=NULL&&temp->value!=i) temp = temp->Naxt;
	return temp;
}

返回一个元素在表中的前驱元

Node_p Findpre(int x, list L) {
	Node_p tem;
	tem = L->Naxt;
	while (tem->Naxt != NULL && tem->Naxt->value != x)	tem = tem->Naxt;
	return tem;
}

在链表中完成随机插入功能,且此功能调用,返回位置的函数即Find

void raddom_Insert(int x, list L,int v_insret) {
	Node_p temp=Find(x, L);
	if (temp == NULL) { printf("不存在你所指定的元素"); return ; }
	Node_p middle = (Node_p)malloc(sizeof(struct Node));
	middle->value = v_insret;
	if (temp->Naxt == NULL) temp->Naxt = middle;
	else
	{
		Node_p t;
		t=temp->Naxt;
		temp->Naxt = middle;
		middle->Naxt = t;
	}
}

删除指定的元素,此函数需要掉用,返回前驱元的函数即FIndpre

void Dlelte(int i,list L) {
	Node_p temp, p;
	temp = Findpre(i, L);
	if (temp->Naxt == NULL) { printf("表中没有要指定的元素\n"); return ; }
	p = temp->Naxt->Naxt;
	free(temp->Naxt);
	temp->Naxt = p;
}

附上调试所用的函数

void Traversal(list L) {
	Node_p temp;
	if (Test(L)) { printf("此表为空"); return ; }
	temp = L->Naxt;

	while (temp!=NULL)
	{
		static 	int i = 1;
		printf("第%d次=%d\n", i, temp->value);
		temp = temp->Naxt;
		i++;
	}
}

这里贴上完整的调试时所用代码,供参考。

#include <iostream>
struct Node;
typedef struct Node* Node_p;
typedef struct Node* list;
struct Node {
	int value;
	Node_p Naxt;
};
//创建一个空表
list Makelist(){
	Node_p head = (Node_p)malloc(sizeof(struct Node));
	if (head == NULL) printf("内存不足");
	head->value = 0;
	head->Naxt = NULL;
	return head;
}
//向表中顺序插入元素
void Insert(int x,list L) {
	Node_p temp,p;
	temp = L;
	while (temp->Naxt != NULL)	temp = temp->Naxt;
	p= (Node_p)malloc(sizeof(struct Node));
	if (p == NULL) printf("内存不足");
	p->value = x;
	p->Naxt = NULL;
	temp->Naxt = p;
}
//检验一个链表是否为空
int Test(list L) {
	return L->Naxt == NULL;
}
//遍历输出一个表用于调试错误
void Traversal(list L) {
	Node_p temp;
	if (Test(L)) { printf("此表为空"); return ; }
	temp = L->Naxt;

	while (temp!=NULL)
	{
		static 	int i = 1;
		printf("第%d次=%d\n", i, temp->value);
		temp = temp->Naxt;
		i++;
	}
}
//返回一个元素在表中的位置
Node_p Find(int i, list L) {
	Node_p temp;
	temp = L->Naxt;
	while (temp!=NULL&&temp->value!=i) temp = temp->Naxt;
	return temp;
}
//返回一个元素在表中位置的前驱元
Node_p Findpre(int x, list L) {
	Node_p tem;
	tem = L->Naxt;
	while (tem->Naxt != NULL && tem->Naxt->value != x)	tem = tem->Naxt;
	return tem;
}
//中间插入操作,在指定元素的之后进行插入,也可以完成顺序插入
void raddom_Insert(int x, list L,int v_insret) {
	Node_p temp=Find(x, L);
	if (temp == NULL) { printf("不存在你所指定的元素"); return ; }
	Node_p middle = (Node_p)malloc(sizeof(struct Node));
	middle->value = v_insret;
	if (temp->Naxt == NULL) temp->Naxt = middle;
	else
	{
		Node_p t;
		t=temp->Naxt;
		temp->Naxt = middle;
		middle->Naxt = t;
	}
}
//删除指定元素
void Dlelte(int i,list L) {
	Node_p temp, p;
	temp = Findpre(i, L);
	if (temp->Naxt == NULL) { printf("表中没有要指定的元素\n"); return ; }
	p = temp->Naxt->Naxt;
	free(temp->Naxt);
	temp->Naxt = p;
}
int main()
{
	list l;
	int k = 3, i=123,x=10;
	l = Makelist();
	for (int i = 0; i < 10; i++)	Insert(i, l);
	raddom_Insert(k, l, i);
	Dlelte(x, l);
	Traversal(l);
	getchar();
	return 0;
}

调试结果
请注意输出的第一句提示语句,他是由查找前驱元所存的不完美之处引起的,即没有找到所要找的元素,但是遍历完链表,返回了最后的节点的前驱元,我们只需要在删除操作中做一步检查即可,即检查该前驱元指向的value是否为要删除的元素。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值