链表增删查找操作

目录

头文件

实现文件

测试文件


头文件

#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLdatatype;
typedef struct SeQlist
{
	SLdatatype data;
	struct SeQlist* next;
}SeQlist;
void print(SeQlist* pphead);
void pushpront(SeQlist** pphead, SLdatatype x);
void pushback(SeQlist** pphead, SLdatatype x);
void poppront(SeQlist** pphead);
void popback(SeQlist** pphead);
void SLinsert(SeQlist** pphead, SeQlist*pos,SLdatatype x);
void SLinsertafter( SeQlist* pos, SLdatatype x);
void SLerase(SeQlist** pphead, SeQlist* pos);
SeQlist* SLfind(SeQlist* pphead, SLdatatype x);
void SLEraseafter( SeQlist* pos);
void SLDestroy(SeQlist* pphead);

实现文件 

#include "SeQlist.h"
void print(SeQlist* pphead)
{
	while (pphead)
	{
		printf("%d->", pphead->data);
		pphead = pphead->next;
	}
	printf("NULL\n");
}
SeQlist* SLbuynode(SLdatatype x)
{
	SeQlist* newnode = (SeQlist*)malloc(sizeof(SeQlist));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void pushpront(SeQlist** pphead,SLdatatype x)
{
	assert(pphead);
	SeQlist* newnode = SLbuynode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}
void pushback(SeQlist** pphead, SLdatatype x)
{
	assert(pphead);
	SeQlist* newnode = SLbuynode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SeQlist* ptail = *pphead;
		while (ptail->next)
		{
			ptail = ptail->next;
		}
		ptail->next = newnode;
	}
}
void poppront(SeQlist** pphead)
{
	assert(pphead && *pphead);
	SeQlist* next = (*pphead)->next ;
	free(*pphead);
	*pphead = next;
}
void popback(SeQlist** pphead)
{
	assert(pphead && *pphead);
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SeQlist* ptail = *pphead;
		SeQlist* prv = *pphead;
		while (ptail->next)
		{
			prv = ptail;
			ptail = ptail->next;
		}
		free(ptail);
		ptail = NULL;
		prv->next = NULL;
	}
	
}
void SLinsert(SeQlist** pphead, SeQlist* pos, SLdatatype x)
{
	assert(pphead);
	assert(pos);
	SeQlist* newnode = SLbuynode(x);
	if (pos == *pphead)
	{
		pushpront(pphead, x);
	}
	else
	{
		SeQlist* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		newnode->next = prev->next;
		prev->next = newnode;
	}
}
void SLinsertafter( SeQlist* pos, SLdatatype x)
{
	assert(pos);
	SeQlist* newnode = SLbuynode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}
void SLerase(SeQlist** pphead, SeQlist* pos)
{
	assert(pphead && *pphead);
	assert(pos);
	if (pos == *pphead)
	{
		poppront(pphead);
	}
	else
	{
		SeQlist* prv = *pphead;
		while (prv->next !=pos)
		{
			prv = prv->next;
		}
		prv->next = pos->next;
		free(pos);
		pos = NULL;
	}
}
SeQlist* SLfind(SeQlist* pphead, SLdatatype x)
{
	assert(pphead);
	while (pphead)
	{
		if (pphead->data == x)
		{
			return pphead;
		}
		pphead = pphead->next;
	}
	return NULL;
}
void SLEraseafter( SeQlist* pos)
{
	assert(pos&&pos->next);
	SeQlist* del = pos->next;
	pos->next = del->next;
	free(del);
	del= NULL;
}
void SLDestroy(SeQlist** pphead)//一个一个销毁
{
	assert(pphead && *pphead);
	SeQlist* pcur = *pphead;
	while (pcur)
	{
		SeQlist* next = pcur;
		next = pcur->next;
		free(pcur);
		pcur = next;
	}
	*pphead = NULL;
}



测试文件

 

#include "SeQlist.h"
void test01()
{
	SeQlist* node1 = (SeQlist*)malloc(sizeof(SeQlist));
	node1->data = 1;
	SeQlist* node2 = (SeQlist*)malloc(sizeof(SeQlist));
	node2->data = 2;
	SeQlist* node3 = (SeQlist*)malloc(sizeof(SeQlist));
	node3->data = 3;
	SeQlist* node4 = (SeQlist*)malloc(sizeof(SeQlist));
	node4->data = 4;
	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	node4->next = NULL;
	SeQlist* plist = node1;
	print(plist);//1 2 3 4
	pushpront(&plist, 7);
	print(plist);//7 1 2 3 4
	pushback(&plist, 9);
	print(plist);//7 1 2 3 4 9
	poppront(&plist);
	print(plist);//1 2 3 4 9
	popback(&plist);
	print(plist);//1 2 3 4
	SLinsert(&plist, node2, 99);
	print(plist);//1 99 2 3 4
	SLinsertafter(node3, 89);
	print(plist);//1 99 2 3 89 4
	SLerase(&plist, node4);
	print(plist);//1 99 2 3 89
	SeQlist*find= SLfind(plist, 2);
	if (find == NULL)
	{
		printf("没有找到\n");
	}
	else
	{
		printf("找到了\n");
	}
	SLEraseafter(find);
	print(plist);//1 99 2 89
}
int main()
{
	test01();
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值