数据结构单链表(c语言)

c语言数据结构单链表的实现

通过c语言实现单链表的建立、初始化、尾插法插入结点、头插法插入结点、数据的查找、删除、在指定位置插入结点、在指定位置删除结点、
打印输出数据、统计单链表的长度等功能。
#include <stdio.h>
#include <stdlib.h>

typedef struct List
{
	char x;//结点的数据区
	struct List * next;//结点的地址区
}e;
void setHead(e *l)
{
	l->next = NULL;
}
void newList(e *l)//尾插法建立链表
{
	//连续建立结点
	//连续输入数据以$表示建表结束
	int i = 1;
	char a;//临时储存数据
	printf("请输入数据!\n");
	while (i)
	{	
		a = getchar();
		if (a == '$')
		{
			i = 0;
		}
		else
		{
			while (l->next == NULL)
			{
				e *l2 = (e *)malloc(sizeof(e));//给新的节点分配空间
				l2->x = a;//将数据放进结点的数据区
				l->next= l2;//上一节点的地址区指向新的结点
				l = l2;//移动指针向后移动
				l2->next = NULL;//新结点的地址区为空
				break;
			}
		}
	}
}
void endAdd(e *l,char a)//尾插法插入数据
{
	e * l1 = l;//移动指针初始化
	while (1)
	{
		//通过遍历将移动指针指向最后一个结点
		if (l1->next != 0)
		{
			l1 = l1->next;
		}
		else
		{
			break;
		}
	}
	e *l2 = (e *)malloc(sizeof(e));//为插入的结点分配内存
	l2->x = a;//将数据存入结点
	l1->next = l2;//将最后结点的指针区指向新添加的结点
	l2->next = NULL;//将最后结点的指针区赋值为空
}
void headAdd(e *l,char a)
{
	e *l2 = (e *)malloc(sizeof(e));//为新的结点分配内存
	l2->x = a;
	//新结点的地址区指向原来头结点的地址区指向
	l2->next = l->next;
	//头结点地址区指向新的结点
	l->next = l2;
}
void printfList(e * l)//输出链表内容
{
	while (l->next != NULL)
	{
		//遍历输出结点的数据区
		l = l->next;
		printf("%c\t", l->x);
	}
	printf("\n");
}
void longList(e *l)//获取单链表的长度
{
	int i = 0;//统计长度
	while(l->next)
	{
		//遍历统计单链表的长度
		l = l->next;
		i++;
	}
	printf("\n该单链表的长度为:%d\n", i);
}
int deleteList(e * l, int j)
{
	//将需要删除的上一个结点的地址区指向需要删除结点的指针区所指向的区域
	e *l2 = NULL;
	int i = 0;//标记结点位置
	while (l->next&& j < i - 1)
	{
		i++;
		l = l->next;
	}
	if (l->next)
	{
		l2 = l->next;//将要删除的结点地址储存下来
		l->next = l->next->next;
		free(l2);//释放空间
		//删除成功返回0
		return 0;
	}
	//删除失败返回-1
	return -1;
}
int add(e *l, int j, char a)
{
	int i = 0;//标记位置
	e *l1=l;//移动指针
	while (l1->next!=NULL)
	{
		if (j - 1 == i)//当指针移动到插入位置的前一个结点时
		{
			e *l2 = (e *)malloc(sizeof(e));
			l2->next = l1->next;
			l1->next = l2;
			l2->x = a;
			//添加成功返回0
			return 0;
		}
		i++;
		l1 = l1->next;
	}
	//添加失败返回-1
	return -1;
}

int  findList(e *l, char a)
{
	e *l1 = l;
	int i = 0;
	while (l1->next != NULL)
	{
		//遍历链表的结点
		l1 = l1->next;
		i++;
		if (l1->x == a)
		{
			//发现需要查找的数据则返回该数据所在结点在链表中的位置
			return i;
		}
	}
	//未找到则返回-1
	return -1;
}
int main()
{	
	
	e *head = (e *)malloc(sizeof(e));
	setHead(head);//初始化头结点
	newList(head);//初始化链表
	longList(head);//输出链表的长度
	printfList(head);//输出链表的数据
	headAdd(head, 's');//头插法插入结点
	longList(head);//输出新链表的长度
	printfList(head);//输出新链表的数据
	deleteList(head,1);//删除链表中第一个结点
	longList(head);//输出新链表的长度
	printfList(head);//输出新链表的数据
	add(head, 2, 's');//在链表的第二个位置插入
	longList(head);//输出新链表的长度
	printfList(head);//输出新链表的数据
	int q=findList(head, 's');//查找结点
	if (q > 0)
	{
		printf("该结点的位置是:%d\n", q);
	}
	else
	{
		printf("没有找到该结点\n");
	}
	system("pause");
	return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值