这是使用数组的循环队列的正确代码 [英] Is this correct code for circular queue using array
本文介绍了这是使用数组的循环队列的正确代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<iostream>
#include<conio.h>
using namespace std;
#define MAX 6
int arr[MAX];
int a_pt = -1, r_pt = -1;
int pop()
{
if (a_pt==-1 && r_pt ==-1) {
cout << "\nQUEUE IS EMPTY"; return NULL;
}
if(r_pt==MAX-1)
{
r_pt = 0;
if (a_pt==0) { a_pt = -1, r_pt = -1; }
return arr[MAX-1];
}
int x = r_pt;
if (r_pt + 1 == a_pt) { a_pt = -1; r_pt = -1; return arr[x];}
return arr[r_pt++];
}
void push(int val)
{
if (a_pt == -1 && r_pt == -1)
{
++a_pt; ++r_pt;
arr[a_pt++] = val;
}
else if (a_pt == MAX-1)
{
a_pt = 0;
arr[MAX - 1] = val;
}
else if (a_pt==r_pt)
{
cout << "\nQue is full";
}
else {
arr[a_pt++] = val;
}
}
int main()
{
push(10);
push(20);
push(30);
push(40);
push(50);
push(60);
push(70);
_getch();
cout << endl << "VALUE POPPED IS :" << pop();
cout << endl << "VALUE POPPED IS :" << pop();
cout << endl << "VALUE POPPED IS :" << pop();
cout << endl << "VALUE POPPED IS :" << pop();
cout << endl << "VALUE POPPED IS :" << pop();
cout << endl << "VALUE POPPED IS :" << pop();
cout << endl << "VALUE POPPED IS :" << pop();
cout << endl << "VALUE POPPED IS :" << pop();
cout << endl << "VALUE POPPED IS :" << pop();
_getch();
return 0;
}
我的尝试:
尝试了一些操作并且它们是正确的
What I have tried:
tried some operations and they were correct
推荐答案
说实话:我不认为这里的任何人都热衷于审查/检查/测试你的代码。如果您遇到代码问题,请随时回来。
尝试了一些操作并且它们是正确的:
At至少一个好兆头;)
对不起我忘了:
A 非常大拇指,你认识到/意识到,一些积极的测试并不意味着代码肯定是好的。
[/ Edit]
To be honest: I don't think that anybody here is keen on to review/check/test your code. Feel free to come back in case you face a Problem with your code.
"tried some operations and they were correct ":
At least a good sign ;)
Sorry I forgot:
A very big thumb ups, that you recognize/are aware of, that some positive tests do not mean the code is definitely ok.
[/Edit]
这篇关于这是使用数组的循环队列的正确代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文