写一个函数create,用了建立一个动态链表
2个回答
展开全部
#include<iostream>
using namespace std;
struct LINK
{
int num;
char a[20];
char b[20];
char c[20];
struct LINK *next;
};
void menu()
{
cout<<"**************************************"<<endl;
cout<<"***** 1.创建链表 *****"<<endl;
cout<<"***** 2.添加成员 *****"<<endl;
cout<<"***** 3.删除成员 *****"<<endl;
cout<<"***** 4.查找成员 *****"<<endl;
cout<<"***** 0.退出程序 *****"<<endl;
cout<<"**************************************"<<endl;
}
void create(LINK *head)
{
LINK *p,*q;
int i;
p=head;
q=p;
while(1)
{
p=new LINK();
cout<<"学号:";
cin>>p->num;
cout<<"姓名:";
cin>>p->a;
cout<<"班级:";
cin>>p->b;
cout<<"学院:";
cin>>p->c;
cout<<"是否继续(1/0)"<<endl;
cin>>i;
q->next=p;
q=p;
if(i==0)
{
p->next=NULL;
cout<<"链表创建完毕!"<<endl;
break;
}
}
}
void deleted(LINK *head)
{
LINK *p,*q;
int i;
p=head->next;
q=head;
cout<<"请输入你要删除成员的学号:";
cin>>i;
cout<<endl;
while(p!=NULL)
{
if(p->num==i)
{
q->next=p->next;
break;
}
q=p;
p=p->next;
}
if(p==NULL)
{
cout<<"没有该学生!"<<endl;
}
else
cout<<"成功删除!"<<endl;
}
void find(LINK *head)
{
LINK *p,*q;
int i;
p=head->next;
q=head;
cout<<"请输入你要查找成员的学号:";
cin>>i;
while(p!=NULL)
{
if(p->num==i)
{
cout<<"学生学号:"<<p->num<<" 姓名:"<<p->a<<endl;
break;
}
p=p->next;
}
if(p==NULL)
{
cout<<"没有该学生!"<<endl;
}
}
void add(LINK *head)
{
LINK *p,*q;
p=head->next;
q=new LINK();
cout<<"请输入学号:";
cin>>q->num;
cout<<"请输入姓名:";
cin>>q->a;
cout<<"班级:";
cin>>q->b;
cout<<"学院:";
cin>>q->c;
head->next=q;
q->next=p;
cout<<"插入成功!"<<endl;
}
void main()
{
LINK *head;
head=new LINK();
int i;
menu();
while(1)
{
cout<<"请输入你的选择:";
cin>>i;
switch(i)
{
case 1:create(head);break;
case 2:add(head);break;
case 3:deleted(head);break;
case 4:find(head);break;
case 0:exit(0);
}
}
}
希望你能用上,能看明白,能帮助你理解
using namespace std;
struct LINK
{
int num;
char a[20];
char b[20];
char c[20];
struct LINK *next;
};
void menu()
{
cout<<"**************************************"<<endl;
cout<<"***** 1.创建链表 *****"<<endl;
cout<<"***** 2.添加成员 *****"<<endl;
cout<<"***** 3.删除成员 *****"<<endl;
cout<<"***** 4.查找成员 *****"<<endl;
cout<<"***** 0.退出程序 *****"<<endl;
cout<<"**************************************"<<endl;
}
void create(LINK *head)
{
LINK *p,*q;
int i;
p=head;
q=p;
while(1)
{
p=new LINK();
cout<<"学号:";
cin>>p->num;
cout<<"姓名:";
cin>>p->a;
cout<<"班级:";
cin>>p->b;
cout<<"学院:";
cin>>p->c;
cout<<"是否继续(1/0)"<<endl;
cin>>i;
q->next=p;
q=p;
if(i==0)
{
p->next=NULL;
cout<<"链表创建完毕!"<<endl;
break;
}
}
}
void deleted(LINK *head)
{
LINK *p,*q;
int i;
p=head->next;
q=head;
cout<<"请输入你要删除成员的学号:";
cin>>i;
cout<<endl;
while(p!=NULL)
{
if(p->num==i)
{
q->next=p->next;
break;
}
q=p;
p=p->next;
}
if(p==NULL)
{
cout<<"没有该学生!"<<endl;
}
else
cout<<"成功删除!"<<endl;
}
void find(LINK *head)
{
LINK *p,*q;
int i;
p=head->next;
q=head;
cout<<"请输入你要查找成员的学号:";
cin>>i;
while(p!=NULL)
{
if(p->num==i)
{
cout<<"学生学号:"<<p->num<<" 姓名:"<<p->a<<endl;
break;
}
p=p->next;
}
if(p==NULL)
{
cout<<"没有该学生!"<<endl;
}
}
void add(LINK *head)
{
LINK *p,*q;
p=head->next;
q=new LINK();
cout<<"请输入学号:";
cin>>q->num;
cout<<"请输入姓名:";
cin>>q->a;
cout<<"班级:";
cin>>q->b;
cout<<"学院:";
cin>>q->c;
head->next=q;
q->next=p;
cout<<"插入成功!"<<endl;
}
void main()
{
LINK *head;
head=new LINK();
int i;
menu();
while(1)
{
cout<<"请输入你的选择:";
cin>>i;
switch(i)
{
case 1:create(head);break;
case 2:add(head);break;
case 3:deleted(head);break;
case 4:find(head);break;
case 0:exit(0);
}
}
}
希望你能用上,能看明白,能帮助你理解
展开全部
#include<stdio.h>
#include<stdlib.h>
struct stu
{
char name[30];
char num[25];
float score;
struct stu *next;
};
struct stu *creat()
{
int i;
float temp;
struct stu *head, *p1, *p2;
head = (struct stu *)malloc(sizeof(struct stu));
p1 = head;
head -> next = NULL;
for (i = 1; i <= 5; i++)
{
p2 = (struct stu *)malloc(sizeof(struct stu));
printf ("Please input :");
scanf ("%s%s%f", p2 -> name, p2 -> num, &temp);
p2 -> score = temp;
p1 -> next = p2;
p1 = p2;
}
p1 -> next = NULL;
return head;
}
void OutList(struct stu *head)
{
struct stu *p;
p = head -> next;
do
{
printf ("%s\t%s\t%f\n", p -> name, p -> num, p -> score);
p = p -> next;
}
while (p != NULL);
}
main ()
{
struct stu *head;
head = creat ();
OutList(head);
}
这个是带头结点动态链表建立的例子,希望对你有帮助!!!
#include<stdlib.h>
struct stu
{
char name[30];
char num[25];
float score;
struct stu *next;
};
struct stu *creat()
{
int i;
float temp;
struct stu *head, *p1, *p2;
head = (struct stu *)malloc(sizeof(struct stu));
p1 = head;
head -> next = NULL;
for (i = 1; i <= 5; i++)
{
p2 = (struct stu *)malloc(sizeof(struct stu));
printf ("Please input :");
scanf ("%s%s%f", p2 -> name, p2 -> num, &temp);
p2 -> score = temp;
p1 -> next = p2;
p1 = p2;
}
p1 -> next = NULL;
return head;
}
void OutList(struct stu *head)
{
struct stu *p;
p = head -> next;
do
{
printf ("%s\t%s\t%f\n", p -> name, p -> num, p -> score);
p = p -> next;
}
while (p != NULL);
}
main ()
{
struct stu *head;
head = creat ();
OutList(head);
}
这个是带头结点动态链表建立的例子,希望对你有帮助!!!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询