链表删除值
//先上代码#include<stdio.h>
#include<stdlib.h>
struct work
{
int data;
struct work *next;
};
typedef struct work myfun;
void show(struct work*);
void delet(struct work*,int);
int main()
{
myfun *head=NULL;
myfun *current,*prev;
myfun *change;
int i=10;
int c=5;
while(i<=30)
{
current=(struct work*)malloc(sizeof(struct work));
if(head==NULL)
{
head=current;
}
else
{
prev->next=current;
}
current->data=i;
i=i+10;
current->next=NULL;
prev=current;
}
printf("The original list is: \n");
show(head);
printf("The list after 5 deleted is: \n");
delet(head,c);
show(head);
system("pause");
return 0;
}
void show(struct work *current)
{
while(1)
{
printf("%d\n",current->data);
if(current->next==NULL)
{
break;
}
current=current->next;
}
}
void delet(struct work *current,int x)
{
struct work *change;
while(1)
{
if(current->data==x)
{
current=current->next;
break;
}
else if((current->next)->data==x)
{
change=current->next;
current->next=(current->next)->next;
free(change);
break;
}
else
{
current=current->next;
}
}
}
不知道为什就是没有办法删掉第一个,虽然题目要求删的是值为20的,可是就是搞不明白,因为我已经考虑了要删除第一个的情况了,可是就是不行