数据结构:链表的基本操作及实现

/*链表*/
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdlib.h>
typedef struct listnode
{
	int data;//链表的数据域
	listnode* next;//指向链表下一节点的指针域
}listnode;
typedef struct headnode
{
	listnode* head;//指向链表的头节点
	int sz;//当前链表有几个节点
}headnode;
//定义结构体类型
int panduan(int a, headnode* head)
{
	while (a > head->sz || a < 1)//判断添加位置的有效性
	{
		int n;
		printf("请重新输入:");
		scanf("%d", &a);
		return a;
	}
	return a;
}
//判断结点是否有效
void creat(headnode* head, int n)
{

	listnode* end, * node;
	head->head = (listnode*)malloc(sizeof(listnode));//申请一个头节点
	end = head->head;//end=head,防止链表为空时,报错
	for (int i = 0; i < n; i++)//for循环初始化各个节点
	{
		node = (listnode*)malloc(sizeof(listnode));
		head->sz++;//增加一个节点sz++
		printf("请输入第%d个节点的值:", i + 1);
		scanf("%d", &node->data);
		end->next = node;
		end = node;
	}
	end->next = NULL;//让最后一个节点的next指向空
}
//创建链表
void add(headnode* head)
{
	int a = 0;
	int n = 0;
	listnode* h = head->head;//备份一个头指针
	if ((head->sz == 0)||(head->head==NULL))
	{
		printf("请先创建链表\n");
		return;
	}
	printf("请输入要增加在第几个节点的后面:");
	scanf("%d", &a);
	a = panduan(a, head);
	printf("请输入要增加的数字:");
	scanf("%d", &n);
	for (int i = 0; i < a; i++)//找到要增加节点的位置
	{
		h = h->next;
	}
	listnode* node = (listnode*)malloc(sizeof(listnode));
	head->sz++;//增加一个节点,sz++
	node->next = h->next;
	h->next = node;
	node->data = n;
	printf("增加成功!\n");
}
//增加结点
void del(headnode* head)
{
	int f = 0;
	listnode* h = head->head;//备份一个头指针
	if (head->sz == 0)
	{
		printf("请先创建链表\n");
		return;
	}
	printf("请输入要删除的节点位置:");
	scanf("%d", &f);
	f = panduan(f, head);
	if (f==0)
	{
		return;
	}
	for (int i = 0; i < f - 1; i++)//找到要删除节点的前一个位置
	{
		h = h->next;
	}
	listnode* del = h->next;
	h->next = h->next->next;
	free(del);//释放删除的节点
	del = NULL;
	head->sz--;//删除一个节点sz--
	printf("删除成功!!!\n");
}
//删除结点
void search(headnode* head)
{
	int s = 0;
	listnode* h = head->head;
	printf("请输入要查找的节点位置:");
	scanf("%d", &s);
	s = panduan(s, head);
	for (int i = 0; i < s; i++)//找到要查找节点的位置
	{
		h = h->next;
	}

	printf("第%d个节点的数值是:%d\n", s, h->data);
}
//查询结点
void data(headnode *head)//修改数值
{
	int l = 0;
	int u = 0;
	listnode *h = head->head;
	printf("请输入要修改的节点位置:");
	scanf("%d", &l);
	l=panduan(l, head);
	printf("请输入修改后的数值:");
	scanf("%d", &u);
	for (int i = 0; i < l; i++)//找到要修改节点的位置
	{
		h = h->next;
	}
	h->data = u;
	printf("修改成功!!!\n");
}
void show(headnode* head)
{
	if ((head->sz == 0)||(head->head==NULL))
	{
		printf("请先创建链表\n");
		return;
	}
	listnode* h = head->head;
	while (h->next != NULL)//打印链表
	{
		h = h->next;
		printf("%d ", h->data);
	}
	printf("\n");
}
//打印链表
void destroy(headnode* head)
{
	free(head->head);
	head->head = NULL;
}
//释放内存
void menu()
{
	printf("........................\n");
	printf("1.创建链表    2.插入节点\n");
	printf("3.删除节点    4.查找节点\n");
	printf("5.修改链表    6.展示链表\n");
	printf("7.清空链表    0.退出程序\n");
	printf("........................\n");
}

int main1()
{
	int n = 0,a;
	headnode head = { 0 };//创建一个headnode类型的head

	do
	{
		menu();
		printf("请输入选择:");
		scanf("%d", &n);
		switch (n)
		{
		case 1:
			printf("请输入链表的个数:");
			scanf("%d", &a);
			if (a<=0)
			{
				printf("请输入大于0的整数!\n");
			}else{
				creat(&head, a);
			}
			break;
		case 2:
			add(&head);
			break;
		case 3:
			del(&head);
			break;
		case 4:
			search(&head);
			break;
		case 5:
			data(&head);
			break;
		case 6:
			show(&head);
			break;
		case 7:
			destroy(&head);
			printf("链表已清空\n");
			break;
		case 0:
			printf("退出\n");
			break;
		default:
			printf("选择错误,请重新输入!\n");
			break;
		}
	} while (n);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值