****用链表实现队列的操作
#include<iostream>
using namespace std;
struct student
{
char name[20];
student *next;
};
struct paiduiqu//每个排队区有多少学生进入走出
{
int data;
paiduiqu *next;//下一个排队区
student *first,*rear;//这个排队区的最前面和最后面的学生
};
//这里不能用模板,因为插入初始化的时候,结构体不同
paiduiqu *create(int n)//创建n个排队区
{
paiduiqu *head,*p,*s;
int i=1;//第一个排队区开始
head=(paiduiqu*)malloc(sizeof(paiduiqu));
p=head;
while(i<=n)
{
s=(paiduiqu*)malloc(sizeof(paiduiqu));
s->data=i;//排队区的区号
p->next=s;
p=s;
i++;
}
p->next=NULL;//最后一个要给它赋值为空,不然就是垂悬指针,很危险
head=head->next;
return head;
}
//这个可以用模板来解决
template <class T>
int length(T *head)
{
int n=0;
T *p=head;
while(p)
{
p=p->next;
n++;
}
return n;
}
/*
int length(paiduiqu *head)
{
int n=0;
paiduiqu *p=head;
while(p)
{
p=p->next;
n++;
}
return n;
}
*/
//因为输出的东西不同,所以也不能够用模板
void print(paiduiqu *head)
{
paiduiqu *p=head;
while(p)
{
cout<<p->data<<endl;
p=p->next;
}
cout<<endl;
}
void print_student(student* head)
{
student *p=head;
while(p)
{
cout<<p->name<<" ";
p=p->next;
}
cout<<endl;
}
paiduiqu *insert(paiduiqu *head,int n)//排队区增加n个学生
{
head->first=head->rear=NULL;//首先赋值为NULL,不然又是垂悬指针,危险
int i=1;//从第一个学生开始计数,知道n个学生
while(i<=n)
{
student *s=(student *)malloc(sizeof(student));//每次都申请一个空间
cout<<"给第"<<i<<"个学生输入名字:";
cin>>s->name;
if(head->rear==NULL)//如果刚开始排队区是空的
{
head->first=s;
head->rear=s;
}
else
{
head->rear->next=s;
head->rear=s;
}
i++;
}
//s->next=NULL;//不在作用域里面了
head->rear->next=NULL;
return head;
}
paiduiqu *deque(paiduiqu* head,int n)
{
if(n==0)//就是不出列学生,否则就有学生要出列
return head;
if(n>length(head->first ))
{
cout<<"超出范围:"<<endl;
exit(0);
}
int i=1;//出队列从第一个开始出
student *p=head->first;//用于循环释放
while(i<=n)
{
head->first=head->first->next;
free(p);
p=head->first;//防止成为悬空指针,并且可以释放结点内存
i++;
}
return head;
}
int main(void)
{
int number;
cout<<"创建几个排队区:"<<endl;
cin>>number;
paiduiqu *head;
head=create(number);
int len=length(head);
cout<<endl<<len<<"个排队区如下:"<<endl;
print(head);
/*************
cout<<"给这"<<len<<"个排队区加入学生队列:"<<endl<<endl;
paiduiqu *p=head;
for(int i=1;i<=len;i++)//给len个排队区加入学生队列,并显示出来
{
cout<<"给第"<<i<<"个排队区加入多少学生:"<<endl;
cin>>number;
insert(p,number);//加入了学生
p=p->next;
}
cout<<endl;
p=head;
for(int i=1;i<=len;i++)
{
cout<<"第"<<i<<"个排队区的学生信息如下:"<<endl;
print_student(p->first );//显示学生信息
p=p->next;
}
p=head;
for(int i=1;i<=len;i++)
{
cout<<"在第"<<i<<"个排队区学生出列人数:"<<endl;
cin>>number;
deque(p,number);//这里出队列
p=p->next;
}
cout<<endl;
p=head;
for(int i=1;i<=len;i++)
{
cout<<"第"<<i<<"个排队区的学生信息如下:"<<endl;
print_student(p->first );//显示学生信息
p=p->next;
}
return 0;
}
#include<iostream>
using namespace std;
struct student
{
char name[20];
student *next;
};
struct paiduiqu//每个排队区有多少学生进入走出
{
int data;
paiduiqu *next;//下一个排队区
student *first,*rear;//这个排队区的最前面和最后面的学生
};
//这里不能用模板,因为插入初始化的时候,结构体不同
paiduiqu *create(int n)//创建n个排队区
{
paiduiqu *head,*p,*s;
int i=1;//第一个排队区开始
head=(paiduiqu*)malloc(sizeof(paiduiqu));
p=head;
while(i<=n)
{
s=(paiduiqu*)malloc(sizeof(paiduiqu));
s->data=i;//排队区的区号
p->next=s;
p=s;
i++;
}
p->next=NULL;//最后一个要给它赋值为空,不然就是垂悬指针,很危险
head=head->next;
return head;
}
//这个可以用模板来解决
template <class T>
int length(T *head)
{
int n=0;
T *p=head;
while(p)
{
p=p->next;
n++;
}
return n;
}
/*
int length(paiduiqu *head)
{
int n=0;
paiduiqu *p=head;
while(p)
{
p=p->next;
n++;
}
return n;
}
*/
//因为输出的东西不同,所以也不能够用模板
void print(paiduiqu *head)
{
paiduiqu *p=head;
while(p)
{
cout<<p->data<<endl;
p=p->next;
}
cout<<endl;
}
void print_student(student* head)
{
student *p=head;
while(p)
{
cout<<p->name<<" ";
p=p->next;
}
cout<<endl;
}
paiduiqu *insert(paiduiqu *head,int n)//排队区增加n个学生
{
head->first=head->rear=NULL;//首先赋值为NULL,不然又是垂悬指针,危险
int i=1;//从第一个学生开始计数,知道n个学生
while(i<=n)
{
student *s=(student *)malloc(sizeof(student));//每次都申请一个空间
cout<<"给第"<<i<<"个学生输入名字:";
cin>>s->name;
if(head->rear==NULL)//如果刚开始排队区是空的
{
head->first=s;
head->rear=s;
}
else
{
head->rear->next=s;
head->rear=s;
}
i++;
}
//s->next=NULL;//不在作用域里面了
head->rear->next=NULL;
return head;
}
paiduiqu *deque(paiduiqu* head,int n)
{
if(n==0)//就是不出列学生,否则就有学生要出列
return head;
if(n>length(head->first ))
{
cout<<"超出范围:"<<endl;
exit(0);
}
int i=1;//出队列从第一个开始出
student *p=head->first;//用于循环释放
while(i<=n)
{
head->first=head->first->next;
free(p);
p=head->first;//防止成为悬空指针,并且可以释放结点内存
i++;
}
return head;
}
int main(void)
{
int number;
cout<<"创建几个排队区:"<<endl;
cin>>number;
paiduiqu *head;
head=create(number);
int len=length(head);
cout<<endl<<len<<"个排队区如下:"<<endl;
print(head);
/*************
cout<<"给这"<<len<<"个排队区加入学生队列:"<<endl<<endl;
paiduiqu *p=head;
for(int i=1;i<=len;i++)//给len个排队区加入学生队列,并显示出来
{
cout<<"给第"<<i<<"个排队区加入多少学生:"<<endl;
cin>>number;
insert(p,number);//加入了学生
p=p->next;
}
cout<<endl;
p=head;
for(int i=1;i<=len;i++)
{
cout<<"第"<<i<<"个排队区的学生信息如下:"<<endl;
print_student(p->first );//显示学生信息
p=p->next;
}
p=head;
for(int i=1;i<=len;i++)
{
cout<<"在第"<<i<<"个排队区学生出列人数:"<<endl;
cin>>number;
deque(p,number);//这里出队列
p=p->next;
}
cout<<endl;
p=head;
for(int i=1;i<=len;i++)
{
cout<<"第"<<i<<"个排队区的学生信息如下:"<<endl;
print_student(p->first );//显示学生信息
p=p->next;
}
return 0;
}