c语言单链表模板

首先准备如下3个文件:

nodelist.h:链表结构,函数声明

nodelist.c:函数定义

test.c:主函数  /  测试

nodelist.h

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



typedef int datatype;


typedef struct listnode       
{
	datatype a;              
	struct listnode* next;

}listnode;

typedef listnode* nodep;


void printlist(nodep lty);

nodep supply(datatype a);
 
void addback(nodep* lty,datatype a);

void addfront(nodep* lty, datatype a);

void deleteback(nodep* lty);

void deletehead(nodep* lty);

nodep finddata(nodep lty, datatype a);

void addanypos(nodep* lty,int pos, datatype a);

void deleteanypos(nodep* lty, int pos);

void destory(nodep* lty);

nodelist.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"nodelist.h"


void printlist(nodep lty)  
{
	if (lty == NULL)
	{
		printf("无数据");
		return;
	}
	nodep temp = lty;
	while (temp)
	{
		printf("%d ", temp->a);
		temp = temp->next;
	}
	printf("\n");
}

nodep supply(datatype a)
{
	nodep lty = (nodep)malloc(sizeof(listnode));
	if (lty == NULL)
	{
		perror("mallocfail");
		return;
	}
	lty->a = a;
	lty->next = NULL;
	return lty;
}


void addback(nodep* lty, datatype a)
{
	assert(lty);
	nodep temp = supply(a);
		if (*lty == NULL)
		{
			*lty = temp;
			return;
		}
		if (*lty != NULL)
		{
			nodep tem = *lty;
			while (tem->next)
			{
				tem = tem->next;
			}
			tem->next = temp;
		}
}


void addfront(nodep* lty, datatype a)
{
	assert(lty);
	nodep temp = supply(a);
	if (*lty == NULL)
	{
		*lty = temp;
		return;
	}
	if (*lty != NULL)
	{
		nodep tem = *lty;
		*lty= temp;
		(*lty)->next = tem;
	}
}

void deleteback(nodep* lty)
{
	assert(lty);
	if (*lty == NULL)
	{
		printf("无数据,删除失败");
		return;
	}
	if ((*lty)->next == NULL)
	{
		free(*lty);
		*lty = NULL;
		return;
	}
	nodep temp = *lty;
	nodep front_1 = NULL;
	while (temp->next)
	{
		front_1 = temp;
		temp = temp->next;
	}
	front_1->next = NULL;
	free(temp);
	temp = NULL;
}

void deletehead(nodep* lty)
{
	assert(lty);
	if (*lty == NULL)
	{
		printf("无数据,删除失败");
		return;
	}
	nodep tempnext = (*lty)->next;
	nodep first = *lty;
	free(first);
	first = NULL;
	*lty = tempnext;
}

nodep finddata(nodep lty, datatype a)
{
	if (lty == NULL)
	{
		printf("无数据");
		return NULL;
	}
	nodep temp = lty;
	while (temp)
	{
		if (temp->a == a)
		{
			printf("找到了\n");
			return temp;
		}
		temp = temp->next;
	}
	printf("找不到数据");
	return NULL;
}



void addanypos(nodep* lty,int pos,datatype a)
{
	assert(lty);
	nodep temp = supply(a);
	if (*lty == NULL)
	{
		*lty = temp;
		return;
	}
	if (pos <= 1)
	{
		addfront(lty, a);
		return;
	}
	if (*lty != NULL)
	{
		nodep tempp = *lty;
		int y = pos - 2;
		while(y--)
		{
			tempp = tempp->next;
			if (tempp->next == NULL)
			{
				addback(lty, a);
				return;
			}
		}
		nodep temppp = tempp->next;
		tempp->next = temp;
		temp->next = temppp;
	}
} 

void deleteanypos(nodep* lty,int pos)
{
	assert(lty);
	if (*lty == NULL)
	{
		printf("无数据");
		return;
	}
	if ((*lty)->next == NULL)
	{
		free(*lty);
		*lty = NULL;
		return;
	}
	nodep temp = *lty;
	int t = pos - 2;
	while (t--)
	{
		temp = temp->next;
		if (temp->next == NULL)
		{
			deletepop(lty);
		}
	}
	nodep newnext = temp->next->next;
	nodep deletenode = temp->next;
	free(deletenode);
	deletenode = NULL;
	temp->next = newnext;
}


void destory(nodep* lty)
{
	assert(lty);
	if ((*lty) == NULL)
	{
		printf("无数据");
		return;
	}
	if ((*lty)->next==NULL)
	{
		free(*lty);
		*lty = NULL;
	}
	else
	{
		nodep temp = *lty;
		while (temp)
		{
			nodep nextnode = temp->next;
			free(temp);
			temp = nextnode;
		}
	}
	*lty = NULL;
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"nodelist.h"

void test()
{





}


int main()
{
	test();

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值