头文件:
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
typedef struct { /*通讯录结点类型*/
char num[30]; /*编号*/
char name[30]; /*姓名*/
char sex[30]; /*性别*/
char phone[30]; /*电话*/
char addr[30]; /*地址*/
} DataType;
typedef struct node { /*结点类型定义*/
DataType data; /*结点数据域*/
struct node *next; /*结点指针域*/
} ListNode;
typedef ListNode *LinkList;
LinkList initList(ListNode *&L);
LinkList CreateList(ListNode *&L);
void InsertNode(LinkList &L,ListNode *p);
LinkList ListFind(LinkList L);
void DelNode(LinkList L);
int ListLength(LinkList L);
void PrintList(LinkList L);
LinkList BubblePoint(LinkList L);
void load(LinkList &L);
int Save(LinkList L);
int Scan(LinkList L);
void xiugai(LinkList L);
函数实现:
#include "shixun.h"
LinkList initList(ListNode *&L)
{
L=(ListNode*)malloc(sizeof(ListNode));
L->next=NULL;
return L;
}
/*******尾插法建立带头结点的通讯录链表算法*******/
LinkList CreateList(ListNode *&L)
{
ListNode *p,*r;
char flag='y';
r=L;
while(r->next!=NULL)
r=r->next;
while (flag=='y')
{
p=(ListNode *)malloc(sizeof(ListNode)); /*申新结点*/
printf("num name sex phone adress \n");
printf("-----------------------------------------------\n");
printf("\n num:\n");
scanf("%s",p->data.num);
printf("\n name:\n");
scanf("%s",p->data.name);
printf("\n sex:\n");
scanf("%s",p->data.sex);
printf("\n phone:\n");
scanf("%s",p->data.phone);
printf("\n adress:\n");
scanf("%s",p->data.addr);
r->next=p;
r=p;
printf("go on to build the form?(y/n):");
scanf("%s",&flag);
}
r->next=NULL;
return L; /*终端结点指针置空*/
}
/*********在通讯录链表L中插入结点************/
void InsertNode(LinkList &L,ListNode *p)
{
ListNode *p1,*p2;
p1=L;
p2=p1->next;
while(p2!=NULL && strcmp(p2->data.num,p->data.num)<0)
{
p1=p2; /*p1指向刚访问过的结点*/
p2=p2->next; /*p2指向表的下一个结点*/
}
p1->next=p; /*插入p所指向的结点*/
p->next=p2;/*连接表中剩余的结点*/
}
/**********有序通讯录链表的查找 ****************/
LinkList ListFind(LinkList L)
{
ListNode *p;
char num[5];
char name[9];
char pp;
printf("==================\n");
printf(" a. locate form the num \n");
printf(" b. locate from the name \n");
printf("==================\n");
printf(" your choice: ");
p=L->next;
scanf("%s",&pp);
if (pp=='a'||pp=='A')
{
printf("input the num you want to locate:");
scanf("%s",num);
while (p!=NULL &&strcmp(p->data.num,num)!=0) p=p->next;
if (p==NULL)
printf("error");
else
return p;
}
else
if (pp=='b'||pp=='B')
{
printf(" input the name you want to locate:");
scanf("%s",name);
while(p!=NULL&&strcmp(p->data.name,name)!=0) p=p->next;
if (p==NULL)
printf("error");
else
return p;
}
}
/********通讯录链表上的结点删除*****************/
void DelNode(LinkList L)
{
char cho;
LinkList p,q;
q=L;
p=ListFind(L); /*调用查找函数*/
if (p==NULL)
{
printf("dont have the person\n");
exit(0);
}
else
{
printf("do you want to delete the person?(y/n):");
scanf("%s",&cho);
if (cho=='y'||cho=='Y')
{
while ((q!=NULL)&&(q->next!=p))
{
q=q->next;
}
if(q==NULL)
{
printf("error");
}
else
{
q->next=p->next; /*删除结点*/
free(p); /*释放被删结点空间*/
printf("success\n");
}
}
}
}
/********通讯录链表的输出函数 **********/
void PrintList(LinkList L)
{
ListNode *p;
p=L->next;
printf("num name sex phone adress \n");
printf("----------------------------------------------------\n");
while (p!=NULL)
{
printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
printf("----------------------------------------------------\n");
p=p->next; /*后移一个结点*/
}
}
int ListLength(LinkList L)
{
LinkList p=L;
int i=0;
while (p->next!=NULL)
{
i++;
p=p->next;
}
return(i);
}
int Save(LinkList L)//保存
{
FILE* fp;
LinkList p;
fp=fopen("D:\q","wb+");/*以只写方式打开二进制文件*/
if(fp==NULL) /*打开文件失败*/
{
printf("\n=====>open file error!\n");
return 0;
}
p=L->next;
while(p)
{
if(fwrite(p,sizeof(ListNode),1,fp)==1)/*每次写一条记录或一个节点信息至文件*/
{
p=p->next;
}
else
{break; }
}
fclose(fp); /*关闭此文件*/
return 1;
}
void xiugai(LinkList L)
{
ListNode *p,*q;
p=ListFind(L); /*调用查找函数*/
if (p==NULL)
{
printf("dont have the person\n");
return;
}
else if(p!=NULL)
{
printf("please input the persons information");
printf("\n num:\n");
scanf("%s",p->data.num);
printf("\n name:\n");
scanf("%s",p->data.name);
printf("\n sex:\n");
scanf("%s",p->data.sex);
printf("\n phone:\n");
scanf("%s",p->data.phone);
printf("\n adress:\n");
scanf("%s",p->data.addr);
}
}
主函数:
#include "shixun.h"
int main()
{
int select;
char ch;
LinkList p,r;
int length;
FILE *fp; /*文件指针*/
LinkList L=initList(L);
r=L;
fp=fopen("D:\q","ab+"); /*以追加方式打开一个二进制文件,可读可写,若此文件不存在,会创建此文件*/
if(fp==NULL)
{
printf("\n=====>can not open file!\n");
exit(0);
}
while(!feof(fp))
{
p=(ListNode*)malloc(sizeof(ListNode));
if(fread(p,sizeof(ListNode),1,fp)==1) /*一次从文件中读取一条学生成绩记录*/
{
p->next=NULL;
r->next=p;
r=p; /*r指针向后移一个位置*/
}
}
r->next=NULL;
fclose(fp);
int j=1;
while (j)
{
printf("\n\n\n\n\n");
printf("\t\t\t\ttongxinlulianbiao\n");
printf("\n\t\t\t******************************");
printf("\n\t\t\t*1 buid *");
printf("\n\t\t\t*2 insert *");
printf("\n\t\t\t*3 locate *");
printf("\n\t\t\t*4 deleat *");
printf("\n\t\t\t*5 output *");
printf("\n\t\t\t*6 count *");
printf("\n\t\t\t*7 xiugai *");
printf("\n\t\t\t*0 exit *");
printf("\n\t\t\t******************************");
scanf("%d",&select);
switch(select)
{
case 1:
{
printf("**********************************\n");
printf("* buid *\n");
printf("**********************************\n");
CreateList(L);
system("cls");
break;
}
case 2:
{
printf("**********************************\n");
printf("* build the form *\n");
printf("**********************************\n");
printf("num name sex phone adress \n");
printf("************************************* \n");
p=(ListNode *)malloc(sizeof(ListNode)); /*申请新结点*/
printf("\n num:\n");
scanf("%s",p->data.num);
printf("\n name:\n");
scanf("%s",p->data.name);
printf("\n sex:\n");
scanf("%s",p->data.sex);
printf("\n phone:\n");
scanf("%s",p->data.phone);
printf("\n adress:\n");
scanf("%s",p->data.addr);
InsertNode(L,p);
system("cls");
break;
}
case 3:
{
printf("***********************************\n");
printf("* locate *\n");
printf("***********************************\n");
p=ListFind(L);
if (p!=NULL)
{
printf("num name sex phone adress \n");
printf("--------------------------------------------------\n");
printf("%s,%s,%s,%s,%s\n",p->data.num,p->data.name,p->data.sex,p->data.phone,p->data.addr);
printf("---------------------------------------------------\n");
}
else printf("dont have the person\n");
break;
}
case 4:
{
printf("***********************************\n");
printf("* deleat *\n");
printf("***********************************\n");
DelNode(L); /*删除结点*/
break;
}
case 5:
{
printf("************************************\n");
printf("* output *\n");
printf("************************************\n");
PrintList(L);
break;
}
/*case 6:
{
BubblePoint(L);
PrintList(L);
break;
}*/
case 6:
{
length=ListLength(L);
printf("length=%d",length);
break;
}
case 7:
{
xiugai(L);
}
case 0:
{
/*printf("if you want to save the form(y/n):");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')*/
Save(L);
printf("=====>thanks");
getchar();
break;
}
}
}
}