0

First Question Here:

So , I'm learning Stacks and Queues. And tried to implement Queue by myself in c++. Now I can't make an queue of an array. Here is the normal code i used:

//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;
}

Here using Integer works well. But if i use int[] then i got following error.

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];

I can understand what error is saying but I tried in built-in queue even:-

//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);
     ^

I don't need the code. I need the explanation why we can't make an queue of an array and if we can then a documentation's link

Thanks and Regards Khushit Suggestion are welcomed

6
  • 3
    C++ symbols are case sensitive! Commented Jul 28, 2018 at 12:53
  • 2
    In the last snippet you forgot #include <queue>. Commented Jul 28, 2018 at 12:54
  • 1
    @HolyBlackCat running without it on rextester.com Commented Jul 28, 2018 at 12:57
  • 2
    @Khushit Shah header files may be transitively included by other files. That does not mean that you shouldn't include them yourself, if you want to write portable code. Commented Jul 28, 2018 at 13:03
  • 1
    @FeiXiang I can't upvote your comment but. You answered my question. Thanks Commented Jul 29, 2018 at 10:24

1 Answer 1

1

You are internally using an array to store your elements. Arrays require every element to be equally big. This works fine for, for example, integers. It doesn't work however for something like an array, which can have different sizes.

You can therefore not use your queue with something like int[]. What you could do however, is use std::vector<int>. The same holds for the built in std::queue.

Sign up to request clarification or add additional context in comments.

6 Comments

I even tried pointer. Here is the linklink[/link]
@KhushitShah, you can see here that it works: cpp.sh/6pcnu Your link is broken, so I can't check that, but you probably did something else wrong.
thanks but it is still not working with strings or char array:- cpp.sh/34k3f
And you used vectors instead of arrays.😅
@KhushitShah, yes, like I explained in my answer. It can't work with arrays. Pointers to arrays, or similarly, vectors will work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.