单链表的创建,插入,输出操作

 一 链表中存的int数据

#include<stdio.h>
#include<stdlib.h>
struct MyStruct
{
	int  data;
	struct MyStruct * next;
};
void chuangjian_and_charu(struct MyStruct* head,struct MyStruct*a)//头插法,将新的节点插在头结点之后。
{	
	int n;
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
	{
		struct MyStruct* s = (struct MyStruct*)malloc(sizeof(struct MyStruct));
		scanf_s("%d", &s->data);
		s->next = head->next;
		head->next = s;
	}
	while (a->next != NULL)//如果a的下一个结点不为NULL,也就是说如果a结点后还有节点存在,那就输出a后面结点中保存的数据。
	{
		printf("%d ", a->next->data);
		a = a->next;//a结点后移
	}
}
void delete_(struct MyStruct* head, struct MyStruct* a)
{
	a = head;
	struct MyStruct* b=head;创建b节点
	int m;
	int i = 0;
	printf("输入你要删除第几个节点:\n");
	scanf_s("%d", &m);
	while (b->next!=NULL)
	{	
		i++;
		a = b;
		b = b->next;
		if (i==m) 
		{
			a->next = b->next;
			free(b);//与malloc相对应,free释放内存
			b = a->next;
		}		
		printf("%d ", b->data);		
	}
	
}
int main()
{
	struct MyStruct* head = (struct MyStruct*)malloc(sizeof(struct MyStruct));//创建头节点,并分配内存
	head->next = NULL;//head指针的初始化
	struct MyStruct* a;
	a = head;//让指针a指向head
	chuangjian_and_charu(head,a);//调用chaungjian_and_charu
	delete_(head,a);//调用delete_
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

桂亭亭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值