0% found this document useful (0 votes)
92 views6 pages

DSA Lab Report 9 Anas Zohrab

This lab report describes implementing a stack using a linked list data structure in C++. The key operations of a stack - Push, Pop, Peek and isEmpty were demonstrated. Push adds an element to the top of the stack. Pop removes and returns the top element. Peek returns the top element without removing it. IsEmpty checks if the stack is empty. Code examples showed adding elements, removing the top element, and checking for empty stack. The report concluded stack operations were successfully implemented using a linked list as designed.

Uploaded by

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

DSA Lab Report 9 Anas Zohrab

This lab report describes implementing a stack using a linked list data structure in C++. The key operations of a stack - Push, Pop, Peek and isEmpty were demonstrated. Push adds an element to the top of the stack. Pop removes and returns the top element. Peek returns the top element without removing it. IsEmpty checks if the stack is empty. Code examples showed adding elements, removing the top element, and checking for empty stack. The report concluded stack operations were successfully implemented using a linked list as designed.

Uploaded by

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

Data Structures and Algorithms

CIS-318
Lab Report no.9
Spring 2023
Obtained Marks
Total Marks

Lab Engineer Signature &


Comments

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

Department of Electrical Engineering


2 Lab 9 : Introduction to Stack and its implementation using Link List

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.1 Title: Introduction to Stack and its implementation using Link


List
9.2 Abstract:
The aim of this lab was to develop an understanding of Stacks, an abstract data type that plays
a vital role in computer science applications. This lab involved the implementation of Stacks
using linked lists in C++. The report began with a brief overview of the Stack data structure
and its fundamental operations: Push, Pop, Peek, and isEmpty. These operations were then
implemented on a Stack implemented using a linked 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.

Figure 1 Linked List

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.

9.5 Results and Code


Create a class Stack and implement the following functions
1. void Push(element) – pushes an element on the top of stack
2. element Pop() – removes and display the element on the top of stack
3. boolisEmpty() – checks if the stack is empty or not
4. boolisFull() – checks if the stack is full or not
5. void Peek() – display the contents of top element of stack

1. • void Push(element)
void push(int value) {

Node* newNode = new Node;


newNode->data = value;

newNode->next = top;

top = newNode;
}
2. Result:

Figure 2 adding 29 23 12 in the Stack

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:

Figure 3 removing element “29” from the top

• boolisEmpty()
bool isEmpty() {
return (top == NULL);
}

3. Result:

Figure 4 Printing Stack is Empty when the Stack is Empty

• 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:

Figure 5 Stack is printed and Stack elements are shown


6 Lab 9 : Introduction to Stack and its implementation using Link List

9.6 Home Tasks


Q1: What is time complexity of queue functions.
For a queue implemented using a linked list, the time complexity of enqueue,
dequeue, and isEmpty operations are O(1) since they only involve modifying the head and
tail pointers of the linked list. However, the time complexity of display operation in a linked
list implementation is O(n) since it requires traversing the entire linked list to print out all the
elements.

Q2: Compare Array based vs Linked List stack implementations.


Array-based implementation:
Simple and easy to implement.
Elements are stored in contiguous memory locations, which results in faster memory access.
Array-based implementation is generally more space-efficient than linked-list
implementation.

Link List-based implementation:


Dynamic memory allocation allows the stack to grow and shrink as needed.
Efficient if we need to insert or delete elements at arbitrary positions.
Stack overflow is unlikely to occur since we can allocate memory dynamically.

9.7 Discussion and Conclusion:


In this lab report, we explored the concept of a stack data structure, which is a collection
of elements that supports the push, pop, peek, and isEmpty operations. We also implemented
these operations using a linked list data structure.

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/

You might also like