top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can we represent a stack and a queue by using only one single dimensional array?

+2 votes
1,360 views
How can we represent a stack and a queue by using only one single dimensional array?
posted Dec 12, 2015 by Maninder Bath

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

QUEUE: If you are using this data structure then first entered data should be move out first.
STACK : If you are using this data structure then last entered data should be move out first.

Storage:
int arr[5] = {1, 2, 3, 4, 5};

/* Queue variable initialization */
int enqueueIndex = 0;
int dequeueIndex = 0;

/ * Queue operation logic */
enqueue (arr[enqueueIndex++], data1);
enqueue (arr[enqueueIndex++], data2);
enqueue (arr[enqueueIndex++], data3);

This time value of "enqueueIndex" is 3. If you want to remove element from queue then
while (dequeueIndex < enqueueIndex)
{
value = dequeue (arr[dequeueIndex++]);
}

/* Stack variable initialization */
int pushIndex = 0;
int popIndex = 0;

/* Stack operation logic *./
push (arr[pushIndex++], data1);
push (arr[pushIndex++], data2);
push (arr[pushIndex++], data3);

popIndex = pushIndex -1;
while (popIndex < = 0)
{
value = pop(arr[popIndex--]);
}

answer Dec 12, 2015 by Harshita
...