创建一个简单的单链表

1.头文件的Slist.h的代码

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
typedef int SListint;
typedef struct Slist//单链表
{
	SListint data;
	struct Slist* next;
}SL;
 //尾插
void SlistPushBank(SL** plist,SListint x);
//头插
void SlistPushFront(SL** plist, SListint x);
//打印
void Slist_print(SL** plist);
//尾删
void SlistPopBank(SL** plist);
//头删
void SlistPopFront(SL** plist);
//查找
SL* SlistFind(SL* plist, SListint x);
//在pos位置前插入x
void SlistInsert(SL** plist, SL* pos, SListint x);
//删除pos位置的值
void SListFrase(SL** plist, SL* pos);
//销毁
void SlistDestroyed(SL** plist);

2.源文件Slist.h的函数实现

#include"Slist.h"
//申请空间函数
SL* kuorong(SListint x)
{
	SL* newnode = (SL*)malloc(sizeof(SL));
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
//尾插
void SlistPushBank(SL** plist, SListint x)
{
	assert(plist);
	SL* newnode = kuorong(x);
	if (*plist == NULL)
	{
		*plist = newnode;
	}
	else 
	{ 
		SL* pcur = *plist; 
		while (pcur->next != NULL)
		{
			pcur = pcur->next;
		}
		pcur->next = newnode;
	}
}
//头插
void SlistPushFront(SL** plist, SListint x)
{
	assert(plist);
	SL* newnode = kuorong(x);
	newnode->next = *plist;
	*plist = newnode;
}
//打印
void Slist_print(SL** plist)
{
	SL* ret = *plist;
	while (ret)//ret != NULL
	{
		printf("%d->", ret->data);
		ret = ret->next;
	}
	printf("NULL\n");
}
//尾删
void SlistPopBank(SL** plist)
{
	assert(*plist);//*plist == NULL   1
	if ((*plist)->next == NULL)//    2
	{
		free(*plist);
		*plist = NULL;
	}
	else                      //     3
	{
		SL* ret = *plist;
		SL* pcur = *plist;
		while (ret->next != NULL)
		{
			pcur = ret;
			ret = ret->next;
		}
		free(ret);
		pcur->next = NULL;
	}
}
//头删
void SlistPopFront(SL** plist)
{
	assert(*plist);
	if ((*plist)->next == NULL)
	{
		free(*plist);
	}
	else
	{
		SL* ret = (*plist)->next;
		free(*plist);
		*plist = ret;
	}
}
//查找
SL* SlistFind(SL* plist, SListint x)
{
	SL* ret = plist;
	while (ret)
	{
		if (x == ret->data)
		{
			return ret;
		}
		ret = ret->next;
	}
	return NULL;
}
//在pos之前插入数据
void SlistInsert(SL** plist, SL* pos, SListint x)
{
	assert(plist);
	if (pos == *plist)
	{
		SlistPushFront(plist, x);//如果只有一个数值,那么调用头插,不然会报错
	}
	else
	{
		SL* ret = *plist;
		SL* newnode = kuorong(x);
		while (ret->next != pos)
		{
			ret = ret->next;
		}
		newnode->next = ret->next;
		ret->next = newnode;
	}
}
//删除pos位置的值
void SListFrase(SL** plist, SL* pos)
{
	assert(plist);
	if (pos == *plist)
	{
		SlistPopFront(plist);
	}
	else
	{
		SL* ret = *plist;
		while (ret->next != pos)
		{
			ret = ret->next;
		}
		ret->next = pos->next;
		free(pos);
	}
}
//销毁
void SlistDestroyed(SL** plist)
{
	SL* ret = *plist;
	while (ret)
	{
		SL* cur = ret->next;
		free(ret);
		ret = cur;
	}
	*plist = NULL;
}

3.测试test.c的代码
 

#define _CRT_SECURE_NO_WARNINGS 1
#include"Slist.h"
void menu()
{
	printf("*************************\n");
	printf("1.尾插             2.头插\n");
	printf("3.尾删             4.头删\n");
	printf("5.打印链表     6.查找元素\n");
	printf("7pos前插入      8.删除pos\n");
	printf("9.销毁        10.退出菜单\n");
	printf("*************************\n");
}
int main()
{

	int input = 0, x = 0, y = 0;
	SL* plist = NULL;
	do
	{
		menu();
		printf("请输入你的选择\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("\n请输入尾插的数值,输入-1结束\n");
			do
			{
				scanf("%d", &x);
				if (x != -1)
				{
					SlistPushBank(&plist, x);
				}
			} while (x != -1);
			break;
		case 2:
			printf("请输入头插的数值,输入-1结束\n");
			do
			{
				scanf("%d", &x);
				if (x != -1)
				{
					SlistPushFront(&plist, x);
				}
			} while (x != -1);
			break;
		case 3:
			SlistPopBank(&plist);
			printf("尾删成功\n");
			break;
		case 4:
			SlistPopFront(&plist);
			printf("头删成功\n");
			break;
		case 5:
			Slist_print(&plist);
			break;
		case 6:
			printf("请输入你要查找的元素\n");
			scanf("%d", &x);
			SL* ret = SlistFind(plist,x);
			if (ret != NULL)
			{
				printf("找到了\n");
			}
			else
			{
				printf("没找到\n");
			}
			break;
		case 7:
			printf("请输入你要在哪个pos之前插入\n");
			scanf("%d", &x);
			SL* retpos1 = SlistFind(plist,x);
			if (retpos1 == NULL)
			{
				printf("要插入的pos数值不存在\n");
				exit(1);
			}
			else 
			{
				printf("请输入要插入的数值\n");
				scanf("%d", &y);
				SlistInsert(&plist, retpos1, y);
				printf("插入成功\n");
			}
			break;
		case 8:
			printf("请输入你要删除的pos数值\n");
			scanf("%d", &x);
			SL* retpos2 = SlistFind(plist,x);
			if (retpos2 == NULL)
			{
				printf("要删除的pos节点不存在\n");
				exit(1);
			}
			else
			{
				SListFrase(&plist, retpos2);
				printf("删除成功\n");
			}
			break;
		case 9:
			SlistDestroyed(&plist);
			printf("销毁成功\n");
			break;
		case 10:
			input = -1;
			printf("退出中....\n");
			break;
		default:
			printf("请选择1-10的方法\n");
			break;
		}
	} while (input != -1);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值