#include "myhead.h"
struct siglelist
{
int data;
struct siglelist *next;
};
struct siglelist *list_init()
{
struct siglelist *myhead=malloc(sizeof(struct siglelist));
myhead->next=NULL;
return myhead;
}
int insert_tail(int newdata,struct siglelist *head)
{
struct siglelist *p=head;
while(p->next!=NULL)
p=p->next;
struct siglelist *newnode=malloc(sizeof(struct siglelist));
newnode->data=newdata;
newnode->next=NULL;
p->next=newnode;
}
int insert_mid(int newdata,int olddata,struct siglelist *head)
{
struct siglelist *p=head;
while(p->next!=NULL)
{
p=p->next;
if(p->data==olddata)
break;
}
struct siglelist *newnode=malloc(sizeof(struct siglelist));
newnode->data=newdata;
newnode->next=NULL;
newnode->next=p->next;
p->next=newnode;
}
int remove_list(int deldata,struct siglelist *head)
{
int flag=0;
struct siglelist *q=head;
struct siglelist *p=head->next;
while(p->next!=NULL)
{
if(p->data==deldata)
{
flag=1;
q->next=p->next;
p->next=NULL;
free(p);
p=q->next;
}
else
{
p=p->next;
q=q->next;
}
}
if(p->next==NULL && p->data==deldata)
{
q->next=NULL;
free(p);
p=NULL;
return 0;
}
if(flag==0 && p->next==NULL && p->data!=deldata)
{
printf("沒有你要刪除的資料!\n");
return -1;
}
}
int show_list(struct siglelist *head)
{
struct siglelist *p=head;
while(p->next!=NULL)
{
p=p->next;
printf("當前遍歷的節點中存放的資料是:%d\n",p->data);
}
}
int main()
{
int m,n;
int i;
struct siglelist *mylist=list_init();
for(i=0; i<4; i++)
{
printf("輸入尾插資料!\n");
scanf("%d",&m);
insert_tail(m,mylist);
}
printf("=====尾部插入========\n");
show_list(mylist);
printf("請輸入你要刪除的資料!\n");
scanf("%d",&n);
remove_list(n,mylist);
printf("=====刪除完畢========\n");
show_list(mylist);
}