很久没更了
最近在复习数据结构
没事把之前的代码翻出来回忆一下
【题目来自去年课程设计的七个题目】
题目一:通讯录管理系统的设计与实现
设计要求:系统包括通讯者结点信息的插入、查询、删除、
更新以及通讯录信息的输出等功能。
菜单内容:
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.snext=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(idata.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
#include
#include
#include
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.snext=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(idata.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");
}
//
}
}