#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct
{
char num[5];
char name[20];
char phone[10];
}studata;
typedef struct node
{
studata data;
struct node *next;
}listnode,*linklist;
void menu()
{
printf("\n***************************班級通訊錄***********************************\n\n");
printf("1-建立通訊錄 2-查看全部記錄 3-查詢 4-同學加入 5-同學離開 0-退出系統 \n\n");
printf("請選擇(0-5):");
}linklist creatlist()
{linklist head,s,p,q;
char ch; head=(listnode*)malloc(sizeof(listnode));
if(!head) exit(-1);
head->next=NULL;
p=head;
do{
s=(linklist)malloc(sizeof(listnode));
if(!s) exit(-1);
printf("請輸入同學的編號:");
scanf("%s",s->data.num);
printf("請輸入同學的姓名:");
scanf("%s",s->data.name) ;
printf("請輸入同學的電話號碼:"); scanf("%s",s->data.phone) ;
s->next=p->next;
p->next=s;
printf("\n繼續添加嗎?(y/n)");
scanf("\n%c",&ch);
}while(ch=='y'||ch=='Y');
return head;
}
listnode * searchlist(linklist head)
{listnode *p;
int x;
char num[5];
char name[20];
printf("1.按編號查詢 2.按姓名查詢\n");
printf("請選擇:");
scanf("%d",&x);
if(x==1)
{printf("請輸入待查同學的編號:");
scanf("%s",num);
for(p=head;p!=NULL&&strcmp(p->data.num,num)!=0;p=p->next) ; }
else if(x==2)
{
printf("請輸入待查同學的姓名:");
scanf("%s",name);
for(p=head;p!=NULL&&strcmp(p->data.name,name)!=0;p=p->next) ;
}
return p;
}
void insertlist(linklist head,listnode *p)
{p->next=head->next; head->next=p;}
void delnode(linklist head)
{linklist p,q; char ch; p=searchlist(head);
if(!p)
{
printf("沒有找到符合條件的記錄!\n");
return ;
}
printf("確定要洗掉嗎?(y/n)");
scanf("\n%c",&ch);
if(ch=='n'||ch=='N')
return;
for(q=head;q!=NULL&&q->next!=p;q=q->next) ; q->next=p->next;
free(p);
printf("洗掉成功!\n");
}
void printlist(linklist head)
{linklist p; p=head->next; printf("編號 姓名 電話號碼\n"); while(p!=NULL)
{printf("%s %s %s\n",p->data.num,p->data.name,p->data.phone); p=p->next;
}}
int main()
{int t;linklist head,p;
menu();
while(1)
{
scanf("%d",&t);
switch(t)
{case 1:
printf("建立通訊錄\n");
head=creatlist();
break;
case 2:
printf("查看全部記錄\n");
printlist(head);
break;
case 3:
printf("查詢\n");
p=searchlist(head);
if(p)
{printf("編號 姓名 電話號碼\n");
printf("%s %s %s\n",p->data.num,p->data.name,p->data.phone); } else printf("查無此人!\n");
break;
case 4:
printf("插入\n");
p=(listnode*)malloc(sizeof(listnode));
printf("請輸入同學的編號:");
scanf("%s",p->data.num);
printf("請輸入同學的姓名:");
scanf("%s",p->data.name) ;
printf("請輸入同學的電話號碼:"); scanf("%s",p->data.phone) ;
insertlist(head,p);
break;
case 5:
printf("洗掉\n");
delnode(head);
break;
case 0:
printf("退出\n");
return 0;
}
}
}
那位大神幫忙給注釋一下,不勝感激

或者是能改進一下
uj5u.com熱心網友回復:
修改如下,供參考:#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct
{
char num[5];
char name[20];
char phone[10];
}studata;
typedef struct node
{
studata data;
struct node *next;
}listnode,*linklist;
void menu()
{
printf("\n***************************班級通訊錄***********************************\n\n");
printf("1-建立通訊錄 2-查看全部記錄 3-查詢 4-同學加入 5-同學離開 0-退出系統 \n\n");
printf("請選擇(0-5):");
}
linklist creatlist()
{
linklist head,s,p,q;
char ch;
head=(listnode*)malloc(sizeof(listnode));
if(!head) exit(-1);
head->next=NULL;
p=head;
do{
s=(linklist)malloc(sizeof(listnode));
if(!s) exit(-1);
printf("請輸入同學的編號:");
fflush(stdout);rewind(stdin);
scanf("%s",s->data.num);
printf("請輸入同學的姓名:");
fflush(stdout);rewind(stdin);
scanf("%s",s->data.name) ;
printf("請輸入同學的電話號碼:");
fflush(stdout);rewind(stdin);
scanf("%s",s->data.phone) ;
s->next=p->next;
p->next=s;
printf("\n繼續添加嗎?(y/n)");
fflush(stdout);rewind(stdin);
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return head;
}
listnode * searchlist(linklist head)
{
listnode *p;
int x;
char num[5];
char name[20];
if(head==NULL){
printf("無記錄,請先添加記錄!\n");
return NULL;
}
printf("1.按編號查詢 2.按姓名查詢\n");
printf("請選擇:");
fflush(stdout);rewind(stdin);
scanf("%d",&x);
if(x==1)
{
printf("請輸入待查同學的編號:");
fflush(stdout);rewind(stdin);
scanf("%s",num);
for(p=head;p!=NULL&&strcmp(p->data.num,num)!=0;p=p->next);
}
else if(x==2)
{
printf("請輸入待查同學的姓名:");
fflush(stdout);rewind(stdin);
scanf("%s",name);
for(p=head;p!=NULL&&strcmp(p->data.name,name)!=0;p=p->next);
}
return p;
}
void insertlist(linklist head)
{
listnode *p;
if(head==NULL){
printf("無記錄,請先添加記錄!\n");
return;
}
printf("插入\n");
p=(listnode*)malloc(sizeof(listnode));
printf("請輸入同學的編號:");
fflush(stdout);rewind(stdin);
scanf("%s",p->data.num);
printf("請輸入同學的姓名:");
fflush(stdout);rewind(stdin);
scanf("%s",p->data.name) ;
printf("請輸入同學的電話號碼:");
fflush(stdout);rewind(stdin);
scanf("%s",p->data.phone) ;
p->next=head->next;
head->next=p;
}
void delnode(linklist head)
{
linklist p,q;
char ch;
if(head==NULL){
printf("無記錄,請先添加記錄!\n");
return;
}
p=searchlist(head);
if(!p)
{
printf("沒有找到符合條件的記錄!\n");
return ;
}
printf("確定要洗掉嗎?(y/n)");
fflush(stdout);rewind(stdin);
scanf("%c",&ch);
if(ch=='n'||ch=='N') return;
for(q=head;q!=NULL&&q->next!=p;q=q->next);
q->next=p->next;
free(p);
printf("洗掉成功!\n");
}
void printlist(linklist head)
{
linklist p;
if(head==NULL){
printf("無記錄,請先添加記錄!\n");
return;
}
p=head->next;
printf("編號 姓名 電話號碼\n");
while(p!=NULL)
{
printf("%s %s %s\n",p->data.num,p->data.name,p->data.phone);
p=p->next;
}
}
int main()
{
int t;
linklist head=NULL,p=NULL;
menu();
while(1)
{
scanf("%d",&t);
switch(t)
{
case 1:
printf("建立通訊錄\n");
head=creatlist();
break;
case 2:
printf("查看全部記錄\n");
printlist(head);
break;
case 3:
printf("查詢\n");
p=searchlist(head);
if(p)
{
printf("編號 姓名 電話號碼\n");
printf("%s %s %s\n",p->data.num,p->data.name,p->data.phone);
}else printf("查無此人!\n");
break;
case 4:
insertlist(head);
break;
case 5:
printf("洗掉\n");
delnode(head);
break;
case 0:
printf("退出\n");
return 0;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283702.html
標籤:基礎類
