假设将循环队列定义为:以域变量rear和length分别指示循环队列中队尾元素的位置和内含元素的个数。编写相应的入队列和出队列的程序,并判断循环队列是否队满(在出队列的算法中要返回队头元素)。 |
假设队列数组为Queue[MAXSIZE],第一行输入队列大小N,第二行开始输入若干入队元素,队满时,停止入队。第三行输入出队元素。 |
输出入队出队操作后的循环队列,并返回出队元素的队头元素。 |
5 3 4 6 2 7 4 |
6 2 7 6 |
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct linklist
{
struct linklist *next;
int data;
};
bool isdigi(char j)
{
return (j >= '0' && j <= '9');
}
struct linkqueue
{
struct linklist *rear;
struct linklist *head;
int length;
}linq;
void init()
{
linq.length = 0;
linq.rear = NULL;
}
void popl()
{
if(linq.head->next == linq.head)
{
linq.length = 0;
linq.rear = NULL;
linq.head = NULL;
}
else
{
linq.length--;
linq.rear->next = linq.head->next;
linq.head = linq.head->next;
}
}
void pushl(int x)
{
struct linklist *newblock;
newblock = (struct linklist *)malloc(sizeof(struct linklist));
newblock->data = x;
if(linq.rear == NULL)
{
linq.rear = newblock;
linq.head = newblock;
linq.rear->next = linq.rear;
}
else
{
newblock->next = linq.head;
linq.rear->next = newblock;
linq.rear = newblock;
}
linq.length+=1;
}
int main()
{
struct linklist *lq;
int num,data;
char m;
cin>>num;
init();
m = getchar();
m = getchar();
for(;m != '\n';)
{
if(isdigi(m) && linq.length < num)
{
pushl((int)(m-48));
}
m = getchar();
}
if(num <= linq.length)
{
getchar();
getchar();
getchar();
getchar();
}
else
{
getchar();
getchar();
getchar();
}
cin >> data;
for(;;)
{
if(linq.head->data != data)
{
popl();
}
else
{
popl();
break;
}
}
lq = linq.head;
if(lq == NULL)
{
cout<<endl;
}
else
{
for(;lq->next != linq.head;)
{
cout<< lq->data << ' ';
lq = lq->next;
}
cout<< lq->data <<endl;
}
if(lq == NULL)
{
cout<<endl;
}
else
{
cout<<linq.head->data<<endl;
}
}