所以,我正在学习堆栈和队列。并尝试用c ++自己实现Queue。 现在,我无法将数组放入队列。这是我使用的普通代码:
// g ++ 5.4.0
#include <iostream>
using namespace std;
#define SIZE 10
template <class Eltype>
class Queue {
/*
* MAXSIZE carries the max no of element a queue can have.
* nItem carries the no of element queue have.
* front carries current front index
* rear carries current rear index.
* """" CIRCULAR QUEUE """
*/
int MAXSIZE, front, rear, nItem;
Eltype queue[1000];
public:
Queue (int size = SIZE) { //constructor:-)
// queue = &base;
MAXSIZE = size;
front = -1;
rear = -1;
nItem = 0;
}
int push(Eltype n) {
//pushes element in queue
if (null()) {
//queue is null so front = rear=-1
front++;
rear++;
// adding alement at 0
queue[rear] = n;
nItem++;
} else if (!full()) {
// queue is not null as well as it is not full
if (rear != MAXSIZE - 1) {
// checking that rear is not the end
//if not, adding an element at rear++;
rear++;
queue[rear] = n;
nItem++;
} else {
/* Imagine a situation where first two element is empty but 3 ,4 ,5 is full.
* so. without this condition it can give Queue overflow.
* but intialising rear by zero can solve this problem
*/
// means rear == MAXSIZE but queue is not full so
rear = 0;
// initialisin rear by 0;
queue[rear] = n;
nItem++;
}
} else {
cout << "Queue overflow";
}
return 1;
}
int pop() {
// /deleting an element from queue.
if (null()) {
// its already null. No need to be popped;
cout << "Queue underflow";
} else {
// it is not null
if (front != MAXSIZE) {
// checking that front is not equal to end (not rear). rear and end are two different terminologies.
// current index element is equal to null
queue[front]= NULL;
front++;
nItem--;
} else {
// means queue is not null and front = rear ("Circural Queue condition")
// so , front = 0;
front = 0;
queue[front] = NULL;
nItem--;
}
}
return 1;
}
void printall() {
// printing all elements in queue
int i;
for (i = 0; i < MAXSIZE; i++) {
cout << " " << queue[i];
}
}
void debug() {
// printting terminologies fro debugging.
cout << "nItems : " << nItem << "front :" << front << "rear :" << rear;
}
private:
int full() {
// checking if full
return ((nItem == MAXSIZE) ? 1 : 0);
}
int null() {
(nItem <= 0) ? front = rear = -1 :0;
// checking if null.
return ((nItem <= 0) ? 1 : 0);
}
};
int main() {
int a[] = {1,2,3};
Queue<int> q;
q.push(1);
q.printall();
return 0;
}
在这里使用Integer效果很好。但是,如果我使用int [],则会出现以下错误。
source_file.cpp: In instantiation of ‘class Queue<int []>’:
source_file.cpp:108:22: required from here
source_file.cpp:17:26: error: creating array of ‘int []’
Eltype queue[1000];
source_file.cpp:26:13: note: initializing argument 1 of ‘int Queue<Eltype>::push(Eltype) [with Eltype = int []]’
int push(Eltype n) {
source_file.cpp:107:13: warning: unused variable ‘a’ [-Wunused-variable]
int a[] = {1,2,3};
^
source_file.cpp: In instantiation of ‘int Queue<Eltype>::push(Eltype) [with Eltype = int []]’:
source_file.cpp:109:17: required from here
source_file.cpp:33:22: error: using invalid field ‘Queue<Eltype>::queue’
queue[rear] = n;
^
source_file.cpp:41:26: error: using invalid field ‘Queue<Eltype>::queue’
queue[rear] = n;
^
source_file.cpp:51:26: error: using invalid field ‘Queue<Eltype>::queue’
queue[rear] = n;
^
source_file.cpp: In instantiation of ‘void Queue<Eltype>::printall() [with Eltype = int []]’:
source_file.cpp:110:20: required from here
source_file.cpp:87:37: error: using invalid field ‘Queue<Eltype>::queue’
cout << " " << queue[i];
我可以理解错误的意思,但是我甚至在内置队列中尝试过:-
//g++ 5.4.0
#include <iostream>
using namespace std;
int main()
{
int a ={1,2,3};
queue<int[3]> q;
q.push(a);
return 0;
}
source_file.cpp: In function ‘int main()’:
source_file.cpp:8:18: error: scalar object ‘a’ requires one element in initializer
int a ={1,2,3};
^
source_file.cpp:9:5: error: ‘queue’ was not declared in this scope
queue<int[3]> q;
^
source_file.cpp:9:11: error: expected primary-expression before ‘int’
queue<int[3]> q;
^
source_file.cpp:10:5: error: ‘q’ was not declared in this scope
q.push(a);
^
感谢Khushit 欢迎建议
答案 0 :(得分:1)
您在内部使用数组存储元素。数组要求每个元素都一样大。例如,对于整数,这很好用。但是,对于像数组这样的东西,它可能具有不同的大小,它不起作用。
因此,您不能将队列与int[]
之类的东西一起使用。但是,您可以使用std::vector<int>
。
内置std::queue
也是如此。