c語言鏈表通訊錄
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<stdio.h>
#include<stdlib.h>
#include<string.h>
#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->next!=NULL)
{
if(strcmp(head->next->address,address)==0)
{
node temp=head->next;
head->next=head->next->next;
free(temp);
}
else
{
head=head->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->next!=NULL)
{
if(strcmp(head->next->name,name)==0)
{
count++;
printf("%d.name:%s number:%s address:%s ",index+1,head->next->name,head->next->number,head->next->address);
}
head=head->next;
index++;
}
if(count==0)
{
printf("not found ");
}
return T;
}
int length(node head)
{
int count=0;
while(head->next!=NULL)
{
head=head->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<0||index>=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<index;i++)
{
head=head->next;
}
printf("please input the name,number,address ");
printf("when you input 0 0 0,exit ");
scanf("%s%s%s",newnode->name,newnode->number,newnode->address);
if(strcmp(newnode->name,"0")!=0)
{
newnode->next=head->next;
head->next=newnode;
}
return T;
}
void print(node head)
{
int count=0;
while(head->next!=NULL)
{
count++;
printf("%d.name:%s number:%s address:%s ",count,head->next->name,head->next->number,head->next->address);
head=head->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->name,newnode->number,newnode->address);
if(strcmp(newnode->name,"0")!=0)
{
newnode->next=NULL;
while(head->next!=NULL)
{
head=head->next;
}
head->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->name,"0");
strcpy(newnode->number,"0");
strcpy(newnode->address,"0");
newnode->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);}
}
}
}
這是查找和主函數,哪出問題了?