链表问题,求助!!!求高手
谁能帮忙看看这链表。。。在删除链表总出问题程序代码:
#include<stdio.h> #include<stdlib.h> #define LEN sizeof(struct work) struct work{ int num; char name[15]; double gongzi; struct work *next; }; int n; struct work *create()//创建链表 { struct work *head,*p1,*p2; n=0; p1 = p2 = (struct work *)malloc(LEN); scanf("%d %lf %s ",&p1->num,&p1->gongzi,p1->name); head = NULL; while(p1->num != 0) { n++; if(n == 1) head = p1; else p2->next = p1; p2 = p1; p1 = (struct work *)malloc(LEN); scanf("%d%lf%s",&p1->num,&p1->gongzi,p1->name); } p2->next = NULL; return head; } struct work *del(struct work *head,int num)//删除链表的一行 { struct work *p1,*p2; if(head == NULL) { printf("此链表无,请确认链表有无创建"); return head; } p1 = head; if(num != p1->num&&p1->next != NULL) { p2 = p1; p1 = p1->next; } if(p1->num == num) { if(head == p1) head = p1->next; else p2->next = p1->next; printf("此数%d存在",num); free(p1); n--; } else printf("此数%d不存在",num); return head; } void print(struct work *head) { struct work *p; p = head; while(p!=NULL) { printf("%d\t%s\t%.2lf\n",p->num,p->name,p->gongzi); p = p->next; } } void main() { struct work *head; char pd; int num; head = create(); print(head); printf("确认是否删除此表的一行??输入y为修改,否则不修改...请输入\n"); scanf("%c",&pd); if(pd == 'y'||pd == 'Y') print(head); else { printf("请把不要行的开头数输入\n"); scanf("%d",num); head = del(head,num); print(head); } }