Stacks and Queues

The question is about a basic concept ,

Why cant we use the variable
s
rather than using
maxSize
.


Queue::Queue(int s) // constructor
{
maxSize = s;
queArray = new int[maxSize];

front = 0;
rear = -1;
nItems = 0;
}

cant we use it as
queArray=new int[s] ;


same thing happens in stacks also , and I dont know the exact reason why we do this way .. any one ?
ok guys , my self found an answer . I think yes we can use the variable
s
but we need the size of the array later for isFull() and isEmpty() functions . So , we need to store the value , the size of the array in a variable .. gotcha .. BINGO ..!!
You should also check if variable s is zero.

I'm not sure why you can't use the variable s. If you wanted to know the array size later you can always use

size t = sizeof int / sizeofqueArray[0];

or store the size on a global variable.
closed account (zb0S216C)
Ahamed Huzaim wrote:
"Why cant we use the variable
s
rather than using
maxSize
"

Maybe because "s" is local to "Queue::Queue( )", and beyond that, "s" will not exist, so you have to store the value of "s" in "maxSize" to extend the scope of "s"'s value.

Ahamed Huzaim wrote:
"cant we use it as
queArray=new int[s] ;
"

Yes, you can, and I don't see a reason not to.

Wazzak
Last edited on
Topic archived. No new replies allowed.