Last active
August 29, 2015 14:02
-
-
Save kaleocheng/71ff0a3edd0c30fd87d0 to your computer and use it in GitHub Desktop.
LinkList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//*******链表的操作**************// | |
//Test | |
/* | |
*本程序为练习链表的操作所写,仅实现了最基本的几个功能:添加,删除,查找联系人. | |
* 很多细节并未实现,如检验姓名,和手机号码是否合法,对重复姓名联系人的处理,按时间先后排序,按字母先后排序等. | |
* 程旭 | |
* [email protected] | |
*/ | |
#include<stdio.h> | |
#include<malloc.h> | |
#include<string.h> | |
#define LinkDataType int | |
#define MAX_NAME 20 | |
//*******链表的结点**************// | |
typedef struct node | |
{ | |
char userName[MAX_NAME]; | |
char telNum[15]; | |
int idNum; | |
struct node *next; | |
}Link; | |
//*****对链表的操作过程中用到的几个指针*******// | |
typedef struct | |
{ | |
Link *head; | |
Link *current, *previous; | |
}LinkStruct; | |
//#######函数的申明########// | |
void print(LinkStruct aLink); | |
int add(char userName[],char telNum[],int idNum,LinkStruct *aLink); | |
int del(char userName[],LinkStruct *aLink); | |
int findId(char userName[],LinkStruct *aLink); | |
//#######################// | |
//*******print() 遍历打印链表*****************// | |
void print(LinkStruct aLink) | |
{ | |
aLink.current = aLink.head; | |
if(aLink.current == NULL) | |
{ | |
printf("Empty\n"); | |
}else{ | |
printf("UserName TelNum\n"); | |
while(aLink.current != NULL) | |
{ | |
printf("\n%-15s%-15s\n",aLink.current->userName,aLink.current->telNum); | |
aLink.current = aLink.current->next; | |
} | |
printf("\n"); | |
} | |
} | |
//**********add() 加入新的联系人信息到链表中*********************// | |
int add(char userName[],char telNum[],int idNum,LinkStruct *aLink) | |
{ | |
aLink->current = ( Link *)malloc(sizeof(Link)); | |
if(aLink->current == NULL) | |
{ | |
printf("malloc error\n"); | |
return 0; | |
}else | |
aLink->current->next = NULL; | |
if(aLink->head == NULL) | |
aLink->head = aLink->current; | |
else | |
aLink->previous->next = aLink->current; | |
strcpy( aLink->current->userName,userName); | |
strcpy(aLink->current->telNum,telNum); | |
aLink->current->idNum = idNum; | |
aLink->previous = aLink->current; | |
return 1; | |
} | |
//*********del() 删除链表中指定的联系人的信息*********************// | |
int del(char userName[],LinkStruct *aLink) | |
{ | |
int i; | |
Link *temp; | |
if(! (i=findId(userName,aLink))) | |
{ | |
printf("So can't del this\n"); | |
return 0; | |
} | |
if(i == aLink->head->idNum) | |
{ | |
temp = aLink->head; | |
aLink->head = aLink->head->next; | |
free(temp); | |
return 1; | |
}else{ | |
aLink->current = aLink->head; | |
while(aLink->current->idNum != i) | |
{ | |
aLink->previous = aLink->current; | |
aLink->current = aLink->current->next; | |
} | |
aLink->previous->next = aLink->current->next; | |
free(aLink->current); | |
aLink->current = aLink->current->next; | |
return 1; | |
} | |
} | |
//*********findId() 查找链表中给定的联系人的信息*********************// | |
int findId(char userName[],LinkStruct *aLink) | |
{ | |
aLink->current = aLink->head; | |
while(aLink->current != NULL && strcmp(aLink->current->userName,userName) ) | |
{ | |
aLink->current = aLink->current->next; | |
} | |
if(aLink->current == NULL) | |
{ | |
printf("Cant find .....\n"); | |
return 0; | |
} | |
return aLink->current->idNum; | |
} | |
//*******main() 打印相应菜单************************// | |
int main() | |
{ | |
LinkStruct TheLink={NULL}; | |
char userName[MAX_NAME],telNum[15]; | |
int idNum; | |
int option,i; | |
printf("#########电话本#########\n"); | |
printf("请选择相应的操作:\n"); | |
printf("1.显示当前联系人\n"); | |
printf("2.新加联系人\n"); | |
printf("3.删除联系人\n"); | |
printf("4.搜索联系人\n"); | |
printf("5.打印菜单\n"); | |
printf("######################\n"); | |
while(1) | |
{ | |
switch(option = getchar()) | |
{ | |
case '1' : | |
print(TheLink); | |
printf("\n#请输入要进行的操作(按5打印菜单):"); | |
break; | |
case '2' : | |
printf("\n请输入联系人姓名:"); | |
scanf("%s",userName); | |
printf("请输入联系人的电话号码:"); | |
scanf("%s",telNum); | |
idNum = ++i; | |
if(add(userName,telNum,idNum,&TheLink)) | |
printf("添加成功 姓名:%s 电话:%s\n",TheLink.current->userName,TheLink.current->telNum); | |
printf("\n#请输入要进行的操作(按5打印菜单):"); | |
break; | |
case '3' : | |
printf("\n请输入要删除的联系人姓名\n"); | |
scanf("%s",userName); | |
if(del(userName,&TheLink)) | |
printf("删除成功\n"); | |
printf("\n#请输入要进行的操作(按5打印菜单):"); | |
break; | |
printf("\n#请输入要进行的操作(按5打印菜单):"); | |
case '4' : | |
printf("\n请输入要查找的联系人姓名:\n"); | |
scanf("%s",userName); | |
if(findId(userName,&TheLink)) | |
printf("您要查找的信息 姓名:%s 电话:%s\n",TheLink.current->userName,TheLink.current->telNum); | |
printf("\n#请输入要进行的操作(按5打印菜单):"); | |
break; | |
case '5' : | |
printf("1.显示当前联系人\n"); | |
printf("2.新加联系人\n"); | |
printf("3.删除联系人\n"); | |
printf("4.搜索联系人\n"); | |
printf("\n(请输入要进行的操作:)"); | |
break; | |
default : | |
break; | |
} | |
} | |
} | |
/*******************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment