0% found this document useful (0 votes)
13 views

Lab4 Dsa W23

The document describes operations on stacks and queues using arrays. It defines a stack as a linear data structure that follows LIFO (last in, first out) principles for insertion and deletion. The key operations on a stack using an array implementation are push to insert at the top, pop to delete from the top, and display to output the stack elements. Similarly, a queue follows FIFO (first in, first out) principles, with enqueue for insertion and dequeue for deletion. The document provides pseudocode for implementing these basic operations on stacks and queues using arrays.

Uploaded by

Rahhim Adeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab4 Dsa W23

The document describes operations on stacks and queues using arrays. It defines a stack as a linear data structure that follows LIFO (last in, first out) principles for insertion and deletion. The key operations on a stack using an array implementation are push to insert at the top, pop to delete from the top, and display to output the stack elements. Similarly, a queue follows FIFO (first in, first out) principles, with enqueue for insertion and dequeue for deletion. The document provides pseudocode for implementing these basic operations on stacks and queues using arrays.

Uploaded by

Rahhim Adeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

LABORATORY MANUAL

Subject Name: CS09204 Data Structures and Algorithms


Student Name Rahhim Adeem
Reg. No. 70129477
Date 10th-March-2023

MAPPING OF LAB TO CLOs & PLOs


CLO 3: Implement commonly used data structures and PLO 5: Modern Tool Usage
algorithms using programming software. Cognitive Domain: C3

Rubric for Modern Tool Usage


Criteria Attainment Score
Excellent Very good Good Fair Poor
(100-85%) (84-71%) (70-61%) (60-50%) (49-0%)
Understanding Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
of Engineering skillful ability to very good ability good ability to some ability to minimal or no
Tools describe and to describe and describe and/or describe ability to describe
explain the explain the explain the and/or explain and/or explain the
principles behind principles behind principles behind the principles principles behind
and applicability and applicability and applicability behind and and applicability
of engineering of engineering of engineering applicability of engineering
tools. tools. tools. of engineering tools.
tools.
Ability to Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
perform skillful ability to very good good ability to some ability to minimal or no
experiment identify and use ability to identify identify and use identify or use ability to identify
using the most relevant and use relevant tools for an tools for an or use tools for an
Engineering
tools for a range of tools for an engineering engineering engineering
Tool (Dev C++)
engineering engineering activity, but may activity. activity.
activities. activity. not identify the
most relevant
tool.
Generation and Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
Interpretation skillful ability to very good ability good ability to some ability to minimal or no
of results using generate results to generate generate results generate ability to generate
modern tools using modern results using using modern results using results using
(Dev C++)
tools. modern tools. tools. modern tools. modern tools.
Ability to Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
relate skillful ability to very good ability good ability to some ability to minimal or no
experiment understand to understand understand understand ability to
with theory significance of significance of significance of significance of understand
and its experiment and its experiment and experiment and its experiment significance of
relation to the its relation to the relation to the and its relation experiment and its
significance
theory theory theory. to the theory relation to the
theory

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)

A stack data structure can be defined as follows.


Stack is a linear data structure in which the operations are performed based on LIFO principle.
Stack can also be defined as
"A Collection of similar data items in which both insertion and deletion operations are performed
based on LIFO principle".

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

The following operations are performed on the stack...

1. Push (To insert an element on to the stack)


2. Pop (To delete an element from the stack)
3. Display (To display elements of the 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.

Stack Using Array

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.

Stack Operations using Array

A stack can be implemented using array as follows...


3
Engr. Muhammad Arslan Rafique
Before implementing actual operations, first follow the below steps to create an empty stack.

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.

push(value) - Inserting value into 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.

Step 1: Check whether stack is FULL. (top == SIZE-1)


Step 2: If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and
terminate the function.
Step 3: If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).

pop() - Delete a value from 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...

Step 1: Check whether stack is EMPTY. (top == -1)


Step 2: If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and
terminate the function.
Step 3: If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).

display() - Displays the elements of a Stack

We can use the following steps to display the elements of a stack.


Step 1: Check whether stack is EMPTY. (top == -1)
Step 2: If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
Step 3: If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display stack[i]
value and decrement i value by one (i--).
Step 3: Repeat above step until i value becomes '0'.
4
Engr. Muhammad Arslan Rafique
Queue Using Array

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.

Queue Operations using Array

Queue data structure using array can be implemented as


follows...
Before we implement actual operations, first follow the below steps to create an empty
queue.
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 user defined functions which are used in queue implementation.
Step 3: Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4: Define two integer variables 'front' and 'rear' and initialize both with '-1'.
(int front = -1, rear = -1)
Step 5: Then implement main method by displaying menu of operations list and make
suitable function calls to perform operation selected by the user on queue.

enQueue(value) - Inserting value into the queue

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.

Step 1: Check whether queue is FULL. (rear == SIZE-1)


Step 2: If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
Step 3: If it is NOT FULL, then increment rear value by one (rear++) and set
queue[rear] = value.

deQueue() - Deleting a value from 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.

Step 1: Check whether queue is EMPTY. (front == rear)


Step 2: If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!"
and terminate the function.
Step 3: If it is NOT EMPTY, then increment the front value by one (front ++). Then
display queue[front] as deleted element. Then check whether both front and rear are equal
(front == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).

display() - Displays the elements of a


Queue

We can use the following steps to display the elements of a


queue...

Step 1: Check whether queue is EMPTY. (front == rear)


Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
Step 3: Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same.

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.

Lab Assessment Total

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

Lab Engineer Name: M. Arslan Rafique Signature: ___________________

9
Engr. Muhammad Arslan Rafique

You might also like