0% found this document useful (0 votes)
25 views11 pages

Lab9 Dsa w23

This document provides instructions for implementing common operations on stacks and queues using linked lists. It discusses using linked lists to implement stacks and queues since they allow for a variable number of elements, unlike arrays which require a fixed size. Operations covered for stacks using linked lists include push(), pop(), and display(). Operations covered for queues using linked lists include enQueue() and deQueue(). Pseudocode is provided outlining the steps to implement each operation. The document is intended as a laboratory manual for a data structures and algorithms course.

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)
25 views11 pages

Lab9 Dsa w23

This document provides instructions for implementing common operations on stacks and queues using linked lists. It discusses using linked lists to implement stacks and queues since they allow for a variable number of elements, unlike arrays which require a fixed size. Operations covered for stacks using linked lists include push(), pop(), and display(). Operations covered for queues using linked lists include enQueue() and deQueue(). Pseudocode is provided outlining the steps to implement each operation. The document is intended as a laboratory manual for a data structures and algorithms course.

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/ 11

LABORATORY MANUAL

Subject Name: CS09204 Data Structures and Algorithms


Student Name Rahhim Adeem
Reg. No. 70129477
Date 21st-April-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 No.9: Implementation of different operations on Stacks and Queue using Linked List

Stack using Linked List

The major problem with the stack implemented using array is, it works only for fixed number of data
values. That means the amount of data must be specified at the beginning of the implementation itself.
Stack implemented using array is not suitable, when we don't know the size of data which we are going to
use. A stack data structure can be implemented by using linked list data structure. The stack implemented
using linked list can work for unlimited number of values. That means, stack implemented using linked
list works for variable size of data. So, there is no need to fix the size at the beginning of the
implementation. The Stack implemented using linked list can organize as many data values as we want. 

In linked list implementation of a stack, every new element is inserted as 'top' element. That means every
newly inserted element is pointed by 'top'. Whenever we want to remove an element from the stack,
simply remove the node which is pointed by 'top' by moving 'top' to its next node in the list.
The next field of the first element must be always NULL.
Example

In above example, the last inserted node is 99 and the first inserted node is 25. The order of elements
inserted is 25, 32,50 and 99.

Operations
To implement stack using linked list, we need to set the following things before implementing actual
operations.
Step 1: Include all the header files which are used in the program. And declare all the user defined
functions.
Step 2: Define a 'Node' structure with two member’s data and next.
Step 3: Define a Node pointer 'top' and set it to NULL.
Step 4: Implement the main method by displaying Menu with list of operations and make suitable
function calls in the main method.

2
Engr. Muhammad Arslan Rafique
push(value) - Inserting an element into the Stack

We can use the following steps to insert a new node into the stack...
Step 1: Create a newNode with given value.
Step 2: Check whether stack is Empty (top == NULL)
Step 3: If it is Empty, then set newNode → next = NULL.
Step 4: If it is Not Empty, then set newNode → next = top.
Step 5: Finally, set top = newNode.

pop() - Deleting an Element from a Stack


We can use the following steps to delete a node from the stack...
Step 1: Check whether stack is Empty (top == NULL).
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 define a Node pointer 'temp' and set it to 'top'.
Step 4: Then set 'top = top → next'.
Step 7: Finally, delete 'temp' (free(temp)).

display() - Displaying stack of elements


We can use the following steps to display the elements (nodes) of a stack...
Step 1: Check whether stack is Empty (top == NULL).
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 Node pointer 'temp' and initialize with top.
Step 4: Display 'temp → data --->' and move it to the next node. Repeat the same until temp reaches to the
first node in the stack (temp → next != NULL).
Step 4: Finally! Display 'temp → data ---> NULL'.

Queue using Linked List


The major problem with the queue implemented using array is, It will work for only fixed number of data.
That means, the amount of data must be specified in the beginning itself. Queue using array is not suitable
when we don't know the size of data which we are going to use. A queue data structure can be
implemented using linked list data structure. The queue which is implemented using linked list can work
for unlimited number of values. That means, queue using linked list can work for variable size of data
(No need to fix the size at beginning of the implementation). The Queue implemented using linked list
can organize as many data values as we want. 

In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and the first
node is always pointed by 'front'.
Example

3
Engr. Muhammad Arslan Rafique
In above example, the last inserted node is 50 and it is pointed by 'rear' and the first inserted node is 10
and it is pointed by 'front'. The order of elements inserted is 10, 15, 22 and 50.

Operations
To implement queue using linked list, we need to set the following things before implementing actual
operations.
Step 1: Include all the header files which are used in the program. And declare all the user defined
functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define two Node pointers 'front' and 'rear' and set both to NULL.
Step 4: Implement the main method by displaying Menu of list of operations and make suitable function
calls in the main method to perform user selected operation.

enQueue(value) - Inserting an element into the Queue

We can use the following steps to insert a new node into the queue...
Step 1: Create a newNode with given value and set 'newNode → next' to NULL.
Step 2: Check whether queue is Empty (rear == NULL)
Step 3: If it is Empty then, set front = newNode and rear = newNode.
Step 4: If it is Not Empty then, set rear → next = newNode and rear = newNode.

deQueue() - Deleting an Element from Queue


We can use the following steps to delete a node from the queue...
Step 1: Check whether queue is Empty (front == NULL).
Step 2: If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and terminate from the
function
Step 3: If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
Step 4: Then set 'front = front → next' and delete 'temp' (free(temp)).

display() - Displaying the elements of Queue


We can use the following steps to display the elements (nodes) of a queue...
Step 1: Check whether queue is Empty (front == NULL).
Step 2: If it is Empty then, display 'Queue is Empty!!!' and terminate the function.
Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
Step 4: Display 'temp → data --->' and move it to the next node. Repeat the same until 'temp' reaches to
'rear' (temp → next != NULL).
Step 4: Finally! Display 'temp → data ---> NULL'.

4
Engr. Muhammad Arslan Rafique
Lab Tasks:

1. Write a program that insert stack at start using linked list?

5
Engr. Muhammad Arslan Rafique
2. Write a program that delete stack at start using linked list?

6
Engr. Muhammad Arslan Rafique
3. Write a program that Enqueue insertion at start using linked list?

7
Engr. Muhammad Arslan Rafique
4. Write a program that Dequeue deletion at end using linked list?

8
Engr. Muhammad Arslan Rafique
5. Write a program that Enqueue insertion at end using linked list?

9
Engr. Muhammad Arslan Rafique
6. Write a program that Dequeue deletion at start using linked list?

10
Engr. Muhammad Arslan Rafique
Lab Assessment
Understanding Ability to Generation and Ability to
of Engineering perform Interpretation of relate
Tools experiment using results using experiment Total
(Criteria 1) Engineering Tool modern tools with theory and 15
2 (Dev C++) (Dev C++) its significance Mark
(Criteria 2) (Criteria 3) (Criteria 4) s
6 4 3
Task 1

Task 2

Task 3

Task 4

Task 5

Task 6

Averag
e Marks

Lab Engineer Name: M. Arslan Rafique Signature: ___________________

11
Engr. Muhammad Arslan Rafique

You might also like