真的绝望,为了这道题用了5 6小时,主要两个问题,在以下我会表达清楚(字数限制。。。)
问题一:为什么我输出的数据后面有,?... 我在贴吧问了好像是%s的问题问题二:怎么做对啊,具体点,学结构到现在只敲对一个
程序代码:
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct stud) #define NUll 0 struct stud {long num; char name[20]; char sex; int old; struct stud *next; }; int n; struct stud *creat(void) { struct stud *head,*p1,*p2; n=0; p2=p1=(struct stud*)malloc(LEN); printf("输入:"); scanf("%ld,%s,%c,%d",&p1->num,&p1->name,&p1->sex,&p1->old); head=NULL; while(p1->num!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct stud*)malloc(LEN); printf("Input:"); scanf("%ld,%s,%c,%d",&p1->num,&p1->name,&p1->sex,&p1->old); } p2->next=NULL; free(p1); return(head); } void print_list(struct stud *head) { struct stud* p; printf("\nNow ,These %d records are:\n",n); p=head; if(head!=NUll) { while(p!=NULL) {printf("%ld,%s,%c,%d\n",p->num,p->name,p->sex,p->old); p=p->next; } } } struct stud *del( struct stud *head,int num ) { struct stud *p1, *p2; if (head==NULL) { printf("\n list is null!\n"); return head; } p1=head; while (num!=p1->old&&p1->next!=NULL) { p2=p1; p1=p1->next; } if ( num==p1->old ) { if (p1==head) head=p1->next; else p2->next=p1->next; printf("delete:%d\n", num); } else printf("%d not been found!\n",num); return head; } void main() { struct stud *head; int del_num=0; printf("input records:\n"); head=creat(); print_list(head); printf("\nInput the deleted number:"); scanf("%d",&del_num); head=del(head,del_num); print_list(head); }