为什么第一个结点不能删除
程序代码:
#include <stdio.h> #include <malloc.h> struct node{ int no; char name[10]; long number; struct node* next; }; struct node * create(int n){//创建列表 int i; struct node *head,*p,*tail; printf("no name number\n"); for(i=1;i<=n;i++){ p=(struct node*)malloc(sizeof(struct node)); scanf("%d%s%ld",&p->no,p->name,&p->number); if(i==1){head=p; tail=p; p->next=NULL;} else {tail->next =p; p->next=NULL; tail=p;} } return head; } struct node *shuchu(struct node *head){//输出列表 struct node *p; p=head; while(p!=NULL){ printf("%d %s %ld\n",p->no,p->name,p->number); p=p->next; } } struct node *delete(struct node *head,int n){//删除第n个结点 struct node *p,*q; q=head; p=head->next; while(1){ if(n==1){head=p; break;} while(p->no!=n){ p=p->next; q=q->next; } q->next=p->next; break; } return head; } int main(){ int n; struct node *head; printf("输入要创建结点的个数:\n"); scanf("%d",&n); head=create(n); printf("输出列表:\n"); shuchu(head); printf("输入要删除结点的序号;\n"); scanf("%d",&n); delete(head,n); printf("输出列表:\n"); shuchu(head); return 0; }