C++简单通讯录

C++简单通讯录
C++简单通讯录

姓名:[ ] 班级:[ ] 学号:[ ] 指导教师:[ ]

1.实验目的:编制一个演示单链表插入、删除、查找等操作的程序。

2.实验目的:本次实验的主要目的在于熟悉线性表的基本运算在两种存储结构上的实现,其中以熟悉各种链表的操作为侧重点。

3.需求分析:设计一个通讯录代码,先创建通讯录,再增加、查询、删除和退出程序。

4.详细设计:

#include

#include

using namespace std;

struct student

{

long num;

char nam[20];

char sex;

char tel[20];

student *next;

};

int n;

int main()

{

void menu_select();

student *creat();

student *insert(student *,student *);

student *search(student *,long);

student *del(student *,long);

void print(student *);

student *head,*stu;

long del_num;

cout<<"创建软件四班通讯录:"<

cout<<"input num nam sex(m/f) tel:"<

head=creat();

print(head);

int choice;

do

{

menu_select();

cin>>choice;

switch(choice)

{

case 1:

cout<

stu=new student;

cin>>stu->num>>stu->nam>>stu->sex>>stu->tel;

while(stu->num!=0)

{

head=insert(head,stu);

print(head);

cout<

stu=new student;

cin>>stu->num>>stu->nam>>stu->sex>>stu->tel;

}

break;

case 2:

cout<

cin>>del_num;

while(del_num!=0)

{

head=search(head,del_num);

cout<<"input the searched number: ";

cin>>del_num;

}

break;

case 3:

cout<

cin>>del_num;

while(del_num!=0)

{

head=del(head,del_num);

print(head);

cout<<" input the deleted number: ";

cin>>del_num;

}

case 4:

delete stu;

break;

default:

cout<<"选择无效:";

break;

}

}while(choice!=4);

system("pause");

return 0;

}

void menu_select()

{

cout<<" "<

cout<<"---- ---- ---- 软件四班通讯录---- ---- ---- "<

cout<<" "<

cout<<" 1_新增记录"<

cout<<" 2_查询记录"<

cout<<" 3_删除记录"<

cout<<" 4_退出"<

cout<<" "<

cout<<"---------------------------------------------"<

cout<<" 请输入你的选择:"<

student *creat()

{

student *head;

student *p1,*p2;

n=0;

p1=p2=new student;

cin>>p1->num>>p1->nam>>p1->sex>>p1->tel;

head=NULL;

while(p1->num!=0)

{

n=n+1;

if(n==1) head=p1;

else p2->next=p1;

p2=p1;

p1=new student;

cin>>p1->num>>p1->nam>>p1->sex>>p1->tel;

}

p2->next=NULL;

return(head);

}

student *insert(student *head,student *stud)

{

student *p0,*p1,*p2;

p1=head;

p0=stud;

if(head==NULL)

{

head=p0;p0->next=NULL;

}

else

{

while((p0->num>p1->num)&&(p1->next!=NULL))

{

p2=p1;

p1=p1->next;

}

if(p0->num<=p1->num)

{

if(head==p1) head=p0;

else p2->next=p0;

p0->next=p1;

}

else

{

p1->next=p0;

p0->next=NULL;

}

}

n=n+1;

return(head);

}

student *search(student *head,long num)

{

student *p1,*p2;

if(head==NULL)

{

cout<<"通讯录为空!"<

return(head);

}

p1=head;

while((num!=p1->num)&&(p1->next!=NULL))

{

p2=p1;p1=p1->next;

}

if(num==p1->num)

{

cout<num<<" "<nam<<" "<sex<<" "<tel;

}

else cout<<"cannot find "<

return(head);

}

student *del(student *head,long num)

{

student *p1,*p2;

if(head==NULL)

{

cout<<"list null!"<

return(head);

}

p1=head;

while((num!=p1->num)&&(p1->next!=NULL))

{

p2=p1;p1=p1->next;

}

if(num==p1->num)

{

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

else p2->next=p1->next;

cout<<"delete:"<

n=n-1;

}

else cout<<"cannot find "<

return(head);

}

void print(student *head)

{

student *p;

cout<

p=head;

if(head!=NULL)

do

{

cout<num<<" "<nam<<" "<sex<<" "<tel<

p=p->next;

}while(p!=NULL);

}

5.操作截图:

相关主题
相关文档
最新文档