上一下程序把:
/******头文件(.h)***********/
#include "stdio.h" /*I/O函数*/
#include "dos.h" /*dos接口函数*/
#include "conio.h" /*屏幕操作函数*/
#include "stdlib.h" /*其它说明*/
#include "string.h" /*字符串函数*/
/*#include "mem.h" /*内存操作函数*/
#include "ctype.h" /*字符操作函数*/
/*#include "alloc.h" /*动态地址分配函数*/
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct PhoneCard)
struct PhoneCard
{
char name[20];
char num [20];
char address[20];
char code[10];
struct PhoneCard *prev; /*前向指针*/
struct PhoneCard *next; /*后向指针*/
};
struct PhoneBook
{
int Count; /*双链表的节点数*/
struct PhoneCard *head;/*记录双链表的第一个节点*/
struct PhoneCard *last; /*记录双链表的最后一个节点*/
struct PhoneCard *current; /*记录双链表的当前节点*/
};
int n;
FILE *fp;
struct PhoneCard *p0;
struct PhoneBook *Book;
void creat() /*创建双链表*/
{
Book->head=NULL;
Book->last=NULL;
Book->current=NULL;
Book->Count=0;
}
/*显示第一页*/
void DisplayFirstPage()
{
Book->current = Book->head;
if(Book->current == NULL)
printf("There is no record now!");
else
printf("First record is found!");
}
/*显示上一页*/
void DisplayPriorPage()
{
if(Book->current!= NULL)
{
if(Book->current->prev != NULL)
{
Book->current = Book->current->prev;
printf("Prior record is found!");
}
else
printf("Has Reached the first record!");
}
else
{
printf("Current card is empty,no prior record!");
}
}
/*显示下一页*/
void DisplayNextPage()
{
if(Book->current != NULL)
{
if(Book->current->next != NULL)
{
Book->current = Book->current->next;
printf("Next record is found!");
}
else
printf("Has Reached the Last record!");
}
else
{
printf("Current card is empty,no next record!");
}
}
/*显示最后页*/
void DisplayLastPage()
{
Book->current =Book->last;
if(Book->current == NULL)
printf("There is no record now!");
else
printf("Last record is found!");
}
void Save() /*保存信息*/
{
fp=fopen("Book.txt","w");
for(p0=Book->head;p0!=NULL;p0=p0->next)
{
fwrite(p0,LEN,1,fp);
}
}
void search ()
{
struct PhoneCard* pstr;
if(Book->head==NULL)
{printf("空表"); return;}
else
{
char Searchname[20];
pstr = Book->head; /*从头节点开始一个节点一个节点查找*/
printf("The Searchname is:");
scanf("%s",Searchname);
while(pstr!=NULL){
if(strcmp(pstr ->name,Searchname)==0)
{
Book->current = pstr;
return ;/*当找到符合查找条件的姓名时跳出*/
}
else pstr=pstr ->next;
}
printf("没有发现记录");/*若没有找到则提示*/
}
return ;
}
int length(struct PhoneBook *book) /*计算双链表的节点数*/
{struct PhoneCard *Card=book->head;
int len=0;
while(Card)
{len++;
Card=Card->next;
}
return len;
}
void del() /*删除链表节点*/
{struct PhoneCard *p1,*p2;
if (Book->head==NULL)
{ printf("\nlist null!\n");return;}
p1=Book->head;
if(Book->current==p1)
{p2=p1->next;
p2->prev=NULL;
Book->head=p2;
}
else
{struct PhoneCard *p3;
p3=Book->current;
p2=p3->prev;
p1=p3->next;
p2->next=p1;
p1->prev=p2;
Book->current=p2;
}
return;
}
void insert() /*添加链表节点*/
{struct PhoneCard *p1,*p2,*p3,*another;
another= (struct PhoneCard *)malloc(LEN); /*申请空间*/
another->next=NULL; /*新结点的后继为空*/
another->prev=NULL; /*新结点的前驱为空*/
scanf("%s%s%s%s",another->name,another->num,another->address,another->code);
if((fp=fopen("Book.txt","wb"))==NULL)
{printf("文件不能打开!\n");
return;
}
if (fwrite(another,LEN,1,fp)!=1)
printf("file write error\n");
p1=Book->head;p2=another;
if(Book->head==NULL)
{Book->head=p2;p2->next=NULL;}
else
{
while((strcmp(p2->name,p1->name)>0)&&(p1->next!=NULL))
{
p3=p1;
p1=p1->next;
}
if(strcmp(p2->name,p1->name)<=0)
{if(Book->head==p1)
{p2->next=p1;p2->prev=NULL;Book->head=p2;p1->prev=p2;}
else
{p2->next=p3->next;p2->prev=p3;p1->prev=p2;p3->next=p2;}
}
else
{p1->next=p2;p2->next=NULL;p2->prev=p1;Book->last=p2;}
}
return;
}
/*退出整个程序*/
void Quit()
{
clrscr();
exit(0);
}
/*******主函数开始**********/
void main() /*用switch语句提供功能选择*/
{ int ch;
scanf("%d",&ch);
creat();
do
{printf("\n\n\n\n\tWELCOME TO USE liushiwei's Address book");/*显示提示的信息*/
printf("\n\n\t\tPlease make a choice below:");
printf("\n\t\t0.第一页");
printf("\n\t\t1.上一页");
printf("\n\t\t2.下一录");
printf("\n\t\t3.最后页");
printf("\n\t\t4.添加");
printf("\n\t\t5.插入");
printf("\n\t\t6.删除");
printf("\n\t\t7.检索");
printf("\n\t\t8.保存");
printf("\n\t\t9.退出");
printf("\n\n\n");
printf("\tInput Your Choice:");
switch(ch)
{
case 0:DisplayFirstPage();break; /*显示第一条记录*/
case 1:DisplayPriorPage();break; /*显示上一条记录*/
case 2:DisplayNextPage();break; /*显示下一条记录*/
case 3:DisplayLastPage();break; /*显示最后一条记录*/
case 4:append();break; /*添加记录*/
case 5:insert();break; /*插入记录*/
case 6:del();break; /*删除记录*/
case 7:search();break; /*按名字检索记录*/
case 8:save();break; /*保存文件*/
case 9:Quit(); /*退出*/
default:
printf("\n\t*********************************\n");
printf("\n\t The num should 0-10!!! \n");
printf("\n\t**********************************");
break;
}
}while(1);
}