当前位置:首页 » 编程语言 » c语言链表通讯录

c语言链表通讯录

发布时间: 2022-11-19 19:04:56

1. 用c语言编写一个通讯录系统,集合数据结构双向链表知识

//类似//#include "stdafx.h"
#include<iostream.h>
#include<string.h>

#include<iomanip.h>
class stu
{
char name[20];
double age,homephone,telphone;
char sex;
public:
stu(){}
stu(char n[20],char se,double ag,double ho,double te)
{
strcpy(name, n);
age=ag;
homephone=ho;
telphone=te;
}
friend void main();
}; void main()
{
cout<<"请选择您需要的操作!"<<endl;
cout<<"操作:"<<endl;
cout<<"(0)通讯录录入"<<endl;
cout<<"(1)增加人员"<<endl;
cout<<"(2)删除人员"<<endl;
cout<<"(3)修改数据"<<endl;
cout<<"(4)显示记录"<<endl;
cout<<"(5)退出"<<endl;
cout<<"选择相关操作请输入相对的括号里的阿拉伯数字!"<<endl;
stu *s[50];
int i=0;
int j=0;
bool flag2=0;
char p;
do
{
cin>>p;
if((p>='0'&&p<='5'))
flag2=1;
else
cout<<"指令错误!请重新输入:"<<endl;
}while(flag2==0); switch(p)
{ case '0': //(0)通讯录录入
{
char name[20];
double age,homephone,telphone;
char sex,c;
do{ cout<<"请输入姓名:"<<endl;
cin>>name;
cout<<"请输入性别:"<<endl;
cin>>sex;
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入家里的电话号码:"<<endl;
cin>>homephone;
cout<<"请输入移动电话号码:"<<endl;
cin>>telphone;
j++;
s[i]=new stu(name, sex, age, homephone , telphone);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
else
flag2=1;
}while(flag2==0);
}while(c=='y');
break; }
////////////////////////////////////////////////////////////////////
case '1': //(1)增加人员(Add)
{
char name[20];
double age,homephone,telphone;
char sex,c;
do{ cout<<"请输入姓名:"<<endl;
cin>>name;
cout<<"请输入性别:"<<endl;
cin>>sex;
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入家里的电话号码:"<<endl;
cin>>homephone;
cout<<"请输入移动电话号码:"<<endl;
cin>>telphone;
j++;
s[i]=new stu(name, sex, age, homephone , telphone);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
else
flag2=1;
}while(flag2==0);
}while(c=='y');
break; } case '2': //(2)删除人员(Delete)
{
char name[20];bool flag3=0;char c;
do{
cout<<"请输入您要删除的学生姓名:"<<endl;
cin>>name;
for(int h=0;h<i;h++)
{
if(strcmp(name,s[h]->name)==0)
{
flag3=1;
i--;
do{
s[h]=s[h+1];
h++;
}while(h<=i);
}
}
if(flag3==0)
cout<<"您要求删除的对象本来就不存在!请检查输入的正确性!";
cout<<"要继续删除吗?(y/n)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
break; }
case '3': //(3)修改数据(Alter)
{
char name[20],se;double ag,ho,te;flag2=0;
char c;
do
{
cout<<"请输入您要修改的学生的姓名:"<<endl;
cin>>name;
for(int h=0;h<i;h++)
{
if(strcmp(name,s[h]->name)==0)
{
flag2=1;
cout<<"请输入性别:"<<endl;
cin>>se;
cout<<"请输入年龄:"<<endl;
cin>>ag;
cout<<"请输入家里的电话号码:"<<endl;
cin>>ho;
cout<<"请输入移动电话号码:"<<endl;
cin>>te;
s[h]->sex=se;
s[h]->age=ag;
s[h]->homephone=ho;
s[h]->telphone=te;
cout<<"数据修改成功!";
}
}
if(flag2==0)
{
cout<<"您要修改的学生本来就不存在!请检查重新输入!"<<endl;
}
cout<<"想继续修改吗(y/n)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
break; }
case '4': //(4)显示记录(List)
{
cout<<"本系统所有通讯录的数据如下:"<<endl;
if(i==0)
cout<<"管理系统中没有录入数据或者数据已经被删除!"<<endl;
for(int k=0;k<i;k++)
{
cout<<k+1<<" "<<"姓名:"<<" "<<s[k]->name<<
"性别:"<<" "<<s[k]->sex<<"年龄:"<<" "<<s[k]->age
<<"家里的电话号码:"<<" "<<s[k]->homephone<<"移动电话号码:"
<<" "<<s[k]->telphone<<endl;
}
break; } }
cout<<"您想继续进行其他操作吗?(y/n)"<<endl;
bool flag4=0;
do
{
cin>>p;
if(p!='y'&&p!='n')
cout<<"指令错误!请重新输入!"<<endl;
else
flag4=1;
}while(flag4==0); if(p=='y')
cout<<"请输入操作代码(0 通讯录录入\n1 增加人员(Add)\n2 删除人员(Delete)\n3 修改数据(Alter)\n4 显示记录(List)\n 5 退出(Exit))"<<endl;
cin>>p;
for(int x=0;x<i;x++)
{
delete s[x];
cout<<"删除所有成员!"<<endl;
} }

2. 怎么建立一个通讯录啊,要用到链表知识,用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;
}

3. C语言利用链表建立一个通讯录,包括添加,修改,删除,学号查找,姓名查找五个功能,用五个调用函数。

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
struct student
{
char num[20];//学号
char name[20];//用户姓名
char phone[20];//电话号码
char addr[100];//通讯地址
struct student *next;
};

void insert(student* head)//添加一条记录
{
student *p=head;
student *newStud=(student*)malloc(sizeof(student));
printf("输入添加用户信息\n");
printf("学号:");
scanf("%s",newStud->num);
printf("姓名:");
scanf("%s",newStud->name);
printf("电话:");
scanf("%s",newStud->phone);
printf("地址:");
scanf("%s",newStud->addr);
while(p->next!=NULL)
{
if(strcmp(p->num,newStud->num)==0)
{
printf("此学号的用户已存在!\n");
return;
}
p=p->next;
}
p->next=newStud;
newStud->next=NULL;
printf("添加成功\n");
}

void update(student *head)//修改一条记录
{
student *p=head->next;
char num[20];
printf("输入待修改用户学号:");
scanf("%s",num);
while(p!=NULL)
{
if(strcmp(p->num,num)==0)
{
printf("输入修改后信息\n");
printf("学号:");
scanf("%s",&p->num);
printf("姓名:");
scanf("%s",&p->name);
printf("电话:");
scanf("%s",&p->phone);
printf("地址:");
scanf("%s",&p->addr);

printf("修改成功\n");
return;
}
p=p->next;
}
printf("不存在此学号的用户");
}
void delRecord(student *head) //删除一条记录
{
student *p1,*p2;
p1=head;
p2=p1->next;
char num[20];
printf("输入待删除用户学号:");
scanf("%s",num);
while(p2!=NULL)
{
if(strcmp(p2->num,num)==0)//找到则删除此用户
{
p1->next=p2->next;
free(p2);
printf("删除成功\n");
return;
}
p1=p2; //没找到则继续遍历
p2=p2->next;
}
printf("不存在此学号的用户\n");
}

void findByNum(student *head)//按学号查找
{
student *p=head->next;
char num[20];
printf("输入待查找用户学号:");
scanf("%s",num);
while(p!=NULL)
{
if(strcmp(p->num,num)==0)
{
printf("学号:%s\n",p->num);
printf("姓名:%s\n",p->name);
printf("电话:%s\n",p->phone);
printf("地址:%s\n",p->addr);
return;
}
p=p->next;
}
printf("不存在此学号的用户\n");
}

void findByName(student *head)//按姓名查找
{
student *p=head->next;
char name[20];
printf("输入待查找用户姓名:");
scanf("%s",name);
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
printf("学号:%s\n",p->num);
printf("姓名:%s\n",p->name);
printf("电话:%s\n",p->phone);
printf("地址:%s\n",p->addr);
return;
}
p=p->next;
}
printf("不存在此姓名的用户\n");
}

void main()
{
student *head=(student*)malloc(sizeof(student));
head->next=NULL;
char choice;

printf("\t*****************************\n");
printf("\t1,添加一条记录\n");
printf("\t2,修改一条记录\n");
printf("\t3,删除一条记录\n");
printf("\t4,按学号查找\n");
printf("\t5,按姓名查找\n");
printf("\t6,退出\n");
printf("\t请按键选择\n");
printf("\t*****************************\n");

while(true)
{
printf("请按键选择操作:\n");
fflush(stdin); //清除缓冲区
choice=getch();
switch(choice)
{
case '1':
insert(head);
break;
case '2':
update(head);
break;
case '3':
delRecord(head);
break;
case '4':
findByNum(head);
break;
case '5':
findByName(head);
break;
case '6':
exit(0);
default:
printf("输入错误\n");
}
}
}
你的num应该是char类型吧?
还有,name数组长度不用那么大啊,好浪费空间!
我测试了下,没什么问题,要是有什么问题可以hi我

4. c语言用链表建立通讯录 要求1.能建立,修改和增删学生通讯录 2.能够按多种方式进

真麻烦,没现成的代码,你自个写去。new和delete配合,应该很容易的

5. c语言通讯录链表文件,需读取文件来实现信息录入,要求文件已存在,程序一运行就自动读取,是什么意思

就是说文件需要先建立在在固定位置,比如说在exe同目录或者在某个固定的位置,比如在d盘上叫123.txt,你程序直接读取这个固定的文件就行了。

6. C语言:用链表做通讯录,运行之后发现我只能储存一个联系人

voidadd(structpeople*head)//添加
-->voidadd(structpeople**head)//添加
必须要用指针的指针才可以。函数里面对应也修改一下就可以了。

7. 用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;
}

8. 怎么用C语言建立一个链表实现一个通讯录

#include&lt;stdio.h&gt;

#include&lt;stdlib.h&gt;

#include&lt;string.h&gt;

#define F-1

#define T 1

struct Address

{

char name[20];

char number[12];

char address[20];

struct Address*next;

};

typedef struct Address*node;

int init(node*head);

int creat_tail(node head);

int insert_index(node head);

int length(node head);

int query_name(node head);

int delete_address(node head);

void print(node head);

int main()

{

node head;

init(&head);

int x;

do

{

printf("0:exit ");

printf("1:creat_tail ");

printf("2:insert_index ");

printf("3:query_name ");

printf("4:delete_address ");

printf("5:print ");

printf("please select ");

scanf("%d",&x);

switch(x)

{

case 0:

exit(0);

case 1:

creat_tail(head);

break;

case 2:

insert_index(head);

break;

case 3:

query_name(head);

break;

case 4:

delete_address(head);

break;

case 5:

print(head);

break;

default:

exit(0);

}

}

while(1);

return 0;

}

int delete_address(node head)

{

char address[20];

printf("please input the address you want to delete ");

scanf("%s",address);

while(head-&gt;next!=NULL)

{

if(strcmp(head-&gt;next-&gt;address,address)==0)

{

node temp=head-&gt;next;

head-&gt;next=head-&gt;next-&gt;next;

free(temp);

}

else

{

head=head-&gt;next;

}

}

return T;

}

int query_name(node head)

{

char name[20];

int count=0,index=0;

printf("please input the name you want ");

scanf("%s",name);

while(head-&gt;next!=NULL)

{

if(strcmp(head-&gt;next-&gt;name,name)==0)

{

count++;

printf("%d.name:%s number:%s address:%s ",index+1,head-&gt;next-&gt;name,head-&gt;next-&gt;number,head-&gt;next-&gt;address);

}

head=head-&gt;next;

index++;

}

if(count==0)

{

printf("not found ");

}

return T;

}

int length(node head)

{

int count=0;

while(head-&gt;next!=NULL)

{

head=head-&gt;next;

count++;

}

return count;

}

int insert_index(node head)

{

int index;

printf("please input the index you want to add ");

scanf("%d",&index);

if(index&lt;0||index&gt;=length(head))

{

printf("out of range ");

return F;

}

node newnode=(node)malloc(sizeof(struct Address));

if(NULL==newnode)

{

return F;

}

int i;

for(i=0;i&lt;index;i++)

{

head=head-&gt;next;

}

printf("please input the name,number,address ");

printf("when you input 0 0 0,exit ");

scanf("%s%s%s",newnode-&gt;name,newnode-&gt;number,newnode-&gt;address);

if(strcmp(newnode-&gt;name,"0")!=0)

{

newnode-&gt;next=head-&gt;next;

head-&gt;next=newnode;

}

return T;

}

void print(node head)

{

int count=0;

while(head-&gt;next!=NULL)

{

count++;

printf("%d.name:%s number:%s address:%s ",count,head-&gt;next-&gt;name,head-&gt;next-&gt;number,head-&gt;next-&gt;address);

head=head-&gt;next;

}

}

int creat_tail(node head)

{

do

{

node newnode=(node)malloc(sizeof(struct Address));

if(NULL==newnode)

{

return F;

}

printf("please input the name,number,address ");

printf("when you input 0 0 0,exit ");

scanf("%s%s%s",newnode-&gt;name,newnode-&gt;number,newnode-&gt;address);

if(strcmp(newnode-&gt;name,"0")!=0)

{

newnode-&gt;next=NULL;

while(head-&gt;next!=NULL)

{

head=head-&gt;next;

}

head-&gt;next=newnode;

}

else

{

break;

}

}

while(1);

return T;

}

int init(node*head)

{

node newnode=(node)malloc(sizeof(struct Address));

if(NULL==newnode)

{

return F;

}

strcpy(newnode-&gt;name,"0");

strcpy(newnode-&gt;number,"0");

strcpy(newnode-&gt;address,"0");

newnode-&gt;next=NULL;

(*head)=newnode;

return T;

}

9. 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)!=0&&p1->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);}
}
}
}
这是查找和主函数,哪出问题了?

热点内容
android文件图片 发布:2025-01-15 17:39:44 浏览:205
linux的路径怎么写 发布:2025-01-15 17:18:49 浏览:185
php解压程序 发布:2025-01-15 17:06:22 浏览:142
刷助力脚本 发布:2025-01-15 17:02:31 浏览:520
c盘里的用户文件夹可以删除 发布:2025-01-15 16:56:45 浏览:951
虚幻4编译到哪里 发布:2025-01-15 16:50:19 浏览:756
透明度渐变android 发布:2025-01-15 16:45:08 浏览:835
dos连接oracle数据库 发布:2025-01-15 16:41:39 浏览:906
网络配置比较低怎么做 发布:2025-01-15 16:35:38 浏览:362
android弹出键盘监听 发布:2025-01-15 16:35:11 浏览:208