C语言 实现单向链表的基本操作

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node
{
	int id;
	struct Node* next;
}Node;

//创建头结点
//链表的头结点地址由函数值返回
Node* SListCreat()
{
	Node* head = NULL;

	//头结点作为标志,不存储有效数据
	head = (Node*)malloc(sizeof(Node));
	//给head的成员变量赋值
	head->id = -1;
	head->next = NULL;

	Node* pCur = head;
	Node* pNew = NULL;

	int data;

	while (1)
	{
		printf("请输入数据:");
		scanf("%d", &data);

		if (data == -1)//输入-1退出
		{
			break;
		}

		//新结点动态分配空间
		pNew = (Node*)malloc(sizeof(Node));
		if (pNew == NULL)
		{
			continue;
		}

		//给pNew成员变量赋值
		pNew->id = data;
		pNew->next = NULL;

		//链表建立关系

		//当前结点指向pNew
		pCur->next = pNew;

		//pNew下一结点指向NULL
		pNew->next = NULL;

		//把pCur移动到pNew,即pCur指向pNew
		pCur = pNew;
	}

	return head;
}

//链表的遍历
int SListPrint(Node* head)
{
	if (head == NULL)
	{
		return -1;
	}

	//取出第一个有效结点(头结点的next)
	Node* pCur = head->next;

	printf("head -> ");
	while (pCur != NULL)
	{
		printf("%d -> ", pCur->id);

		//当前结点往下移动一位,pCur指向下一个
		pCur = pCur->next;
	}
	printf("NULL\n");

	return 0;
}

int SListNodeInsert(Node* head, int x, int y)
{
	if (head == NULL)
	{
		return -1;
	}
	Node* pPre = head;
	Node* pCur = head->next;

	while (pCur != NULL)
	{
		if (pCur->id == x)
		{
			break;
		}

		pPre = pCur;
		pCur = pCur->next;
	}

	//2种情况
	//1、找到了匹配的结点,pCur为匹配节点,pPre为pCur的上一结点
	//2、没有找到匹配节点,pCur为空结点,pPre为最后一个结点

	//给新结点动态分配空间
	Node* pNew = (Node*)malloc(sizeof(Node));
	if (pNew == NULL)
	{
		return -2;
	}
	//给pNew成员变量赋值
	pNew->id = y;
	pNew->next = NULL;

	//插入指定位置
	pPre->next = pNew;
	pNew->next = pCur;

	return 0;
}

//删除第一个值为x的结点
int SListNodeDel(Node* head, int x)
{
	if (head == NULL)
	{
		return -1;
	}
	Node* pPre = head;
	Node* pCur = head->next;
	int flag = 0;//0代表没找到,1代表找到了

	while (pCur != NULL)
	{
		if (pCur->id == x)
		{
			flag = 1;

			pPre->next = pCur->next;
			free(pCur);
			pCur = NULL;

			break;
		}

		pPre = pCur;
		pCur = pCur->next;
	}

	//2种情况
	//1、找到了匹配的结点,pCur为匹配节点,pPre为pCur的上一结点
	//2、没有找到匹配节点,pCur为空结点,pPre为最后一个结点
	if (0 == flag)
	{
		printf("没有找到值为%d的结点\n", x);
		return -3;
	}

	return 0;
}

//清空链表,释放所有结点
int SListDestroy(Node* head)
{
	if (head == NULL)
	{
		return -1;
	}

	int i = 0;
	Node* tmp = NULL;

	while (head != NULL)
	{
		tmp = head->next;
		free(head);
		head = NULL;

		head = tmp;
		i++;
	}
	printf("共释放%d个结点", i);
	
	return 0;
}

int main()
{
	Node* head = NULL;
	head = SListCreat();//创建头结点
	SListPrint(head);

	SListNodeInsert(head, 5, 4);
	printf("在5的前面插入4;若没有5,则尾插\n");
	SListPrint(head);

	SListNodeDel(head, 5);
	printf("删除5结点后\n");
	SListPrint(head);

	SListDestroy(head);
	head = NULL;

	printf("\n");
	system("pause");
	return 0;
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

banjitino

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

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

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

打赏作者

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

抵扣说明:

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

余额充值