資料結構程式設計回顧(一) 通訊錄管理系統的設計與實現
阿新 • • 發佈:2018-12-31
很久沒更了
最近在複習資料結構
沒事把之前的程式碼翻出來回憶一下
【題目來自去年課程設計的七個題目】
題目一:通訊錄管理系統的設計與實現
設計要求:系統包括通訊者結點資訊的插入、查詢、刪除、
更新以及通訊錄資訊的輸出等功能。
選單內容:
1. 通訊錄連結串列的建立
2. 通訊者資訊的插入
3. 通訊者資訊的查詢
4. 通訊者資訊的修改
5. 通訊者資訊的刪除
6. 通訊錄連結串列的輸出
7. 退出管理系統
請選擇:1 – 7:
使用連結串列實現該功能:
typedef struct { char num[5]; char name[30]; char sex[3]; char phone[13]; char addr[31]; } DataType; //define a node; typedef struct node { DataType data; struct node * next; } ListNode; ListNode *head;
首先 在main函式中使用switch實現不同功能的選擇:
while(1) { // system("cls"); system("DATE/t"); //顯示系統日期和時間 system("TIME/t"); switch(menu_select()) { //根據函式返回值進行選擇 case 1: head = CreateList();//建立連結串列 printf("Querr OK\n"); break; case 2: head=insertList(head);//插入連結串列 c++; break; case 3: findlist(head);//查詢 break; case 4: modify(head);//修改 break; case 5: head=deletelist(head);//刪除 c--; break; case 6: displayList(head);//顯示 break; case 7: printf("thanks ! bye!");//退出 return 0; break; default : printf("23333"); } // }
根據menu_select函式的返回值[1-7]做出不同的選擇,當為7時退出while迴圈,系統關閉。
menu_select函式實現了顯示選單內容以及獲得輸入值並判斷輸入的是否合法(是否為1-7),若不合法,則重新輸入直合法為止,然後返回選擇的數值。,其實現如下:
int menu_select() { int sn; printf(" the address list management\n"); printf(" 1.create the address list\n"); printf(" 2.insert a piece of information\n"); printf(" 3.find a piece of information\n"); printf(" 4.modify a piece of information\n"); printf(" 5.delete a piece of information\n"); printf(" 6.display the address list\n"); printf(" 7.exit the management\n"); printf(" input 1-7:\n"); while(1) { scanf("%d",&sn); getchar(); if(sn < 1 || sn > 7) printf("wrong input!please input again:\n"); else break; } return sn; }
如果選擇1,那麼會呼叫CreateList函式,動態申請一個連結串列,並返回head結點:
ListNode* CreateList (void) {
ListNode * head = (ListNode * )malloc(sizeof(ListNode));
if(head == NULL) {
printf("in the CreateList: can't malloc the memory");
exit(0);
}
head->next = NULL;
return head;
}
如果選擇2,那麼會先動態申請一個結點,將輸入的新的通訊錄某個人的資訊儲存在結點內,並根據輸入的學號(num)遍歷連結串列,遞增的順序插入在合適的位置:
ListNode* insertList(ListNode * head ) {
ListNode *p=head;
ListNode *s=(ListNode *)malloc(sizeof(ListNode));//建立結點儲存新輸入的值
printf("pls input the info you want to insert in order(number, name, sex, phone number and address):\n");//input data
gets(s->data.num);
gets(s->data.name);
gets(s->data.sex);
gets(s->data.phone);
gets(s->data.addr);
while(p->next) {//find place to insert
p=p->next;
if(strcmp(s->data.num,p->data.num)<0)
break;
}/*
迴圈結束時:
\
2.p->next==NULL;s>p; s-p
3.s<p; p-s
*/
if(p==head) {//表為空 則直接插入在表後
s->next=p->next;
p->next=s;
}
else if(strcmp(s->data.num,p->data.num)<0) {//q->s->p//找到一個num比當前num大的 插入在其前
ListNode *q=head;//q指向p的前一個
while(q->next!=p) {
q=q->next;
}
q->next=s;
s->next=p;
} else {//p->s //沒有比當前num大的 插入到最後(好像與第一個相同 之前寫的 懶得改了)
s->next=p->next;
p->next=s;
}
return head;//insert end
}
選擇3:輸入num,在表內查詢到num對應的結點並輸出其全部內容,否則輸出未找到,並返回改結點,可以用來做修改或者刪除的操作:
ListNode *findlist(ListNode * head) {
ListNode *p=head->next;
ListNode *s=(ListNode *)malloc(sizeof(ListNode));
printf("pls input the data ( in number ) you want to find:\n");
gets(s->data.num);// input the data you want to find
while(p) {
if(strcmp(p->data.num,s->data.num)==0)
break;
p=p->next;
}
if(p==NULL) {
printf("cannot be found!\n");//not find
return NULL;
} else {//find sucess and output it
printf("%s\t",p->data.num);
printf("%s\t",p->data.name);
printf("%s\t",p->data.sex);
printf("%s\t",p->data.phone);
printf("%s\n",p->data.addr);
return p;
}
}
選擇4:呼叫findlist,返回得到對應的結點(如果找到),並對該節點的全部資訊做出新的修改:
void modify(ListNode * head) {
ListNode *p=findlist(head);//呼叫上一個函式的結果
if(p==NULL) {
printf("cannot be found!\n");//not find
} else {
printf("pls input new info:\n");//modify it
gets(p->data.num);
gets(p->data.name);
gets(p->data.sex);
gets(p->data.phone);
gets(p->data.addr);
}
}
選擇5:同樣呼叫findlist,得到想要刪除的結點,並遍歷連結串列得到該結點的前驅結點,之後修改next指標並free結點即可。
ListNode *deletelist(ListNode * head) {
ListNode *p=findlist(head);
if(p==NULL) {
printf("cannot be found!\n");//not find
} else {
ListNode *q=head;
while(q->next!=p) {
q=q->next;
}//find the place before the deleted data
q->next=p->next;
free(p);//release it
printf("delete success.\n");
}
return head;
}
選擇6:遍歷表,然後依次輸出所有內容即可。
void *displayList(ListNode * head ) {
ListNode *p= head->next;
if(p==NULL)
printf("the address list is NULL now.\n");// the list is empty now
else {
int i=0;
printf("num\tname\tsex\tphone\taddr\n");
while(i<c) {
//output everything
printf("%s\t",p->data.num);
printf("%s\t",p->data.name);
printf("%s\t",p->data.sex);
printf("%s\t",p->data.phone);
printf("%s\n",p->data.addr);
p=p->next;
i++;
}
}
}
選擇7:return 0; 結束
完整程式碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
int c;
//define the information about the data;
typedef struct {
char num[5];
char name[30];
char sex[3];
char phone[13];
char addr[31];
} DataType;
//define a node;
typedef struct node {
DataType data;
struct node * next;
} ListNode;
ListNode *head;
//the menu of the management;
int menu_select() {
int sn;
printf(" the address list management\n");
printf(" 1.create the address list\n");
printf(" 2.insert a piece of information\n");
printf(" 3.find a piece of information\n");
printf(" 4.modify a piece of information\n");
printf(" 5.delete a piece of information\n");
printf(" 6.display the address list\n");
printf(" 7.exit the management\n");
printf(" input 1-7:\n");
while(1) {
scanf("%d",&sn);
getchar();
if(sn < 1 || sn > 7)
printf("wrong input!please input again:\n");
else
break;
}
return sn;
}
ListNode* CreateList (void) {
ListNode * head = (ListNode * )malloc(sizeof(ListNode));
if(head == NULL) {
printf("in the CreateList: can't malloc the memory");
exit(0);
}
head->next = NULL;
return head;
}
ListNode* insertList(ListNode * head ) {
ListNode *p=head;
ListNode *s=(ListNode *)malloc(sizeof(ListNode));
printf("pls input the info you want to insert in order(number, name, sex, phone number and address):\n");//input data
gets(s->data.num);
gets(s->data.name);
gets(s->data.sex);
gets(s->data.phone);
gets(s->data.addr);
while(p->next) {//find place to insert
p=p->next;
if(strcmp(s->data.num,p->data.num)<0)
break;
}/*
迴圈結束時:
\
2.p->next==NULL;s>p; s-p
3.s<p; p-s
*/
if(p==head) {
s->next=p->next;
p->next=s;
}
else if(strcmp(s->data.num,p->data.num)<0) {//q->s->p
ListNode *q=head;
while(q->next!=p) {
q=q->next;
}
q->next=s;
s->next=p;
} else {//p->s
s->next=p->next;
p->next=s;
}
return head;//insert end
}
ListNode *findlist(ListNode * head) {
ListNode *p=head->next;
ListNode *s=(ListNode *)malloc(sizeof(ListNode));
printf("pls input the data ( in number ) you want to find:\n");
gets(s->data.num);// input the data you want to find
while(p) {
if(strcmp(p->data.num,s->data.num)==0)
break;
p=p->next;
}
if(p==NULL) {
printf("cannot be found!\n");//not find
return NULL;
} else {//find sucess and output it
printf("%s\t",p->data.num);
printf("%s\t",p->data.name);
printf("%s\t",p->data.sex);
printf("%s\t",p->data.phone);
printf("%s\n",p->data.addr);
return p;
}
}
void modify(ListNode * head) {
ListNode *p=findlist(head);
if(p==NULL) {
printf("cannot be found!\n");//not find
} else {
printf("pls input new info:\n");//modify it
gets(p->data.num);
gets(p->data.name);
gets(p->data.sex);
gets(p->data.phone);
gets(p->data.addr);
}
}
ListNode *deletelist(ListNode * head) {
ListNode *p=findlist(head);
if(p==NULL) {
printf("cannot be found!\n");//not find
} else {
ListNode *q=head;
while(q->next!=p) {
q=q->next;
}//find the place before the deleted data
q->next=p->next;
free(p);//release it
printf("delete success.\n");
}
return head;
}
void *displayList(ListNode * head ) {
ListNode *p= head->next;
if(p==NULL)
printf("the address list is NULL now.\n");// the list is empty now
else {
int i=0;
printf("num\tname\tsex\tphone\taddr\n");
while(i<c) {
//output everything
printf("%s\t",p->data.num);
printf("%s\t",p->data.name);
printf("%s\t",p->data.sex);
printf("%s\t",p->data.phone);
printf("%s\n",p->data.addr);
p=p->next;
i++;
}
}
}
// the main function;
int main() {
while(1) {
// system("cls");
system("DATE/t");
system("TIME/t");
switch(menu_select()) {
case 1:
head = CreateList();
printf("Querr OK\n");
break;
case 2:
head=insertList(head);
c++;
break;
case 3:
findlist(head);
break;
case 4:
modify(head);
break;
case 5:
head=deletelist(head);
c--;
break;
case 6:
displayList(head);
break;
case 7:
printf("thanks ! bye!");
return 0;
break;
default :
printf("23333");
}
//
}
}