#include
#include
#include
struct stus
{
int date;
struct stus *next;
};
int output(struct stus *first)
{
struct stus *p;
p = first;
while(p)
{
printf("%d\n",p->date);
p = p->next;
}
return 0;//输出成功
}
int insert(struct stus *first,int n,int date)
{
struct stus *tmp,*stunew,*p;
stunew = (struct stus *)malloc(sizeof(struct stus));
tmp = first;
p = NULL;
n--;
while(tmp && n--)
{
p = tmp;
tmp = tmp->next;
}
if(tmp == NULL)
{
stunew->date = date;
p->next = stunew;
stunew->next = NULL;
return -1;
}
stunew->date = date;
stunew->next = p->next;
p->next = stunew;
return 0;//插入成功
}
int del(struct stus *first,int n)
{
struct stus *tmp = NULL,*p = NULL;
tmp = first;
while(tmp && n--)
{
printf("循环体\n");
p = tmp;
tmp = tmp->next;
}
if(tmp == NULL)
{
printf("选择删除位置不对\n");
return -1;
}
if(tmp->next == NULL)
{
p->next = NULL;
free(tmp);
printf("选择删除尾节点\n");
return 1;//删除尾节点
}
printf("删除操作\n");
p->next = tmp->next;
free(tmp);
return 0;//删除成功
}
int find(struct stus *first,int date)
{
struct stus *tmp = first;
while(tmp)
{
if(tmp->date == date)
{
printf("%d\n--------看到你了----------\n",tmp->date);
return 0;
}
tmp = tmp->next;
}
if(tmp == NULL)
{
printf("没有这个数\n-------------\n");
return NULL;
}
}
int main()
{
int i,j,k;
struct stus stu1,stu2,stu3,*p,*head;
stu1.date = 100;
stu2.date = 1000;
stu3.date = 10000;
head = &stu1;
stu1.next = &stu2;
stu2.next = &stu3;
stu3.next = NULL;
insert(&stu1,3,22);
find(&stu1,22);
printf("\n------------------\n");
output(&stu1);
printf("\n------------------\n");
del(&stu1,1);
output(&stu1);
}
输出结果: