从自己U盘里复制到。。。不鄙视吧。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma pack(1)
typedef struct data
{
char addr[50];
char name[30];
long teleno;
struct data *next;
}* L,N;
#pragma pack()
L load(const char *dir)
{
FILE *pfile = fopen(dir,"rb");
if(pfile == NULL)
return NULL;
L head,tail,node;
int flag=1;
//fwrite (const void*, size_t, size_t, FILE*);
node = (L) malloc( sizeof( N ) );
while( fread( node, sizeof(N)-sizeof(L), 1,pfile ) )
{
if(flag)
{
flag=0;
tail=head=node;
}
else
tail= (tail->next=node);
node = (L) malloc( sizeof( N ) );
}
free(node);
tail->next=NULL;
if(fclose(pfile)) return NULL;
return head;
}
int save(const char *dir,L head)
{
FILE *pfile = fopen(dir,"wb");
if(pfile == NULL)
return 0;
while( head )
{
fwrite(head,sizeof(N)-sizeof(L),1,pfile);
head=head->next;
}
if(fclose(pfile)) return 0;
return 1;
}
//查看电话本记录
void look(L head)
{
L p=head;
if(head == NULL)
printf("There is no any record yet.\n ");
else
{
printf("name\t\tnumber\t\taddr\n");
printf("===========================================================\n");
for (;p!=NULL;p = p->next)
{
printf("%s\t\t%ld\t\t%s\n",p->name,p->teleno,p->addr);
}
}
}
//插入新记录
L insert(L head)
{
L p1,p2,p = head;
//读入数据
p1 = (L) malloc(sizeof(N));
printf("please enter information:\n");
printf("name:");
scanf("%s",p1->name);
printf("telepone:");
scanf("%ld",&p1->teleno);
printf("address:");
scanf("%s",p1->addr);
//如果链表为空
if (head == NULL)
{
head = p1;
p1->next = NULL; return head;
}
// 查找插入位置
if( (strcmp(p1->name,p->name)<0) )
{
p1->next = head;
return p1;
}
while((strcmp(p1->name,p->name)>0)&&(p->next!=NULL))
{
p2 = p;
p = p->next;
}
//如果找到位置
if (strcmp(p1->name,p->name)<=0)
{
//如果已经有该姓名
if (strcmp(p1->name,p->name)==0)
{
printf("it exist already.\n");
}
else//新姓名则插入到链表中
{
p2 ->next = p1;
p1 ->next = p;
}
}
else//没有找到位置,则插入到链尾
{
p ->next = p1;
p1->next = NULL;
}
return head;
}
//查找相应记录
void lookup(L head)
{
char name[20];
L p = head;
printf("enter the name to search:\n");
scanf("%s",name);
//循环进行查找
while ((strcmp(p->name,name)!=0)&&(p->next!=0))
{
p = p->next;
}
//如果找到相应记录则显示
if (strcmp(p->name,name)==0)
{
printf("name\t\tnumber\t\taddr\n");
printf("===========================================================\n");
printf("%s\t\t%ld\t\t%s\n",p->name,p->teleno,p->addr);
}
else //如果没有找到则给出提示字串
{
printf("no such record.\n");
}
}
L del(L head)
{
if(!head) return NULL;
char name[100];
scanf("%s",name);
L node=head,pre=node;
while( node->next && strcmp(name,node->name) != 0 )
node = (pre=node)->next;
if( strcmp(node->name,name)==0)
{
if(node != head)
pre->next=node->next;
else
head=node->next;
free(node);
}
return head;
}
int main()
{
int choice;
L head = NULL;
if( (head=load("TelBook.bin")) == NULL) printf("No File Or Load Failed\n");
else printf("Succeed\n");
do
{
printf("enter your choice:\n");
printf("1. 查看\n2. 新增\n3. 查找\n4. 删除\n5. 退出\n");
scanf("%d",&choice);
switch (choice)
{
case 1: look(head);break;
case 2: head = insert(head);save("TelBook.bin",head);break;
case 3: lookup(head);break;
case 4: head = del(head);save("TelBook.bin",head);break;
case 5: exit(0);break;
default:printf("enter choice 1-5:\n");scanf("%d",&choice);
}
} while (true);
}
温馨提示:内容为网友见解,仅供参考