蓝胖子喊你学数据结构之单链表练习二(加强)——对单链表进行插入、删除、删除并返回删除元素

在前一篇文章的基础上https://blog.csdn.net/2201_75285422/article/details/134407379?spm=1001.2014.3001.5501

进一步对单链表的操作进行了加强 ,包含对单链表中指定位置插入指定元素、删除指定位置的元素并返回其被删除元素的值。

背景设置:假设单链表内定义的元素全为int类型。

代码如下:

/*11.15今日目标:单链表的插入删除并完成上机题1.2*/
//现已经实现了 单链表的创建、打印、按位查找、按值查找
#include<stdio.h>
#include<stdlib.h>
typedef int elemtype;
typedef struct node {
	elemtype data;
	struct node* next;
}LinkList, LNode;

//头插法创建链表
LinkList* CreateList(int n)
{
	int i;
	LinkList* L, * p;
	L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;//L为头结点
	for (i = n; i >= 1; i--)
	{
		p = (LNode*)malloc(sizeof(LNode));
		printf("请输入第%d个元素:\n", i);
		scanf_s("%d", &p->data);
		p->next = L->next;
		L->next = p;
	}
	return L;
}

尾插法创建单链表
//LinkList* CreateList2(int n)
//{
//	int i;
//	LinkList* L,*r,*p;
//	L = (LNode*)malloc(sizeof(LNode));
//	r= (LNode*)malloc(sizeof(LNode));
//	r = L;
//	for (i = 0; i < n; i++)
//	{
//		p = (LNode*)malloc(sizeof(LNode));
//		printf("请输入第%d个元素",i+1);
//		scanf_s("%d", &p->data);
//		r->next = p;
//		r = p;
//	}
//	return L;
//}


//打印
LinkList*PrintList(LNode* L)
{
	LinkList* s;
	s = (LNode*)malloc(sizeof(LNode));
	s = L->next;//指向首结点
	printf("现输出单链表中的元素:\n");
	while (s != NULL)//s为空则停止
	{
		printf("%d ", s->data);
		s = s->next;
	}
	printf("\n");
	return 0;
}

//按位查找
LinkList* Getelem(LNode L, int i, elemtype* e)
{
	int j = 1;
	LinkList* p;
	if (L.data == NULL)
	{
		printf("empty");
		exit(0);

	}
	p = (LNode*)malloc(sizeof(LNode));
	p = L.next;//p指向首结点
	if (i < 1)
	{
		printf("查找数值错误");
		exit(0);
	}
	while (j < i && p != NULL)
	{
		j++;
		p = p->next;
	}
	if (p == NULL)
	{
		printf("查找数值不存在\n");

	}
	else
		*e = p->data;//p->data 是elemtype类型的
	return 0;

}
//按值查找
int LocateList(LNode L, elemtype e)
{
	LinkList* s;
	int i = 1;
	s = (LNode*)malloc(sizeof(LNode));
	s = L.next;
	if (L.data == NULL)
	{
		printf("empty"); exit(0);
	}
	while (s != NULL && s->data != e)//此处把s!=NULL放前面 判断表满
	{
		s = s->next;
		i++;
	}
	if (s == NULL)
	{
		printf("链表中不存在该值\n");
		return 0;
	}
	else
		return i;
}
删除指定位置的链表元素(不返回删除部分)
//int Delect(LNode* L, int i)
//{
//	if (L->next == NULL) {
//		printf("empty");
//		exit(0);
//	}
//	int j=0;
//	LinkList* s;
//	s = (LNode*)malloc(sizeof(LNode));
//	s = L;//s指向头结点
//	while(s!= NULL &&j<i-1)
//	{
//		s = s->next;
//		j++;
//	}//将s定位到i-1的结点
//	if (s ->next==NULL)
//	{
//		printf("删除元素不存在\n");
//		return 0;
//	}
//	else
//	{
//		LinkList* q;
//		q = (LNode*)malloc(sizeof(LNode));
//		q = s->next;
//		s->next = q->next;
//		free(q);
//		return L;
//	}
//
//	
//	
//}
//删除指定位置的链表元素(返回删除部分)
int Delect(LNode* L, int i,elemtype*e)
{
	if (L->next == NULL) {
		printf("empty");
		exit(0);
	}
	int j = 0;
	LinkList* s;
	s = (LNode*)malloc(sizeof(LNode));
	s = L;//s指向头结点
	while (s != NULL && j < i - 1)//此处可以参考插入算法
	{
		s = s->next;
		j++;
	}//将s定位到i-1的结点
	if (s->next == NULL)
	{
		printf("删除元素不存在\n");
		return 0;
	}
	else
	{
		LinkList* q;
		q = (LNode*)malloc(sizeof(LNode));
		q = s->next;
		s->next = q->next;
		*e = q->data;
		free(q);
		return L;
	}



}
//插入元素到指定的位置
int InsertList(LNode*L,int i,elemtype a)
{
	LinkList* s;
	s = (LNode*)malloc(sizeof(LNode));
	s = L;
	if (L->next == NULL)
	{
		printf("empty");
		exit(0);
	}
	else
	{
		
		int j = 1;//若j为0 则j<i-1否则会多向后指 指到第i个元素 本算法要求指到i前一个元素进行插入
		while (s->next != NULL && j < i)
		{
			s = s->next;
			j++;
		}
	}

	
	
	LinkList* p;
	p = (LNode*)malloc(sizeof(LNode));
	p->data = a;
	p->next = s->next;//先连再断
	s->next = p;
	return 0;
	//free(p);犯了思想固化的错误 如果在这里把p释放掉了以后 链表就找不到了
}
int main()
{
	int num, i,c,f;
	LinkList* first;
	elemtype* e, b,*d,g;
	e = (elemtype*)malloc(sizeof(elemtype));
	d =(elemtype*)malloc(sizeof(elemtype));
	/*LinkList*first2;*/
	printf("请输入单链表存储的个数:\n");
	scanf_s("%d", &num);
	first = CreateList(num);
	/*first2 = CreateList2(num);*/
	PrintList(first);
	/*PrintList(first2);*/
	printf("请输入要查找的值:\n");
	scanf_s("%d", &i);
	Getelem(*first, i, e);
	if (i <= num)
		printf("查找的第%d个元素是:%d\n", i, *e);
	printf("请输入要查找的内容:\n");
	scanf_s("%d", &b);
	if (LocateList(*first, b))
		printf("%d的位置是%d\n", b, LocateList(*first, b));
	printf("请输入准备删除元素的位置:\n");
	scanf_s("%d", &c);
	/*Delect(first, c);*/	
	Delect(first, c, d);
	printf("被删除的元素为:%d\n", *d);
	PrintList(first);
	printf("请输入你准备插入的位置为:\n");
	scanf_s("%d", &f);
	printf("请输入你要插入的元素为:\n");
	scanf_s("%d", &g);
	InsertList(first, f, g);
	PrintList(first);
    return 0;
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值