/*简单通讯录*/
#include <stdio.h>
#include <stdlib.h>
struct address{
char name[20];
char street[20];
char city[10];
char state[10];
char zip[7];
struct address *next;
struct address *prior;
}list_entry;
struct address *start;
struct address *last;
void enter();
void display();
void search();
void save();
void load();
void list();
struct address *find(char *name);
struct address *dls_store(struct address *,struct address *);
main()
{
clrscr();
start=last=NULL;
load();
for (;;){
switch(menu_select()){
case 1:enter();break;
case 2:delete();break;
case 3:list();break;
case 4:search();break;
case 5:save();break;
case 6:exit(0);
}
}
}
menu_select()
{
char c;
printf("1.Enter\n");
printf("2.Delete\n");
printf("3.List\n");
printf("4.Search\n");
printf("5.Save\n");
printf("6.Quit\n");
do{
printf("\nEnter you choice:");
c=getche();
}while (c<'0'||c>'6');
return c-48;
}
void enter()
{
struct address *info;
for (;;){
info=(struct address *)malloc(sizeof(list_entry));
if (!info){
printf("\nout of memory");
return;
}
inputs("\nenter name:",info->name,19);
if (!info->name[0]) break;
inputs("enter street:",info->street,19);
inputs("enter city:",info->city,9);
inputs("enter state:",info->state,9);
inputs("enter zip:",info->zip,6);
start=dls_store(info,start);
}
}
inputs(char *prompt,char *s,int count)
{
char p[255];
do{
printf(prompt);
gets(p);
if (strlen(p)>count) printf("\ntoo long\n");
}while (strlen(p)>count);
strcpy(s,p);
}
struct address *dls_store(struct address *i,struct address *top)
{
struct address *old,*p;
if (last==NULL){
i->next=NULL;
i->prior=NULL;
last=i;
return i;
}
p=top;
old=NULL;
while(p){
if (strcmp(p->name,i->name)<0){
old=p;p=p->next;
}
else{
if(p->prior){
p->prior->next=i;
i->next=p;
i->prior=p->prior;
p->prior=i;
return top;
}
i->next=p;
i->prior=NULL;
p->prior=i;
return i;
}
}
old->next=i;
i->next=NULL;
i->prior=old;
last=i;
return start;
}