Lab4 Dsa W23
Lab4 Dsa W23
1
Engr. Muhammad Arslan Rafique
Lab 04: To implement different operations on Stacks and Queues using arrays
Objective: To implement different operations on Stacks (Push, Pop, Display etc.) and Queues
(enQueue, deQueue, display) using arrays.
What is a Stack?
Stack is a linear data structure in which the insertion and deletion operations are performed at
only one end. In a stack, adding and removing of elements are performed at single position
which is known as "top". That means, new element is added at top of the stack and an element is
removed from the top of the stack. In stack, the insertion and deletion operations are performed
based on LIFO (Last in First Out) principle.
In a stack, the insertion operation is performed using a function called "push" and deletion operation
is performed using a function called "pop".
In the figure, PUSH and POP operations are performed at top position in the stack. That means,
both the insertion and deletion operations are performed at one end (i.e., at Top)
Example
If we want to create a stack by inserting 10,45,12,16,35 and 50. Then 10 becomes the bottom most
2
Engr. Muhammad Arslan Rafique
element and 50 is the top most element. Top is at 50 as shown in the image below.
Operations on a Stack
Stack data structure can be implement in two ways. They are as follows...
1. Using Array
2. Using Linked List
When stack is implemented using array, that stack can organize only limited number of elements.
When stack is implemented using linked list, that stack can organize unlimited number of elements.
A stack data structure can be implemented using one dimensional array. But stack implemented
using array, can store only fixed number of data values. This implementation is very simple, just
define a one dimensional array of specific size and insert or delete the values into that array by
using LIFO principle with the help of a variable 'top'. Initially top is set to -1. Whenever we want to
insert a value into the stack, increment the top value by one and then insert. Whenever we want to
delete a value from the stack, then delete the top value and decrement the top value by one.
Step 1: Include all the header files which are used in the program and define a constant 'SIZE'
with specific value.
Step 2: Declare all the functions used in stack implementation.
Step 3: Create a one dimensional array with fixed size (int stack[SIZE])
Step 4: Define an integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5: In main method display menu with list of operations and make suitable function calls to
perform operation selected by the user on the stack.
In a stack, push() is a function used to insert an element into the stack. In a stack, the new element
is always inserted at top position. Push function takes one integer value as parameter and inserts
that value into the stack. We can use the following steps to push an element on to the stack.
In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is
always deleted from top position. Pop function does not take any value as parameter. We can use
the following steps to pop an element from the stack...
A queue data structure can be implemented using one dimensional array. But, queue
implemented using array can store only fixed number of data values. The implementation of
queue data structure using array is very simple, just define a one dimensional array of specific
size and insert or delete the values into that array by using FIFO (First In First Out) principle
with the help of variables ‘front' and ‘rear'. Initially both ‘front’ and ‘rear' are set to -1.
Whenever, we want to insert a new value into the queue, increment 'rear' value by one and
then insert at that position. Whenever we want to delete a value from the queue, then
increment 'front' value by one and then display the value at 'front' position as deleted element.
In a queue data structure, enQueue() is a function used to insert a new element into the
queue. In a queue, the new element is always inserted at rear position. The enQueue()
function takes one integer value as parameter and inserts that value into the queue. We
can use the following steps to insert an element into the queue.
5
Engr. Muhammad Arslan Rafique
In a queue data structure, deQueue() is a function used to delete an element from the queue.
In a queue, the element is always deleted from front position. The deQueue() function does
not take any value as parameter. We can use the following steps to delete an element from
the queue.
6
Engr. Muhammad Arslan Rafique
Lab Tasks:
Task 1: Write a program to implement following operations of stack using array data
structure. Write your algorithm for this implementation.
Task 2: Write a program to implement following operations of Queue using array data
7
Engr. Muhammad Arslan Rafique
structure. Write your algorithm for this implementation.
8
Engr. Muhammad Arslan Rafique
Understanding Ability to Generation and Ability to
of Engineering perform Interpretation of relate
Tools experiment using results using experiment
(Criteria 1) Engineering Tool modern tools with theory and
2 (Dev C++) (Dev C++) its significance 15
(Criteria 2) (Criteria 3) (Criteria 4) Marks
6 4 3
Task 1
Task 2
Average
Marks
9
Engr. Muhammad Arslan Rafique