DSA Lab Report 9 Anas Zohrab
DSA Lab Report 9 Anas Zohrab
CIS-318
Lab Report no.9
Spring 2023
Obtained Marks
Total Marks
Student Name
1. Anas Zohrab
Section: A (Electronics)
Experiment No: 09 Date of Submission:
May-8-2023
Experiment Title:
Introduction to Stack and its implementation using Link List
Batch: Teacher:
BSEE 2019-23 Dr. Kamran Safdar
Semester Lab Engineer:
8th Mr. Ghaznooq Ahmad
Table of Contents
Introduction to Stack and its implementation using Link List ........................................................................... 1
9.1 Title: Introduction to Stack and its implementation using Link List ...................................................... 3
9.2 Abstract: .................................................................................................................................................. 3
9.3 Objectives: .............................................................................................................................................. 3
9.4 Introduction: ............................................................................................................................................ 3
9.5 Results and Code..................................................................................................................................... 4
1. • void Push(element) ............................................................................................................................ 4
2. Result:.................................................................................................................................................... 4
........................................................................................................................................................................ 4
1. • element Pop() ..................................................................................................................................... 4
2. Result:.................................................................................................................................................... 5
........................................................................................................................................................................ 5
• boolisEmpty() ......................................................................................................................................... 5
3. Result:.................................................................................................................................................... 5
........................................................................................................................................................................ 5
• void Peek() ............................................................................................................................................. 5
4. Result:.................................................................................................................................................... 5
........................................................................................................................................................................ 5
9.6 Home Tasks ............................................................................................................................................ 6
9.7 Discussion and Conclusion: .................................................................................................................... 6
9.8 References: .............................................................................................................................................. 6
3 Lab 9 : Introduction to Stack and its implementation using Link List
9.3 Objectives:
• Understanding of stack
• Implementing the basic operations of stack using link list
9.4 Introduction:
Data Structure List:
A stack is an abstract data type that represents a collection of elements, with two primary
operations: push and pop. It follows the Last-In-First-Out (LIFO) principle, which means that
the last element pushed into the stack is the first one to be popped out. In other words, the most
recently added element is always at the top of the stack.
Stacks are essential data structures in computer science and are used in a variety of applications,
including evaluating expressions, parsing, recursion, and backtracking. They provide a simple
and efficient way of storing and retrieving data, making them an integral part of algorithm
design.
Basic Operations:
Push: The push operation adds an element to the top of the stack. This is done by incrementing
the stack pointer and inserting the new element into the memory location pointed to by the
stack pointer. If the stack is implemented using an array, the push operation may result in
overflow if the stack size exceeds the allocated memory. In the case of a linked list
implementation, the push operation involves creating a new node and linking it to the previous
top node.
Pop: The pop operation removes the element at the top of the stack and returns it. This is done
by first checking if the stack is empty, and if it is not, then decrementing the stack pointer to
4 Lab 9 : Introduction to Stack and its implementation using Link List
point to the next element in the stack. The element at the new top of the stack is then returned.
If the stack is implemented using a linked list, the top node is removed and the second-to-top
node becomes the new top node.
Peek: The peek operation returns the element at the top of the stack without removing it. This
is done by accessing the memory location pointed to by the stack pointer, or by returning the
value stored in the top node of a linked list implementation. The peek operation is useful when
you want to examine the next element to be popped, without actually removing it from the
stack.
isEmpty: The isEmpty operation checks whether the stack is empty. This is done by verifying
if there are any elements in the stack. If the stack pointer is pointing to the first element in the
stack, the stack is considered empty. If the stack is implemented using a linked list, an empty
stack would have a null top node.
1. • void Push(element)
void push(int value) {
newNode->next = top;
top = newNode;
}
2. Result:
1. • element Pop()
void pop() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
return;
}
Node* temp = top;
top = top->next;
5 Lab 9 : Introduction to Stack and its implementation using Link List
cout << temp->data << " is popped from the stack." << endl;
delete temp;
}
2. Result:
• boolisEmpty()
bool isEmpty() {
return (top == NULL);
}
3. Result:
• void Peek()
void display() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
return;
}
cout << "Elements of the stack are: ";
Node* temp = top;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
4. Result:
We learned that stacks are widely used in computer science and are essential in various
applications such as expression evaluation, recursion, parsing, and backtracking. We also
discussed the basic operations of a stack, which include push, pop, peek, and isEmpty.
Furthermore, we implemented a stack using a linked list, which is a dynamic data structure that
allows the stack to grow and shrink as needed. We also compared the linked list-based
implementation with the array-based implementation and discussed their respective
advantages.
In conclusion, this lab report provided an excellent introduction to the concept of a stack
data structure and its basic operations. It also demonstrated how to implement a stack using a
linked list and compared it with an array-based implementation. Overall, this lab report serves
as an excellent starting point for anyone interested in learning more about stacks and their
applications.
9.8 References:
• •Tutorials Point. (n.d.). Stacks. Retrieved from
https://www.tutorialspoint.com/data_structures_algorithms/queue_data_structure.htm
• •Programiz. (n.d.). Stacks in C++. Retrieved from https://www.programiz.com/dsa/queue
• •GeeksforGeeks. (n.d.). Stacks. Retrieved from https://www.geeksforgeeks.org/queue-data-
structure/