单链表通讯录

 

 
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
 
#define LEN sizeof(struct student)
 
struct student 
{
char name[10];
char num[10];
char phone[10];
char address[10];
struct student *next;
};
 
void show();
struct student *append(struct student *head);
struct student *modify(struct student *head);
struct student *del(struct student *head);
void quit();
void print(struct student *head);
void save(struct student *head);
void search(struct student *head);
 
void main()
{
system("color 1e");
struct student *head=NULL;
FILE *fp,*fp1;
if((fp=fopen("summer.txt","r"))==NULL)
{
printf("Cache file is not exist,file is creating now...");
Sleep(3000);
fp1=fopen("summer.txt","w");
printf("/n");
printf("/n");
printf("file creating success!/n");
printf("/n");
fclose(fp1);
}
while(1)
{
int n;
show();
printf("choose one item:");
scanf("%d",&n);
getchar();
switch(n)
{
case 1:head=append(head);break;
case 2:head=modify(head);break;
case 3:head=del(head);break;
case 4:save(head);break;
case 5:print(head);break;
case 6:search(head);break;
case 7:quit();break;
default:printf("No this Choice!/n");printf("Please Wait For 3 Sec.../n");Sleep(3000);break;
}
system("cls");
}
}
 
void show()
{
printf("1.append/n");
printf("2.modify/n");
printf("3.del/n");
printf("4.save/n");
printf("5.print/n");
printf("6.search/n");
printf("7.quit/n");
}
 
struct student *append(struct student *head)
{
struct student *phead,*prear;
phead=prear=head;
struct student *p;
p=(struct student *)malloc(LEN);
printf("enter name:");
gets(p->name);
printf("enter num:");
gets(p->num);
printf("enter phone:");
gets(p->phone);
printf("enter address:");
gets(p->address);
if(head==NULL)
{
head=p;
p->next=NULL;
}
else
{
while((strcmp(p->name,prear->name)>0)&&(prear->next!=NULL))
{
phead=prear;
prear=prear->next;
}
if((strcmp(p->name,prear->name)<=0))
{
phead->next=p;
p->next=prear;
}
else
{
prear->next=p;
p->next=NULL;
}
}
printf("append success!/n");
printf("Please Wait For 3 Sec.../n");
Sleep(3000);
return head;
}
 
void quit()
{
exit(0);
}
 
void print(struct student *head)
{
struct student *p=head;
if(head!=NULL)
{
do
{
printf("name:%s/n",p->name);
printf("num:%s/n",p->num);
printf("phone:%s/n",p->phone);
printf("address:%s/n",p->address);
p=p->next;
}
while(p!=NULL);
}
else
{
printf("No Person!");
}
printf("Please Wait For 3 Sec.../n");
Sleep(3000);
}
 
struct student *modify(struct student *head)
{
char name[10];
struct student *phead,*prear;
phead=prear=head;
// struct student *p=(struct student *)malloc(LEN);
printf("modify which name:");
gets(name);
//getchar();
if(head==NULL)
{
printf("No person to modify!");
}
else
{
while((strcmp(prear->name,name)==0)&&(prear->next!=NULL))
{
phead=prear;
prear=prear->next;
}
if((strcmp(phead->name,name)==0))
{
printf("name modify to:/n");
gets(phead->name);
printf("num modify to:/n");
gets(phead->num);
printf("phone modify to:/n");
gets(phead->phone);
printf("address modify to:/n");
gets(phead->address);
}
else
{
printf("Sorry,no this person!/n");
}
}
printf("Please Wait For 3 Sec...");
Sleep(3000);
return head;
}
 
struct student *del(struct student *head)
{
char name[10];
struct student *phead,*prear;
phead=prear=head;
printf("del which name:");
gets(name);
if(head==NULL)
{
printf("No person to del!");
}
else
{
while(prear!=NULL)
{
if((strcmp(prear->name,name)==0))
{
if(head==prear)
{
head=prear->next;
// head=phead->next;
}
else
{
phead->next=prear->next;
}
free(prear);
printf("del success!/n");
break;
}
phead=prear;
prear=prear->next;
}
 
}
printf("Please Wait For 3 Sec.../n");
Sleep(3000);
return head;
}
 
void save(struct student *head)
{
FILE *fp=fopen("summer.txt","w");
struct student *prear=head;
while(prear!=NULL)
{
fprintf(fp,"name saving:%s/n",prear->name);
fprintf(fp,"num saving:%s/n",prear->num);
fprintf(fp,"phone saving:%s/n",prear->phone);
fprintf(fp,"address saving:%s/n",prear->address);
prear=prear->next;
}
fclose(fp);
printf("save success!/n");
printf("Please Wait For 3 Sec.../n");
Sleep(3000);
}
 
void search(struct student *head)
{
char name[10];
struct student *prear,*phead;
phead=prear=head;
printf("search which name:");
gets(name);
if(head==NULL)
{
printf("No person!");
}
else
{
while(prear!=NULL)
{
if((strcmp(prear->name,name)==0))
{
printf("name is:");
puts(prear->name);
printf("num is:");
puts(prear->num);
printf("phone is:");
puts(prear->phone);
printf("address is:");
puts(prear->address);
printf("You find it!");
}
else
{
printf("Not found!/n");
}
phead=prear;
prear=prear->next;
}
}
printf("Please Wait For 3 Sec.../n");
Sleep(3000);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值