c语言单链表的实现和各个功能(增删查改)

头文件

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct MyStruct
{
    int data;
    struct MyStruct* next;
}SL;
void listprint(SL** phead);//打印链表
void listpushback(SL** phead, int x);//尾插链表
void listpopback(SL** phead);//尾删链表
void listpushfront(SL** phead, int x);//头插链表
void listpopfront(SL** phead);//头删链表
SL* listfind(SL** phead, int x);//查找某个数
void listinsert(SL** phead, SL* pos,int x);//插入一个数
void listerase(SL** phead, SL* pos);//任意位置删除目标
void listdestory(SL** phead);//销毁链表

主函数

#include"list.h"
void listprint(SL **phead)//打印链表
{
	SL* cur = *phead;
	while(cur != NULL)
	{
		printf("%d", cur->data);
		cur = cur->next;
	}
}
void listpushback(SL**phead,int x)//尾插链表
{
	SL* newnode = (SL*) malloc(sizeof(SL));
	if (newnode == NULL)
	{
		printf("malloc,error");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (*phead == NULL)
	{
		*phead = newnode;
	}
	else
	{
		SL* tail = *phead;
		while(tail->next!=NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}
void listpopback(SL** phead) // 尾删链表
{
	assert(*phead);
	SL* aheadtail = *phead;
	SL* tail = aheadtail->next;
	while (tail->next != NULL)
	{
		tail = tail->next;
		aheadtail = aheadtail->next;
	}
	free(tail);
	tail = NULL;
	aheadtail->next = NULL;

}
void listpushfront(SL** phead, int x)
{
	SL* newnode = (SL*)malloc(sizeof(SL));
	if (newnode == NULL)
	{
		printf("malloc,error");
		exit(-1);
	}
	SL* fronthead;
	newnode->data = x;
	if (*phead == NULL)
		*phead = newnode;
	else
	{
		fronthead = *phead;
		newnode->next = fronthead;
		*phead = newnode;
	}
}
void listpopfront(SL** phead) {

	assert(*phead);
	SL* delphead= *phead;
	delphead = delphead->next;
	free(*phead);
	*phead = delphead;

}
SL* listfind(SL** phead, int x)

{
	SL* newnode = *phead;
	assert(*phead);
	while (*phead)
		if (newnode->data == x)
			return newnode;
		else
		{   
			newnode = newnode->next;
			if (newnode == NULL)
				return newnode;
		}
}
void listinsert(SL** phead, SL* pos, int x)
{
	assert(*phead);
	SL* find=NULL;
	SL* newnode = (SL*)malloc(sizeof(SL));
	if (newnode == NULL)
	{
		printf("malloc,error");
		exit(-1);
	}
	newnode->data = x;
	if (*phead == pos)  // 使用比较运算符
		{
			listpushfront(phead, x);
		}
	else
	{
		find = *phead;
		while (find->next != pos)
		{
			find = find->next;

		}
		newnode->next = find->next;
		find->next = newnode;
	}
			}
void listerase(SL** phead, SL* pos)//任意位置删除目标
{
	if (*phead == pos)
	{
		listpopfront(phead);

	}
	else
	{
		SL* prev = *phead;
		while (prev->next != pos)
		{

			prev = prev->next;


		}
		prev->next = pos->next;
		free(pos);
		pos = NULL;
	}

}
void listdestory(SL** phead)//销毁链表
{
	assert(*phead);
	SL* cur = *phead;
	while (cur)
	{
		
		*phead = cur->next;
		free(cur);
		cur = *phead;

	}
	cur = NULL;
	*phead = NULL;
	printf("销毁成功");

}

测试

#include"list.h"
void test()
{
	SL* plist = NULL;
	listpushback(&plist, 1);
	listpushback(&plist, 3);
	listpushback(&plist, 2);
	listpushfront(&plist, 5);
	SL* pos = listfind(&plist, 5);
	listinsert(&plist, pos, 8);

	listprint(&plist);
	listdestory(&plist);

}
int main()
{
	test();


}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值