Stack and Que in C++
Stack and Que in C++
Objectives: To familiarize students with stack & queue implementation using arrays.
The main purpose of C++ programming is to add Object-Orientation to the C programming language and
classes are the central feature of C++ that supports object-oriented programming. Classes are often
called user-defined types. In this lab we will implement Stack and Queue classes while using arrays as
the main data storage and restricting the access to the data elements according to the rules followed by
the two data structure.
Stack – a collection of elements arranged in a linear order. The real life examples can be as stack of
books are stack of plates etc. The new items are added at the top of the stack or remove them from the
top and can access the elements of the stack at the top. The following are some of the important
methods used for stack:
pop() Remove the top element of the stack and return it.
top() Return the top element without removing it from the stack.
The push(x) method will take an element and insert it at the top of the stack. This element will become
top element. The pop() method will remove the top element of the stock and return it to the calling
program. The top() method returns the top-most stack element but does not remove it from the stack.
The working of stack can be as follow in diagram.
1 top
7 7 top
5 5 5 top
2 2
top 2 2
top
top top
push(2) push(5) push(7) push(1)
21
7 7 7
5 5 5 top 5
2 2 2 2 top 2
21
1 push(21) pop() 7 pop() 5 pop()
pop()
At the start, the stack is empty. Push the value 2 in the stack and as a result, the number 2 is placed in
the stack. The top pointer points at the top element. Then push(5). The number 5 is placed at the top of
number 2 and the pointer top moves one step upward. Then pushed the number 7 which is placed on
the top and the number 2 and 5 are below. Similarly, push number 1. The last figure in the first row
shows the stacked values of the numbers as 1, 7, 5 and 2.
Now pop the elements from the stack. The first figure of second row shows the pop operation. As a
result, the number 1 is popped. Then again push the number 21 on the stack. The number 7, 5, and 2
are already in the stack and number 21 is pushed at the top. By pop now, the number 21 is popped.
Now number 7 is at the top. Then pop again, the number 7 is popped. Pop again and the number 5 is
popped and number 2 remains in the stack.
The last element to go into the stack is the first to come out. That is why, a stack is known as LIFO (Last
In First Out) structure.
Stack implementation using Array – Stack can be implemented using an array. The interface will
remain as push and pop methods. The user of the stack does not need to know that the stack is
internally implemented with the help of array. The worst case for insertion and deletion from an array
may happen when we insert and delete from the beginning of the array where the elements must be
shifted to the right for insertion and left for removal. In case of push, the elements of the stack must be
shifted to the right while in case of pop, the stack elements must be shift to the left where we remove
elements. By pushing the element at the end of the array, there is no need to shift any element.
Similarly as the pop method removes the last element of the stack which is at the end of the array, no
element is shifted. To insert and remove elements at the end of the array we need not to shift its
elements. Best case for insert and delete is at the end of the array where there is no need to shift any
Top 1 way to implement push() and pop() by inserting and deleting at the end of an array.
element. The best 2 5 7 1
7 shown as an array in following figure where top is equal to 3 at element number 1.
The stack can be
5
2
0 1 2 3 4
Top = 3
There are two parts of above figure. On the left hand, there is the stack implemented using an array. The
elements present inside this stack are 1, 7, 5 and 2. The most recent element of the stack is 1. It may be
removed if the pop() is called at this point of time.
Algorithm: In Stack we have two main functions Push and pop, following is the pseudo code for both.
Push ( ): Here STACK is an array with MAX locations. TOP points to the top most element and ITEM is the value to be
inserted.
Pop (): Here STACK is an array with MAX locations. TOP points to the top most element.
Step 6: Save file again, build project and run as local C/C++ application for required output as shown in figure
below
Figure 23: Lab5.cpp class output
Queue Implementation
Queue – a linear data structure into which items can only be inserted at one end and removed
from the other. A queue is a FIFO (First In First Out) structure. The objective of the queue is to
serve data in their arrival order. The first coming process is served first. The process, who comes
first, waits at the start followed by the process coming after and so on. The side through which
data is inserted is called as backend or rear and process is known as enqueue. The deletion side is
known as front end and the process is called in terms of queue as dequeue. The queue data
structure supports the following operations:
Operation Description
1 2 1 7 5 2
7 5
The above figure shows queue elements on the left with two reference variables front and rear. This is
an abstract view of the queue, independent of its implementation method. When dequeue() function is
called once, the front element 1 is removed. The picture of the queue showing one element removal is also
depicted below. The front pointer has been moved to the next element 7 in the list afer removing the
front element 1.
1 7 5 2 1 7 5 2
Step 1: Create New C++ Program File and save it as lab4b.cpp
Step 2: Create a New class and save it as queue.cpp
Step 3: Write the following code as shown in figure below to create the data storage and reference
variables for the implementation of queue.
Step 4: Write the code for insertion of data in the queue as shown in figures below
Step 5: Write the code for removal of data (dqueue) as shown in the following figure.
The additional functions isEmpty() and isFull() may also be added to the Queue class for ensuring underflow
and overflow safety in your program. The code for both functions is given below.
Step 7: Save file again, build project and run as local C/C++ application
1. Implement stack using array of size 10, Add a display method which will display the current value of
the Top pointer and then just display the elements of the stack in LIFO fashion without popping any
element of the stack.
2. Modify the above queue class so that front is always at the index 0 while rear pointer changes
its position with both the insertion and deletion of elements of the queue.
3. Create a DEqueue class based on the discussion of deques (double-ended queues) theory class.
With space for 10 items, it should include insertRear(), insertFront(), removeRear(),
removeFront(), isEmpty(), and isFull() methods. It will need to support wraparound at the end of
the array, as queues do. The program should be able to produce output in the following format.
Please enter a number representing an operation in the following list (-1 to quit):
1. InsertRear()
2. InsertFront()
3. RemoveRear()
4. RemoveFront()
5. Display()
Please enter a number representing an operation in the following list (-1 to quit):
1. InsertRear()
2. InsertFront()
3. RemoveRear()
4. RemoveFront()
5. Display()
Please enter a number representing an operation in the following list (-1 to quit):
1. InsertRear()
2. InsertFront()
3. RemoveRear()
4. RemoveFront()
5. Display()