单链表基本操作

看:

#include<stdio.h>
#include<stdlib.h>

typedef int SListDataType;
//结点
typedef struct SListNode
{
	SListDataType data;
	struct SListNode* next;
}SListNode;
SListNode* BuySListNode(SListDataType x)
{
	SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));//申请结点并存储数据
	if (newNode == NULL)
	{
		printf("申请结点失败\n");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;

	return newNode;
}
void SListPrint(SListNode* phead)
{
	SListNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL");
	printf("\n");
}
void SListPushBack(SListNode** pphead, SListDataType x)//尾加
{
	if (*pphead == NULL)//原链表为空
	{
		SListNode* newNode = BuySListNode(x);
		*pphead = newNode;
	}
	else//原链表不为空,找尾
	{
		SListNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		SListNode* newNode = BuySListNode(x);
		tail->next = newNode;
	}
}
void SListPopBack(SListNode** pphead)
{
	//(1)空链表
	if (*pphead == NULL)
	{
		return;
	}
	//一个结点
	else if ((*pphead)->next == NULL)
	{
		free(*pphead);//释放地址为*pphead的空间;
		*pphead = NULL;//让指针*pphead指向NULL;
	}
	//一个以上结点
	else
	{
		SListNode* prev = NULL;//用于存放删减结点前面的结点的地址
		SListNode* tail = *pphead;
		while (tail->next != NULL)
		{ 
			prev = tail;
			tail = tail->next;
		}
		free(tail);
		//tail = NULL;
		prev->next = NULL;
	}
}
void SListPushFront(SListNode** pphead, SListDataType x)//头加
{
	SListNode* newNode = BuySListNode(x);
	newNode->next = *pphead;
	*pphead = newNode;
}
void SListPopFront(SListNode** pphead)//头删
{
	if (*pphead == NULL)
	{
		return;
	}
	else
	{
		SListNode* next = (*pphead)->next;
		free(*pphead);
		*pphead = next;
	}
}
SListNode* SListFind(SListNode* phead, SListDataType x)//查找数据x,并返回它的指针;
{
	SListNode* cur = phead;
	while (cur != NULL)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
/*
void SListInserAfter(SListNode* pos, SListDataType x)//在pos处后面插入数据
{
	SListNode* newNode = BuySListNode(x);
	SListNode* next=pos->next;
	pos->next=newNode;
	newNode->next=next;//原理和交换两个数的值差不多(在给一个变量赋另一个值时,如果原来的值有用,就得事先保存);
}
void SListEraseAfter(SListNode* pos, SListDataType x)//删除pos处后面数据
{
	SListNode* next = pos->next;
	SListNode* nextnext = next->next;
	free(next);
	pos->next = nextnext;
}
*/
void test1()
{
	SListNode* pList = NULL;//永远指向链表的第一个结点;

	SListPushBack(&pList, 1);
	SListPushBack(&pList, 2);
	SListPushBack(&pList, 3);
	SListPushBack(&pList, 4);
	SListPrint(pList);


	SListPopBack(&pList);
	SListPopBack(&pList);
	SListPopBack(&pList);
	SListPopBack(&pList);
	SListPopBack(&pList);
	SListPrint(pList);

	SListPushFront(&pList, 1);
	SListPushFront(&pList, 2);
	SListPushFront(&pList, 3);
	SListPushFront(&pList, 4);
	SListPushFront(&pList, 5);
	SListPrint(pList);

	SListPopFront(&pList);
	SListPopFront(&pList);
	SListPopFront(&pList);
	SListPopFront(&pList);
	SListPopFront(&pList);
	SListPrint(pList);

	SListPushBack(&pList, 1);
	SListPushBack(&pList, 2);
	SListPushBack(&pList, 3);
	SListPushBack(&pList, 4);
	SListPrint(pList);
	SListNode* pos = SListFind(pList, 3);
	if (pos !=NULL)
	{
		pos->data = 30;
	}
	SListPrint(pList);
}
int main()
{
	test1();
	//system("pause");
	return 0;
}

无语!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值