#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;
}