//链表队列
#include<iostream>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
using namespace std;
typedef struct Student
{
int num;
char name[100];
struct Student *next;
}Liststudent;
Liststudent * creat(int n)
{
Student *p,*head,*q;
head=(Liststudent *)malloc(sizeof(Liststudent));
head->next=NULL;
q=head;
int i;
for(i=0;i<n;i++)
{
p=(Liststudent *)malloc(sizeof(Liststudent));
int a;char s[100];
cout<<"姓名:";cin>>a;
cout<<"学号:";cin>>s;
strcpy(p->name,s);
p->num=a;
q->next=p;
q=p;
}
q->next=NULL;
return head;
Liststudent *h;
h=p->next;
while(h!=NULL)
{
cout<<"姓名:";cout<<h->num;
cout<<"学号:";cout<<h->name;
h=h->next;
}
{
Liststudent *h;
for(int i=0;i<n-1;i++)
{
p=p->next;
}
h=p->next;
p->next=h->next;
{
Liststudent *h;
for(int i=0;i<n-1;i++)
{
p=p->next;
}
h=(Liststudent *)malloc(sizeof(Liststudent));
strcpy(h->name,s);
h->num=num;
h->next=p->next;
p->next=h;
{
int i;
for(i=0;i<n-1;i++)
{
p=p->next;
}
cout<<"学生名字:"<<p->name;
cout<<"学生学号:"<<p->num;
}
int main()
{
Liststudent *p;
int n,i;
int num;
char s[100];
while(i)
{
cout<<"1"<<"请输入学生信息"<<endl;
cout<<"2"<<"删除学生信息"<<endl;
cout<<"3"<<"增加学生信息"<<endl;
cout<<"4"<<"查询学生信息"<<endl;
cout<<"5"<<"打印所有学生信息"<<endl;
cout<<"0"<<"退出该程序"<<endl;
cout<<"您要操作的对象序号为:"<<endl;cin>>i;
switch(i)
{
case 1:
cout<<"学生总人数为:"<<endl;cin>>n;
p=creat(n);
pop(p);
cout<<endl;
break;
case 2:
cout<<"所要删除学生的位置为:"<<endl;
cin>>i;
del(p,i);
pop(p);
cout<<endl;
break;
case 3:
cout<<"所要增加的学生位置为:"<<endl;
cin>>i;
cout<<"增加的学生信息"<<endl;
cout<<"姓名:"<<endl;
cin>>s;
cout<<"学号:"<<endl;
cin>>num;
ins(p,i,num,s);
pop(p);
cout<<endl;
break;
case 4:
cout<<"所要查询的学生位置为:"<<endl;
cin>>i;
search(p,i);
cout<<endl;
break;
case 5:
cout<<"所有学生信息为:"<<endl;
pop(p);
cout<<endl;
break;
case 0:
return 0;
}
}
}
#include<iostream>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
using namespace std;
typedef struct Student
{
int num;
char name[100];
struct Student *next;
}Liststudent;
Liststudent * creat(int n)
{
Student *p,*head,*q;
head=(Liststudent *)malloc(sizeof(Liststudent));
head->next=NULL;
q=head;
int i;
for(i=0;i<n;i++)
{
p=(Liststudent *)malloc(sizeof(Liststudent));
int a;char s[100];
cout<<"姓名:";cin>>a;
cout<<"学号:";cin>>s;
strcpy(p->name,s);
p->num=a;
q->next=p;
q=p;
}
q->next=NULL;
return head;
}
//打印链表
Liststudent * pop(Liststudent *p)
{Liststudent *h;
h=p->next;
while(h!=NULL)
{
cout<<"姓名:";cout<<h->num;
cout<<"学号:";cout<<h->name;
h=h->next;
}
}
//删除节点
Liststudent * del(Liststudent *p,int n){
Liststudent *h;
for(int i=0;i<n-1;i++)
{
p=p->next;
}
h=p->next;
p->next=h->next;
}
//增加节点
Liststudent * ins(Liststudent *p,int n,int num,char s[]){
Liststudent *h;
for(int i=0;i<n-1;i++)
{
p=p->next;
}
h=(Liststudent *)malloc(sizeof(Liststudent));
strcpy(h->name,s);
h->num=num;
h->next=p->next;
p->next=h;
}
//查询节点
Liststudent *search(Liststudent *p,int n){
int i;
for(i=0;i<n-1;i++)
{
p=p->next;
}
cout<<"学生名字:"<<p->name;
cout<<"学生学号:"<<p->num;
}
int main()
{
Liststudent *p;
int n,i;
int num;
char s[100];
while(i)
{
cout<<"1"<<"请输入学生信息"<<endl;
cout<<"2"<<"删除学生信息"<<endl;
cout<<"3"<<"增加学生信息"<<endl;
cout<<"4"<<"查询学生信息"<<endl;
cout<<"5"<<"打印所有学生信息"<<endl;
cout<<"0"<<"退出该程序"<<endl;
cout<<"您要操作的对象序号为:"<<endl;cin>>i;
switch(i)
{
case 1:
cout<<"学生总人数为:"<<endl;cin>>n;
p=creat(n);
pop(p);
cout<<endl;
break;
case 2:
cout<<"所要删除学生的位置为:"<<endl;
cin>>i;
del(p,i);
pop(p);
cout<<endl;
break;
case 3:
cout<<"所要增加的学生位置为:"<<endl;
cin>>i;
cout<<"增加的学生信息"<<endl;
cout<<"姓名:"<<endl;
cin>>s;
cout<<"学号:"<<endl;
cin>>num;
ins(p,i,num,s);
pop(p);
cout<<endl;
break;
case 4:
cout<<"所要查询的学生位置为:"<<endl;
cin>>i;
search(p,i);
cout<<endl;
break;
case 5:
cout<<"所有学生信息为:"<<endl;
pop(p);
cout<<endl;
break;
case 0:
return 0;
}
}
}