单向链表的基本操作

链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
在这里插入图片描述
链表的操作包括增删查改。

test.c

#include "SList.h"
void main()
{

	SListNode* pList = NULL;
	SListPushBack(&pList,1);
	SListPushBack(&pList,2);
	SListPushBack(&pList,3);
	SListPushBack(&pList,4);
	SListPrint(pList);
	SListPopBack(&pList);
	SListPrint(pList);
	SListPopBack(&pList);
	SListPrint(pList);
	SListPopBack(&pList);
	SListPrint(pList);
	SListPopBack(&pList);
	SListPrint(pList);
	SListPopBack(&pList);
	SListPrint(pList);
	SListPushFront(&pList,3);
	SListPushFront(&pList, 3);
	SListPushFront(&pList, 2);
	SListPushFront(&pList, 3);
	SListPrint(pList);
	SListPopFront(&pList);
	SListPrint(pList);
	SListNode* pos = SListFind(pList,3);
	if (pos)
	{
		pos->data = 30;
	}
	SListPrint(pList);
	SListInsertAfter(pList->next, 10);
	SListPrint(pList);
	SListErase(pList->next);
	SListPrint(pList);
	

}

SList.h

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SListDataType;
typedef struct SListNode
{
	SListDataType data;
	struct SListNode* next;
}SListNode;
void SListPushBack(SListNode** phead,SListDataType x);// 动态申请一个结点
void SListPopBack(SListNode** phead);//单链表尾删
void SListPushFront(SListNode** phead,SListDataType x);//单链表头插
void SListPopFront(SListNode** phead);//单链表头删
void SListPrint(SListNode* phead);//单链表打印
SListNode* BuySListNode(SListDataType x);// 动态申请一个结点
SListNode* SListFind(SListNode* phead, SListDataType x);//单链表查找
void SListInsertAfter(SListNode* pos, SListDataType x);//在pos位置后插入
void SListErase(SListNode* pos);//在pos位置后删除

SList.c

#include "SList.h"
SListNode* BuySListNode(SListDataType x)
{
	SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
	if (newNode == NULL)
	{
		printf("申请内存失败");
		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("\n");
}
void SListPushBack(SListNode** phead,SListDataType x)
{
	SListNode* newNode = BuySListNode(x);
	if (*phead == NULL)
	{
		*phead = newNode;
	}
	else
	{
		SListNode* tail = *phead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newNode;
	}
}
void SListPopBack(SListNode** phead)
{
	//空链表
	if (*phead == NULL)
	{
		return;
	}
	//一个
	else if ((*phead)->next==NULL)
	{
		free(*phead);
		*phead = NULL;
	}
	//大于1个
	else
	{
		SListNode* tail = *phead;
		SListNode* prev = NULL;
		while (tail->next!=NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		free(tail);
		prev->next = NULL;
	}
}
//单链表查找
void SListPushFront(SListNode** phead, SListDataType x)
{
	SListNode* newNode = BuySListNode(x);
	newNode->next = *phead;
	*phead = newNode;
}
void SListPopFront(SListNode** phead)
{
	if (*phead==NULL)
	{
		return;
	}
	else
	{
		SListNode* next = (*phead)->next;
		free((*phead));
		*phead = next; 
	}
}
SListNode* SListFind(SListNode* phead, SListDataType x)
{
	SListNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SListInsertAfter(SListNode* pos, SListDataType x)
{
	assert(pos);
	SListNode* newnode = BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}
void SListErase(SListNode* pos)
{
	assert(pos);
	if (pos->next)
	{
		SListNode* next = pos->next;
		SListNode* nextnext = next->next;
		pos->next = next->next;
		free(next);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值