简单的单链表实现 c

#ifndef SINGLE_LIST
#define SINGLE_LIST
#include<stdlib.h>
#include<stdio.h>

typedef int elemtype;
struct LIST;
typedef LIST *list;
typedef list item;
#define error 0;
struct LIST 
{
	item next;
	elemtype data;
};

//初始化链表
list initial();
//找到制定值得前一个节点
item list_find(list l, elemtype x);
//删除有指定值得节点
int list_delete(list l, elemtype node);
//在节点后插入
int list_insert(list l,item node,elemtype x);
//打印
int list_print(list l);
#endif

#include"singleList.h"

list initial()
{
	list l=new LIST;
	if(l==NULL)
		return error;
	l->next=NULL;
	return l;
}

item list_findprevious(list l, elemtype x)
{
	item tmp=l;
	while(tmp->next)
	{
		if(tmp->next->data==x)
			return tmp;
		else 
			tmp=tmp->next;
	}
	return 0;
}


int list_delete(list l, elemtype x)
{
	item tmp=list_findprevious(l,x);
	if(tmp==NULL)
		return NULL;
	item delitem=tmp->next;
	tmp->next=delitem->next;
	delete(delitem);
	return 1;
}

int list_insert(list l,item node,elemtype x)
{
	item itemadd=new LIST;
	if(itemadd==NULL)
		return 0;
	itemadd->data=x;
    itemadd->next=node->next;
	node->next=itemadd;
	printf("\ndone");
	return 1;
	
}

int list_print(list l)
{
	item tmp=l->next;
	while(tmp)
	{
		printf("\n%d",tmp->data);
		tmp=tmp->next;
	}
	getchar();
	return 1;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值