各位大虾,我的链表为什么删除功能执行不了啊
#include<stdio.h>#include<stdlib.h>
typedef struct part
{
int number;
struct part *next;
}node;
node *search(node*head,int n);
node*insert(node*head,int n);
node*dele(node*head,int n);
void print(node*head);
int main()
{
node*head=NULL;
char ch;
int n;
for(; ;)
{
printf("input ch");
scanf(" %c",&ch);
switch(ch)
{
case'i':
printf("input n");scanf("%d",&n);
head=insert(head,n);
break;
case'd':
printf("input n");
scanf("%d",&n);
head=dele(head,n);
break;
case's':
printf("input n");
scanf("%d",&n);
head=search(head,n);
break;
case'p':
print(head);break;
default:
printf("you make a mistake");
}
}
}
node* insert(node*head,int n)
{
node *new_node;
new_node=malloc(sizeof(node));
if(new_node==NULL)
{
printf("no space lefted");
exit(0);
}
else
{new_node->number=n;
new_node->next =head;
return new_node;
}
}
node *dele(node*head,int n)
{
node *ctr,*ptr;
for(ctr=head,ptr=NULL;ctr->number !=n&&ctr!=NULL;ptr=ctr,ctr=ctr->next)
;
if(ctr=NULL)
return head;
if(ptr=NULL)
head=head->next;
else
ptr->next=ctr->next;
free(ctr);
return head;
}
node *search(node*head,int n)
{
node *ptr;
for(ptr=head;ptr!=NULL;ptr=ptr->next)
if(ptr->number=n)
return ptr;
else
return NULL;
}
void print(node*head)
{
node *ptr;
for(ptr=head;ptr!=NULL;ptr=ptr->next)
printf("%d",ptr->number);
}