初学链表,有关单链表的程序有几处错,求高手们改错或完善
建立一个结构体可存储7个人数据(姓名,电话),然后用链表编写,使用户可以从第一个位置添加及删除,从最后一个位置添加及删除,也可以删除任意制定位置的信息,并且显示出来程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Contact { struct Contact* next; char name[7]; char tel; }Contact; Contact*start=NULL; void printList(Contact*a) { if(a==NULL){ printf("no contact given"); } else{ printf("\nName:%s \nTel:%f", a->name,a->tel); } } void addFirst(Contact*a){ struct Contact*newA; newA=malloc(sizeof(Contact)); printf("Name:"); scanf("%6s",&(newA->name)); printf("Tel:"); scanf("%f",&(newA->tel)); return newA; void push(Contact*newA){ if(start==NULL){ start=newA; start->next=NULL; }else{ newA->next=start; start=newA; } } } void addLast(Contact*a){ struct Contact*newA; newA=malloc(sizeof(Contact)); printf("Name:"); scanf("%6s",&(newA->name)); printf("Tel:"); scanf("%s",&(newA->tel)); return newA; } void removeByName(char*removeByName){ printf("0"); Contact*ptr=start; Contact*last_ptr=start; if(start==NULL) return; if(strcmp(start->name,removeByName)==0){ ptr=start; start=start->next; } else { while(ptr!=NULL){ if(strcmp(ptr->name,removeByName)==0){ break; } last_ptr=ptr; ptr=ptr->next; } last_ptr->next=ptr->next; } free(ptr); } void removeFirst() { Contact*pop(){ if(start==NULL) treurn NULL; Contact*help=start; start=start->next; return help; } } void removeLast() { void push(Contact*newA){ if(start==NULL){ start=newA; start->next=NULL; } else{ newA->next=start; start=newA; } } Contact*pop(){ if(start==NULL) treurn NULL; Contact*help=start; start=start->next; return help; } } void builtList(Contact contacts[]) { Contact *ptr=NULL; int i; for(i=0; i<=6; i++) { ptr=&contacts[i]; addLast(ptr); } } int main() {Contact*a; printStack(); Contact contacts[7]= { {NULL,"Dennis","0203/123456"}, {NULL,"Chantal","0177/3123345"}, {NULL,"Robert","0163/9295986"}, {NULL,"Bjoern","040 - 123232345"}, {NULL,"Andreas","+49 178 11903123"}, {NULL,"Jenny","+41 119 34544345"}, {NULL,"Zeuss","0162-123-4531698"}, }; int choose; do{ printf("1.Removing name Andreas\n"); printf("2.Removing first\n"); printf("3.Removing last\n"); printf("4.Exit\n"); printf("Input:"); scanf("%i",&choose); printf("\n"); switch(choose) { case 1: removeByName(char*removeByName); //此处程序报错,不知道怎么改// printList(Contact*a); break; case 2: removeFirst(); printList(Contact*a); break; case 3:removeLast(); printList(Contact*a); break; }while(choose!=4); return 0; }