您的位置:

c语言链表通讯录问题总结,链表输出函数C语言

本文目录一览:

用C语言建立一个链表实现一个通讯录,

#include stdio.h

#include stdlib.h

#include conio.h

#include string.h

typedef unsigned long ulong;

typedef struct _list {

char name[16];

char addr[64];

ulong phone;

ulong qq;

struct _list* next;

} *node, list;

/* insert a node */

node Insert( node* head, node pos, list* l )

{

node tmp;

tmp = ( node )malloc( sizeof( list ) );

strcpy( tmp-name, l-name );

strcpy( tmp-addr, l-addr );

tmp-phone = l-phone;

tmp-qq = l-qq;

tmp-next = pos ? pos-next : *head;

if ( pos ) {

pos-next = tmp;

} else {

*head = tmp;

}

return tmp;

}

/* create a list */

node Create( void )

{

node head, t;

list input;

head = t = NULL;

printf( "请按 [姓名] [地址] [家庭电话] [qq] 的顺序输入\n" );

printf( "每行一组数据,输入空行结束:\n" );

while ( 1 ) {

if ( getchar() == '\n' ) break;

scanf( "%s%s%lu%lu", input.name, input.addr, input.phone, input.qq );

while ( getchar() != '\n' );

t = Insert( head, t, input );

}

return head;

}

/* view list */

void Print( node head )

{

while ( head ) {

printf( "%s\t%s\t%lu\t%lu\n", head-name, head-addr, head-phone, head-qq );

head = head-next;

}

putchar( '\n' );

}

/* merge sort */

node msort( node* head, int n )

{

int i, m;

node l, r, p, *x, *y;

if ( n 2 ) return *head;

m = n/2;

p = l = r = *head;

for ( i = m; i 0; --i )

p = r, r = r-next;

p-next = NULL;

l = msort( l, m );

r = msort( r, n - m );

x = p;

while ( l r ) {

*x = l-qq r-qq ? (y = l, l) : (y = r, r);

*y = (*y)-next; x = (*x)-next;

}

l = l ? l : r ? r : NULL;

*x = l; *head = p;

return p;

}

/* sort wrapper */

void Sort( node* head )

{

int i;

node tmp = *head;

for ( i = 0; tmp; ++i, tmp = tmp-next );

msort( head, i );

}

int main( void )

{

node head = Create();

printf( "\n链表内容:\n" );

Print( head );

Sort( head );

printf( "\n排序之后:\n" );

Print( head );

getch();

return 0;

}

C语言关于链表的问题(通讯录)

struct people *find(char *name) /*查找*/

{struct people *p1,*p2;

char c;

clrscr();

if(head==NULL) {printf("\nlist null!\n");}

p1=head;

while(strcmp(name,p1-name)!=0p1-next!=NULL)

{p2=p1;

p1=p1-next;}

if(strcmp(name,p1-name)==0) /*如果找到*/

{printf("\tThe message of %s is:\n",name);

printf("\tName: %s\n\taddress: %s\n\tZip: %s\n\tPhone: %s\n",p1-name,p1-adress,p1-zip,p1-phone);

do /*子止录,对找到的信息操作*/

{

puts("\tWhat's you want to do!");

puts("\t1.Delect"); /*删除*/

puts("\t2.Reset"); /*修改*/

puts("\t3.Return"); /*返回*/

c=getch();}while(c!='1'c!='2'c!='3');

if(c=='1')

{if(p1==head) head=p1-next;

else p2-next=p1-next;

printf("\tDelect: %s\n",name);

sleep(1);

n--;}

else if(c=='2')

{printf(" Please input new address:");

scanf("%s",p1-adress);

printf(" Please input new zip:");

scanf("%s",p1-zip);

printf(" Please input new phone:");

scanf("%s",p1-phone);}

}

else printf("%s not been found!\n",name);

sleep(1);

}

main()

{

struct people *p1,*p;

char c,c1,s,name1[20],s1;

textbackground(GREEN); /*设置背景色*/

loop:

while(1) /*主目录*/

{do{clrscr();

puts("\t\tWELCOME");

puts("\t Please Select Key:");

puts("\t 1.Insert"); /*添加*/

puts("\t 2.Find"); /*查找*/

puts("\t 3.Del_All"); /*清空*/

puts("\t 4.Exit"); /*退出*/

c=getch();

}while(c!='1'c!='2'c!='3'c!='4');

while(c)

{if(c=='1')

{clrscr();

puts(" Please input the messages about:");

creat();

goto loop;

}

else if(c=='2')

{clrscr();

printf(" Who you want to find:");

gets(name1);

find(name1);

goto loop;}

else if(c=='3')

{clrscr();

puts(" Are you sure to delect all?(y/n)");

s=getch();

if(s=='y'||s=='Y')

{head=NULL;

n=0;

puts("\tDelect Over");

sleep(1);}

goto loop;

}

else if(c=='4')

{clrscr();

puts("\tThank you for your using!");

puts("\t Good bye!");

sleep(1);

exit(0);}

}

}

}

这是查找和主函数,哪出问题了?

一道C语言链表通讯录的题

#include stdio.h

#includeiostream.h

#include string.h

#include conio.h

#include stdlib.h

#define null 0 ;

struct record

{

char name[20];

char phone[20];

char adress[40];

char postcode[10];

char e_mail[30];

}student[30];

struct LinkList

{

struct record US;

struct LinkList *next;

}a;

struct LinkList *head=null;

int num=0;

FILE *fp;

int menu_select();

int adduser();

int list();

int search();

int display();

int add();

int listbyname();

int dele();

int save();

int exit();

void main()

{

system("cls");

for(;;)

{

switch(menu_select())

{

case 0:

adduser();

break;

case 1:

list();

break;

case 2:

search();

break;

case 3:

display();

break;

case 4:

add();

break;

case 5:

listbyname();

break;

case 6:

dele();

break;

case 7:

save();

break;

case 8:

exit(0);

}

}

}

menu_select()

{

int s;

int a;

cout"*_* 按任意键进入菜单! *_* "endl;

getch();

system("cls");

cout"\t\t********************MENU*********************\n"endl;

cout"\t\t 0. 输入记录"endl;

cout"\t\t 1. 显示记录 "endl;

cout"\t\t 2. 按姓名查找"endl;

cout"\t\t 3. 按电话号码查找"endl;

cout"\t\t 4. 增加用户"endl;

cout"\t\t 5. 按姓名排序"endl;

cout"\t\t 6. 删除记录"endl;

cout"\t\t 7. 记录保存文件"endl;

cout"\t\t 8. Quit"endl;

cout"\t\t***********************************************"endl;

do{

cout"\n 输入你的选择(0~8):";

cins;

a=s;

}

while (a0||a8);

return a;

}

adduser()

{

cout"\n\t\t**************** 请输入用户信息 ****************"endl;

cout"\n\t\t输入姓名:";

cinstudent[num].name;

cout"\n\t\t输入电话号码:";

cinstudent[num].phone;

cout"\n\t\t输入地址:";

cinstudent[num].adress;

cout"\n\t\t输入邮编:";

cinstudent[num].postcode;

cout"\n\t\t输入e-mail:";

cinstudent[num].e_mail;

num++;

cout"\n\t\t是否继续添加?(Y/N):";

char z;

cinz;

if((z=='y')||(z=='Y'))

adduser();

return(0);

}

list()

{

int i;

system("cls");

if(num!=0)

{

cout"\n\t\t*************** 以下为通讯录所有信息************";

for(i=0;inum;i++)

{

cout"\n\t\t姓名:"student[i].name;

cout"\n\t\t电话: "student[i].phone;

cout"\n\t\t地址: "student[i].adress;

cout"\n\t\t邮编: "student[i].postcode;

cout"\n\t\te-mail:"student[i].e_mail;

cout"\t\t";

if(i+1num)

{

cout"\n\t\t__________________________";

system("pause");

}

}

cout"\n\t\t************************************************";

}

else

cout"\n\t\t通讯录中无任何纪录";

cout"\n\t\t按任意键返回主菜单:";

getch();

return(0);

}

search()

{

int mark=0;

int i;

int a=0;

cout"\n\t\t***************** 按姓名查找 *******************";

char name[20];

cout"\n\t\t请输入姓名:";

cinname;

for(i=a;inum;i++)

{

if(strcmp(student[i].name,name)==0)

{

cout"\n\t\t************* 以下是您查找的用户信息 ***********";

cout"\n\t\t姓名: "student[i].name;

cout"\n\t\t电话: "student[i].phone;

cout"\n\t\t地址: "student[i].adress;

cout"\n\t\te-mail:"student[i].e_mail;

cout"\n\t\t************************************************";

mark++;

if((i+1)num)

{

cout"\n\t\t是否继续查找相同名字的用户信息:(y/n)";

char y;ciny;

if((y=='y')||(y=='Y'))

{

a=i;

continue;

}

else

return(0);

}

else

{

cout"\n\t\t按任意键返回主菜单";

getch();

return(0);

}

}

}

if(mark==0)

{

cout"\n\t\t没有相同姓名的用户纪录";

cout"\n\t\t按任意键返回主菜单";

getch();

return(0);

}

}

display()

{

int mark=0;

int i;

int a=0;

cout"\n\t\t****************** 按电话查找 ******************";

char phone[10];

cout"\n\t\t请输入电话号码:";

cinphone;

for(i=0;inum;i++)

{

if(strcmp(student[i].phone,phone)==0)

{

cout"\n\t\t************** 以下是您查找的用户信息 **********";

cout"\n\t\t姓名: "student[i].name;

cout"\n\t\t电话: "student[i].phone;

cout"\n\t\t地址: "student[i].adress;

cout"\n\t\te-mail:"student[i].e_mail;

cout"\n\t\t************************************************";

cout"\n\t\t按任意键返回主菜单:";

mark++;

getch();

return(0);

}

}

if(mark==0)

{

cout"\n\t\t没有改用户的信息";

cout"\n\t\t按任意键返回主菜单";

getch();

return(0);

}

return(0);

}

add()

{

int i;

if((fp=fopen("student.bin","wb"))==NULL)

{

cout"\n\t\t文件打开失败";

}

for (i=0;inum;i++)

{

if (fwrite(student[i],sizeof(struct record),1,fp)!=1)

{

cout"\n\t\t写入文件错误!\n";

}

cout"\n\t\t**************** 请输入用户信息 ****************"endl;

cout"\n\t\t输入姓名:";

cinstudent[num].name;

cout"\n\t\t输入电话号码:";

cinstudent[num].phone;

cout"\n\t\t输入地址:";

cinstudent[num].adress;

cout"\n\t\t输入邮编:";

cinstudent[num].postcode;

cout"\n\t\t输入e-mail:";

cinstudent[num].e_mail;

num++;

cout"\n\t\t是否继续添加?(Y/N):";

if(getch()=='y')

adduser();

return(0);

}

fclose(fp);

cout"\n\t\t通讯录文件已保存";

cout"\n\t\t按任意键退出程序\n\t\t";

exit(0);

return(0);

}

void deletebyphone()

{

int i,j;

int deletemark=0;

char phone[20];

cout"\n\t\t请输入要删除用户电话号码:";

cinphone;

if(num==0)

{

cout"\n\t\t对不起,文件中无任何纪录";

cout"\n\t\t按任意键返回主菜单";

getch();

return;

}

for (i=0;inum;i++)

{

if(strcmp(student[i].phone,phone)==NULL)

{

cout"\n\t\t以下是您要删除的用户纪录:";

cout"\n\t\t姓名: "student[i].name;

cout"\n\t\t电话: "student[i].phone;

cout"\n\t\t地址: "student[i].adress;

cout"\n\t\te-mail:"student[i].e_mail;

cout"\n\t\t是否删除?(y/n)";

char u;cinu;

if((u=='y')||(u=='Y'))

{

for(j=i;jnum-1;j++)

student[j]=student[j+1];

num--;

deletemark++;

cout"\n\t\t删除成功";

cout"\n\t\t是否继续删除?(y/n)";

char v;cinv;

if((v=='y')||(v=='Y'))

deletebyphone();

return;

}

else

return;

}

continue;

}

if(deletemark==0)

{

cout"\n\t\t没有该用户的纪录";

cout"\n\t\t是否继续删除?(y/n)";

char m;cinm;

if((m=='y')||(m=='Y'))

deletebyphone();

return;

}

}

void deletebyname()

{

int a=0;

int findmark=0;

int j;

int deletemark=0;

int i;

char name[20];

cout"\n\t\t请输入要删除用户姓名:";

cinname;

for(i=a;inum;i++)

{

if(strcmp(student[i].name,name)==NULL)

{

cout"\n\t\t以下是您要删除的用户纪录:";

findmark++;

cout"\n\t\t________________________________";

cout"\n\t\t姓名: "student[i].name;

cout"\n\t\t电话: "student[i].phone;

cout"\n\t\t地址: "student[i].adress;

cout"\n\t\te-mail:"student[i].e_mail;

cout"\n\t\t________________________________";

cout"\n\t\t是否删除?(y/n)";

char n;cinn;

if((n=='y')||(n=='Y'))

{

for(j=i;jnum-1;j++)

student[j]=student[j+1];

num--;

deletemark++;

cout"\n\t\t删除成功";

if((i+1)num)

{

cout"\n\t\t是否继续删除相同姓名的用户信息?(y/n)";

char l;cinl;

if((l=='y')||(l=='Y'))

{

a=i;

continue;

}

}

cout"\n\t\t是否继续删除?(y/n)";

char o;cino;

if((o=='y')||(o=='Y'))

deletebyname();

return;

}

if((i+1)num)

{

cout"\n\t\t是否继续删除相同姓名的用户信息?(y/n)";

char p;cinp;

if((p=='y')||(p=='Y'))

{

a=i;

continue;

}

}

}

else

continue;

}

if((deletemark==0)(findmark==0))

{

cout"\n\t\t没有该用户的纪录";

cout"\n\t\t是否继续删除?(y/n)";

char q;cinq;

if((q=='y')||(q=='Y'))

deletebyphone();

return;

return;

}

else if(findmark!=0)

{

cout"\n\t\t没有重名信息";

cout"\n\t\t没有该用户的纪录";

cout"\n\t\t是否继续删除?(y/n)";

char r;cinr;

if((r=='y')||(r=='Y'))

deletebyphone();

return;

return;

}

}

dele()

{

int choic;

cout"\n\t\t1-按电话号码删除 2-按姓名删除";

cout"\n\t\t请选择:";

cinchoic;

switch (choic)

{

case 1:

deletebyphone();

break;

case 2:

deletebyname();

break;

}

return(0);

}

listbyname()

{

int i,j;

struct record tmp;

for(i=1;inum;i++)

{

if(strcmp(student[i].name,student[i-1].name)0)

{

tmp=student[i];

j=i-1;

do

{

student[j+1]=student[j];

j--;

}while ((strcmp(tmp.name,student[j].name)0j=0));

student[j+1]=tmp;

}

}

cout"\n\t\t排序成功,是否显示?(y/n)";

char x;cinx;

if((x=='y')||(x=='Y'))

list();

return(0);

}

save()

{

int j;

FILE*fp;

fp=fopen("student.txt","w");

if (fp==NULL)

cout"不能打开文件.";

if(num!=0)

{

for(j=0;jnum;j++)

{

fwrite(student,sizeof(student),1,fp);

}

}

cout"保存成功!";

fclose(fp);

return(0);

}

数据结构通讯录管理与链表(C语言)求指导.,本人菜鸟中的菜鸟

完全基于用户需求分析,系统设计分为六个子模块:添加信息和查询信息,删除信息,信息,显示所有信息,退出并保存信息。六个模块之间紧密结合起来,共享信息资源,形成一个完美的联系人管理解决方案。该框架的整体设计如下:

开始

该文件将被自动添加

载入保存的文件的内容,创建新的链接列表

进入while循环

输出菜单

输入选择开关语句被称为

输入是否是0

保存并退出

确定的关键

调用相关函数,

结束

没有

C语言数据结构:链表的应用--通讯录管理

/* HELLO.C -- Hello, world */

#includestdio.h

struct person

{

char name[8];

char tel[15];

char addr[50];

};

char filename[20]="E:\\txl.txt";

FILE *fp;

void creat();

void output();

void search();

void append();

void modify();

void delete();

main( )

{

int m;

creat();

while(1)

{

printf("\n\t\t*********欢迎使用通讯录信息管理系统*********\n\n");

printf("\n\t\t添加,请按1");

printf("\n\t\t查找,请按2");

printf("\n\t\t修改,请按3");

printf("\n\t\t删除,请按4");

printf("\n\t\t输出,请按5");

printf("\n\t\t退出,请按0\n");

printf("\n\t\t********************************************\n\n");

printf("Please select(0--5):");

scanf("%d",m);

if(m=0m=5)

{

switch(m)

{

case 1: append();

break;

case 2: search();

break;

case 3: modify();

break;

case 4: delete();

break;

case 5: output();

break;

case 0: exit();

}

printf("\n\n操作完毕,请再次选择!");

}

else

printf("\n\n选择错误,请再次选择!");

}

}

/*输入模块creat( ): 创建通讯录的子函数。*/

void creat()

{

struct person one;

long s1;

/*printf("\n请输入通讯录文件名:");

scanf("%s",filename); */

if((fp=fopen(filename,"at+"))==NULL)

{

printf("\n不能建立通讯录!");

exit();

}

fprintf(fp,"%-10s%-20s%-50s\n","姓名","电话号码","住址");

printf("\n请输入姓名、电话号码及住址(以0结束)\n");

scanf("%s",one.name);

while(strcmp(one.name,"0"))

{

scanf("%s%s",one.tel,one.addr);

fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);

scanf("%s",one.name);

}

fclose(fp);

}

/*输出模块output( ):输出通讯录中联系人通讯信息的子函数*/

void output()

{

struct person one;

if((fp=fopen(filename,"r"))==NULL)

{

printf("\n不能打开通讯录!");

exit();

}

printf("\n\n%20s\n","通 讯 录");

while(!feof(fp))

{

fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);

printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);

}

fclose(fp);

}

/*添加模块append( ):向通讯录中添加某人通讯信息的子函数*/

void append()

{

struct person one;

if((fp=fopen(filename,"a"))==NULL)

{

printf("\n不能打开通讯录!");

exit();

}

printf("\n请输入添加的姓名、电话号码及住址\n");

scanf("%s%s%s",one.name,one.tel,one.addr);

fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);

fclose(fp);

}

/*查找模块search( ):在通讯录中查找某人通讯信息的子函数*/

void search()

{

int k=0;

char namekey[8];

struct person one;

printf("\n请输入姓名:");

scanf("%s",namekey);

if((fp=fopen(filename,"rb"))==NULL)

{

printf("\n不能打开通讯录!");

exit();

}

while(!feof(fp))

{

fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);

if(!strcmp(namekey,one.name))

{

printf("\n\n已查到,记录为:");

printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);

k=1;

}

}

if(!k)

printf("\n\n对不起,通讯簿中没有此人的记录。");

fclose(fp);

}

/*修改模块modify( ):在通讯录中修改某人通讯信息的子函数*/

void modify()

{

int m,k=0;

long offset;

char namekey[8];

struct person one;

printf("\n请输入姓名:");

scanf("%s",namekey);

if((fp=fopen(filename,"r+"))==NULL)

{

printf("\n不能打开通讯录!");

exit();

}

while(!feof(fp))

{

offset=ftell(fp);

fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);

if(!strcmp(namekey,one.name))

{

k=1;

break;

}

}

if(k)

{

printf("\n已查到,记录为:");

printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);

printf("\n请输入新姓名、电话号码及住址:");

scanf("%s%s%s",one.name,one.tel,one.addr);

fseek(fp,offset,SEEK_SET);

printf("%ld",ftell(fp));

fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);

}

else

printf("\n对不起,通讯录中没有此人的记录。");

fclose(fp);

}

/*删除模块delete( ):在通讯录中删除某人通讯信息的子函数*/

void delete()

{

int m,k=0,flag;

long offset1,offset2;

char namekey[8], valid[4];

struct person one;

printf("\n请输入姓名:");

scanf("%s",namekey);

if((fp=fopen(filename,"r+"))==NULL)

{

printf("\n不能打开通讯录!");

exit();

}

while(!feof(fp))

{

offset1=ftell(fp);

fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);

if(!strcmp(namekey,one.name))

{

k=1;

break;

if(k)

{

printf("\n已查到,记录为");

printf("\n%-10s%-18s%-50s",one.name,one.tel,one.addr);

printf("\n确实要删除,按1;不删除,按0:");

scanf("%d",m);

if(m)

{

fseek(fp,offset1,SEEK_SET);

fprintf(fp,"%-10s%-20s%-50s\n","","","");

}

else

printf("\n对不起,通讯录中没有此人的记录。");

fclose(fp);

}

}

}

}

PIXTEL_MMI_EBOOK_20051###########################################################/PIXTEL_MMI_EBOOK_2005