Lab9 Dsa w23
Lab9 Dsa w23
1
Engr. Muhammad Arslan Rafique
Lab No.9: Implementation of different operations on Stacks and Queue 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.
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.
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.
4
Engr. Muhammad Arslan Rafique
Lab Tasks:
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
11
Engr. Muhammad Arslan Rafique