前辈帮忙指导,里面的程序有些问题想请教
程序代码:
#include <stdio.h> #include <stdlib.h> struct sdu_info{ int number; //学号 int sex; //0为男,1为女 int age; //年龄 int class; //班级代码 int counter; //成绩分数 struct sdu_info * next; //节点指针 }; typedef struct sdu_info Sdu; //Sdu链表 typedef Sdu * SduPtr; //Sdu链表指针 int input(int i); void set(int i,SduPtr *head,SduPtr *last); void add(SduPtr *head,SduPtr *last); void List(SduPtr head); //以下的函数可否用函数指针来处理,请前辈指教 SduPtr chake(int data,int i,SduPtr head,SduPtr last); SduPtr ck01(int data,SduPtr head,SduPtr last); SduPtr ck02(int data,SduPtr head,SduPtr last); SduPtr ck03(int data,SduPtr head,SduPtr last); SduPtr ck04(int data,SduPtr head,SduPtr last); SduPtr ck05(int data,SduPtr head,SduPtr last); //以下函数有问题。 void del(SduPtr *head,SduPtr *last); int main(int argc, char **argv) { SduPtr head = NULL; //头元素地址 SduPtr last = NULL; //最后元素地址 int i = 0; //流程控制码 do { // printf("in main head = %p , last = %p\n",head,last); set(input(i),&head,&last); List(head); }while(1); return 0; } //输入增加、删除操作码 int input(int i){ printf("1 is add , 2 is del , input your select : "); do { scanf("%d",&i); }while(i!=1 && i!=2); return i; } //接收操作码后的操作 void set(int i,SduPtr *head,SduPtr *last){ switch (i){ case 1: add(head,last); break; case 2: del(head,last); break; default: printf("select error !\n"); break; } } //实现添加链表 void add(SduPtr *head,SduPtr *last){ SduPtr p = (SduPtr)malloc(sizeof(Sdu)); //申请失败则提示并返回 if ( p == NULL){ printf("malloc cache is NULL , error!\n"); return; } //申请成功,录入新数据 printf("input your mouber :"); scanf("%d",&p->number); printf("input sex (0 man , 1 wuman):"); scanf("%d",&p->sex); printf("input your age :"); scanf("%d",&p->age); printf("input your class :"); scanf("%d",&p->class); printf("input counter :"); scanf("%d",&p->counter); //每次都从head重新计算last的地址 *last = *head; //插入新数据p if (*head == NULL){ //表空的操作 *head = p; *last = p; (*head)->next = NULL; }else{ //表不为空的操作 if ((*head)->next == NULL){ //表不空,但只有表头1个数据的操作 (*head)->next = p; *last = p; (*last)->next = NULL; }else{ do { //很多数据,则重新计算生成last的地址并链接新数据 *last = (*last)->next; }while((*last)->next != NULL); (*last)->next = p; *last = p; } } // printf("in add head = %p , last = %p , p = %p\n",*head,*last,p); } //按选择的类型与其值来搜索删除数据 void del(SduPtr *head,SduPtr *last){ SduPtr p , now1 , now2 ; int i , data; //数据为null返回 if (*head == NULL){ printf("link node is NULL, no data .\n"); return ; } //否则操作录入要删除的资料 printf("1 del number , 2 del sex , 3 del age , 4 del class ,5 del counter .\n"); printf("input your del type num : "); scanf("%d",&i); printf("input your del data value : "); scanf("%d",&data); //检查是否存在要删除的数组,有则接收其指针p,无则NULL p = chake(data,i,*head,*last); //操作查询返回的结果p now1 = *head; now2 = *head; if (p == NULL){ printf("no your find DATA , error !\n"); }else{ if(now1->next == NULL){ *head = NULL; }else{ do { now2 = now2->next; if (now2 == p){ now1->next = now2->next; free(now2); break; } now1 = now2; }while(now2->next != NULL); } } } SduPtr chake(int data,int i,SduPtr head,SduPtr last){ SduPtr p; switch (i){ case 1: p = ck01(data,head,last); break; case 2: p = ck02(data,head,last); break; case 3: p = ck03(data,head,last); break; case 4: p = ck04(data,head,last); break; case 5: p = ck05(data,head,last); break; } return p; } SduPtr ck01(int data,SduPtr head,SduPtr last){ SduPtr i = head; do { if (i->number == data){ break; } i = i->next; }while(i->next != NULL); if (i == last){ i = NULL; } return i; } SduPtr ck02(int data,SduPtr head,SduPtr last){ SduPtr i = head; do { if (i->sex == data){ break; } i = i->next; }while(i->next != NULL); if (i == last){ i = NULL; } return i; } SduPtr ck03(int data,SduPtr head,SduPtr last){ SduPtr i = head; do { if (i->age == data){ break; } i = i->next; }while(i->next != NULL); if (i == last){ i = NULL; } return i; } SduPtr ck04(int data,SduPtr head,SduPtr last){ SduPtr i = head; do { if (i->class == data){ break; } i = i->next; }while(i->next != NULL); if (i == last){ i = NULL; } return i; } SduPtr ck05(int data,SduPtr head,SduPtr last){ SduPtr i = head; do { if (i->counter == data){ break; } i = i->next; }while(i->next != NULL); if (i == last){ i = NULL; } return i; } void List(SduPtr head){ SduPtr i = head; if (i == NULL){ printf("now is NULL\n"); }else{ do{ printf("data ptr = %p , ",i); printf( "number %d,sex %d,age %d,class %d,counter %d.\n", i->number,i->sex,i->age,i->class,i->counter); i = i->next; printf("\n"); }while(i != NULL); } }