数据结构-单链表的增删改查

SList.h

#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>

typedef int SLDateType;

typedef struct SListNode
{
	SLDateType data;
	struct SListNode* next;
}SLNode;

void SLPrint(SLNode* phead);//链表的打印
void SLPushFront(SLNode**pphead,SLDateType x);//链表的头插法
void SLPushBack(SLNode** pphead, SLDateType x);//链表的尾插法
void SLPopFront(SLNode** pphead);//链表的头删
void SLPopBack(SLNode** pphead);//链表的尾删
SLNode* SLSearch(SLNode* phead, SLDateType x);//链表的查找
void SLInsert(SLNode** pphead, SLNode* pos, SLDateType x);//在pos之前插入
void SLInsertAfter(SLNode** pphead, SLNode* pos, SLDateType x);//在pos后插入
void SLErase(SLNode** pphead, SLNode*pos);//删除pos位置的数据
void SLEraseAfter(SLNode* pos);//删除pos之后的数据
void SLDestroy(SLNode** pphead);//删除单链表

SList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SList.h"

void SLPrint(SLNode* phead)
{
	SLNode* cur = phead;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

SLNode* BuyNode(SLDateType x)
{
	SLNode *newnode = (SLNode*)malloc(sizeof(SLNode));
	if (newnode == NULL)
	{
		perror("malloc fail ::");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
}

void SLPushFront(SLNode** pphead, SLDateType x)
{
	SLNode* newnode = BuyNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

void SLPushBack(SLNode** pphead, SLDateType x)
{
	SLNode* newnode = BuyNode(x);
	//空链表
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	//非空链表
	else
	{
		SLNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

void SLPopFront(SLNode** pphead)
{
	assert(*pphead);
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLNode* del = *pphead;
		*pphead = (*pphead)->next;
		free(del);
	}
}

void SLPopBack(SLNode** pphead)
{
	assert(*pphead);
	if ((*pphead)->next->next == NULL)
	{
		*pphead = NULL;
	}
	else
	{
		SLNode* tail = *pphead;
		while (tail->next->next != NULL)
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}

SLNode* SLSearch(SLNode* phead, SLDateType x)
{
	if (phead == NULL)
		return NULL;
	else
	{
		SLNode* search = phead;
		while (search)
		{
			if (search->data == x)
			{
				return search;
			}
			search = search->next;
		}
		return NULL;
	}
}

void SLInsert(SLNode** pphead, SLNode* pos, SLDateType x)
{
	assert(pphead&&pos);
	SLNode* prev = *pphead;
	if (*pphead == pos)
		SLPushFront(pphead, x);
	else 
	{
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		SLNode* newnode = BuyNode(x);
		newnode->next = pos;
		prev->next = newnode;
	}
}

void SLInsertAfter(SLNode** pphead, SLNode* pos, SLDateType x)
{
	assert(pphead && pos);
	SLNode* newnode = BuyNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

void SLErase(SLNode** pphead, SLNode* pos)
{
	assert(pphead && pos);
	if (pos == *pphead)
	{
		SLPopFront(pphead);
	}
	else
	{
		SLNode* prev = *pphead;
		while (prev->next != pos) 
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
	}
}

void SLEraseAfter(SLNode* pos)
{
	assert(pos->next);
	SLNode* next = pos->next;
	pos->next = pos->next->next;
	free(next);
}

void SLDestroy(SLNode** pphead)
{
	assert(pphead);
	SLNode* cur = *pphead;
	while (cur)
	{
		SLNode* next = cur->next;
		free(cur);
		cur = next;
	} 
	*pphead = NULL;
}


test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SList.h"

void TestSList1()
{
	SLNode* plist = NULL;
	SLPushFront(&plist, 0);
	SLPushFront(&plist, 7);
	SLPushFront(&plist, 2);
	SLPushFront(&plist, 0);
	SLPrint(plist);
}

void TestSList2()
{
	SLNode* plist = NULL;
	SLPushBack(&plist, 0);
	SLPushBack(&plist, 7);
	SLPushBack(&plist, 2);
	SLPushBack(&plist, 0);
	SLPrint(plist);
}

void TestSList3()
{
	SLNode* plist = NULL;
	SLPushBack(&plist, 0);
	SLPushBack(&plist, 7);
	SLPushBack(&plist, 2);
	SLPushBack(&plist, 0);
	SLPopFront(&plist);
	SLPrint(plist);
}

void TestSList4()
{
	SLNode* plist = NULL;
	SLPushFront(&plist, 0);
	SLPushFront(&plist, 7);
	SLPushFront(&plist, 2);
	SLPushFront(&plist, 0);
	printf("%p\n", SLSearch(plist, 7));
}

void TestSList5()
{
	SLNode* plist = NULL;
	SLPushBack(&plist, 0);
	SLPushBack(&plist, 7);
	SLPushBack(&plist, 2);
	SLPushBack(&plist, 0);
	SLInsert(&plist, SLSearch(plist, 7), 6);
	SLInsertAfter(&plist, SLSearch(plist, 2), 6);
	SLErase(&plist, SLSearch(plist, 6));
	SLEraseAfter(SLSearch(plist, 2));
	SLDestroy(&plist);
}

int main()
{
	TestSList5();
	return 0;
}

测试函数的功能时最好写成test函数形式,不要写成菜单形式,便于调试。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值