無法回傳表頭
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct per /*定義結構體型別*/ {
long int num;
char name[20];
char tel[12];
char mail[30];
char add[50];
long int mnum;
char id[18];
struct per *next;
};
struct per *jianli(); /*宣告建立通信錄函式*/
void shuchu(struct per *head); /*宣告輸出通信錄函式*/
struct per *tianjia(struct per *head); /*宣告添加聯系人函式*/
struct per *shanchu(struct per *head); /*宣告洗掉聯系人函式*/
struct per *chazhao(struct per *head); /*宣告查找聯系人函式*/
struct per *head=NULL;
int main() { /*主函式*/
int select;
printf("\n\t***************** 歡迎進入個人通信錄管理系統 ******************\n");
/*主控選單*/ printf("\n\t ----------------->> 1: 建 立 通 信 錄 <<-----------------");
printf("\n\t ----------------->> 2: 輸 出 通 信 錄 <<-----------------");
printf("\n\t ----------------->> 3: 添 加 聯 系 人 <<-----------------");
printf("\n\t ----------------->> 4: 刪 除 聯 系 人 <<-----------------");
printf("\n\t ----------------->> 5: 查 找 聯 系 人 <<-----------------");
printf("\n\t ----------------->> 0: 退 出 系 統 <<-----------------\n");
printf("\n\t***************************************************************\n");
printf("\n\t請選擇(0---5):");
scanf("%d",&select);
printf("\n");
switch(select) { /*對應模塊函式*/
case 1:
head=jianli();
break;
case 2:
shuchu(head);
break;
case 3:
head=tianjia(head);
break;
case 4:
head=shanchu(head);
break;
case 5:
head=chazhao(head);
break;
case 0:
break;
default:
printf("您的輸入有誤,請重新輸入(0---5):\n");
}
while(select!=0);
}
/*建立通信錄函式*/
struct per *jianli() { /*創建鏈表,并回傳表頭指標*/
struct per *head; /*表頭*/
struct per *p1; /*新建節點*/
struct per *p2; /*表尾節點*/
long int num1;
char name1[20];
char tel1[12];
char mail1[30];
char add1[50];
long int mnum1;
char id1[18];
head=NULL; /*無任何節點,表頭指向空*/
printf("請輸入聯系人編號、姓名、電話、身份證號、郵箱、地址及郵編(用空格分隔,用0結束)\n");
scanf("%d",&num1);
while(num1!=0) { /*假設num=0表示輸入結束*/
scanf(" %s %s %s %s %s %d",name1,tel1,id1,mail1,add1,&mnum1); /*讀入第一個聯系人的資訊*/
p1=(struct per *)malloc(sizeof(struct per)); /*新建一個節點*/
p1->num=num1; /*存入聯系人的資訊*/
strcpy(p1->name,name1);
strcpy(p1->tel,tel1);
strcpy(p1->id,id1);
strcpy(p1->mail,mail1);
strcpy(p1->add,add1);
p1->mnum=mnum1;
p1->next=NULL; /*將next域置為空,表示尾節點*/
if(head==NULL)
head=p1; /*第一個新建節點是表頭*/
else
p2->next=p1; /*原表尾的下一個節點是新節點*/
p2=p1; /*新建節點成為表尾*/
scanf("%d",&num1);
}
return head; /*回傳表頭指標*/
}
/*輸出通信錄函式*/
void shuchu(struct per *head) {
struct per *p;
int n=0; /*統計節點數,即聯系人數*/
printf("\n\t*******************通*******信*******錄*******************\n");
printf("\n%-5s%-12s%-12s%-19s%-17s%-9s%-7s\n","編號","姓名","電話號碼","身份證號","郵箱","地址","郵編");
if(head!=NULL) {
for(p=head; p!=NULL; p=p->next) { /*如果到達尾節點退出回圈,否則繼續*/
printf("%-5d%-12s%-12s%-19s%-17s%-9s%-7d\n",p->num,p->name,p->tel,p->id,p->mail,p->add,p->mnum);
n++;
}
printf("聯系人總數:%d\n\n",n);
} else
printf("\n對不起,該通信錄中沒有任何聯系人!\n\n");
}
/*添加聯系人函式*/
struct per*tianjia(struct per*head) {
struct per*p;
struct per*p1;
struct per*p2;
p2=head;
p=(struct per*)malloc(sizeof(struct per)); /*新建節點p*/
printf("請輸入要添加的聯系人的編號、姓名、電話、身份證號、郵箱、地址、郵編:\n");
scanf("%d %s %s %s %s %s %d",&p->num,p->name,p->tel,p->id,p->mail,p->add,&p->mnum);
printf("成功添加聯系人%s !\n",p->name);
if(head==NULL) {
head=p;
p->next=NULL;
} else {
while((p->num>p2->num)&&(p2->next!=NULL)) { /*查找添加位置*/
p1=p2;
p2=p2->next;
}
if(p->num<=p2->num) { /*從小到大排列,添加至表內*/
if(p2==head)
{
head=p;
p->next=p2;
} else {
p1->next=p;
p->next=p2;
}
} else { /*添加至表尾*/
p2->next=p;
p->next=NULL;
}
}
return(head);
}
/*洗掉聯系人函式*/
struct per *shanchu(struct per *head) {
struct per *p2;
struct per *p1;
int num1,n;
printf("請輸入要洗掉的聯系人編號:"); /*根據輸入資訊查找聯系人*/
scanf("%d",&num1);
if(head==NULL) {
printf("\n通訊錄為空\n\n");
return(head);
}
p2=head;
while(num1 != p2-> num && p2->next !=NULL) {
p1=p2;
p2=p2->next;
}
if(num1==p2->num) {
printf("\n成功查詢到該聯系人!\n");
printf("\n%-5s%-12s%-12s%-19s%-17s%-9s%-7s\n","編號","姓名","電話號碼","身份證號","郵箱","地址","郵編");
printf("%-5d%-12s%-12s%-19s%-17s%-9s%-7d",p2->num,p2->name,p2->tel,p2->id,p2->mail,p2->add,p2->mnum);
printf("\n確定洗掉? 1確定 0取消 ");
scanf("%d",&n);
switch(n) {
case 1: {
if(p2==head)
head=p2->next;
else
p1->next=p2->next;
free(p2);
printf("\n成功洗掉編號為%ld的聯系人!\n\n",num1);
}
break;
case 0:
printf("\n您取消了洗掉任務,聯系人未被洗掉!\n");
break;
}
} else
printf("\n對不起,該通信錄中沒有任何聯系人!\n\n");
return(head);
}
/*查找函式*/
struct per *chazhao(struct per *head) {
struct per*p2;
char id1[20];
printf(" 請輸入要查找的聯系人身份證號:\n");
scanf("%s",id1);
if(head==NULL) {
printf("\n通訊錄為空\n\n");
return(head);
}
p2=head;
while (p2) {
if(strcmp(p2->id,id1)==0) {
printf("\n成功查詢到該聯系人!\n");
printf("\n%-5s%-12s%-12s%-19s%-17s%-9s%-7s\n","編號","姓名","電話號碼","身份證號","郵箱","地址","郵編");
printf("%-5d%-12s%-12s%-19s%-17s%-9s%-7d\n",p2->num,p2->name,p2->tel,p2->id,
p2->mail,p2->add,p2->mnum);
break;
}
p2=p2->next;
}
if(!p2)
printf("\n對不起,該通信錄沒有該聯系人!\n\n");
return (head);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/235148.html
標籤:C語言
上一篇:求解
下一篇:C語言
