The document discusses the stack data structure, including its definition as a LIFO structure where elements are inserted and removed from one end called the top. Basic stack operations like push, pop, peek, and size are described. Implementations of stacks using lists and linked lists are covered, including code examples. Advantages of stacks like efficient memory usage and fast access are outlined, along with disadvantages like limited capacity and no random access. Applications of stacks in areas like compiler design, web browsers, and operating systems are also mentioned.
1. 2.5 Stack ADT
Definition
Basic Operations in Stack ADT
Advantages of Stack ADT
Disadvantages of Stack ADT
Application of Stack ADT
Prepared by
Mrs. G. Mareeswari,
Assistant professor,
Department of Information technology
Ramco Institute of Technology
Rajapalayam
2. Definition:
• A stack is a linear data structure in which the insertion of a new
element and removal of an existing element takes place at only one
end represented as the top of the stack.
• Stack is called a Last In First Out (LIFO) structure because the last
element which is added to the stack is the first element which is
deleted from the stack
• Only access the stack is the top element
3. Basic Operations on Stack
In order to make manipulations in a stack, there are certain
operations provided to us.
• push() to insert an element into the stack
• pop() to remove an element from the stack
• top()/peek() Returns the top element of
the stack without removing it.
• isEmpty() returns true if stack is empty else false.
• size() returns the size of stack.
• display() print all the elements in the stack
4. Implementation of Stack ADT
• There are several ways to implement the stack ADT
Some are
Implementation of Stack ADT using list
Linked list implementation of Stack ADT
Implementation of Stack ADT using list:
The stack is formed by using the list. All the operations
regarding the stack are performed using list functions in python.
5. Implementation of Stack ADT using list
• Push Operation – Insert an element at the end of the list so use
append function of list
• Pop Operation – Delete an element from the end of the list so use
pop function of list
• Peek Operation – return the last element in the list without removing
so access the last element by its index.
• IsEmpty operation – check the list is empty or not
• Size operation – return the length of the list
6.
7. Operations in implementation of stack using list
i) Creation of stack:
class Stack:
def __init__(self, size):
self.size = size
self.stack = []
ii) Push operation:
def push(self, item):
if len(self.stack) < self.size:
self.stack.append(item)
print("Pushed item:", item)
else:
print("Stack Overflow")
8. Operations in implementation of stack using list
iii) Pop operation:
def pop(self):
if len(self.stack) > 0:
item = self.stack.pop()
print("Popped item:", item)
else:
print("Stack Underflow")
iv) Peek operation:
def peek(self):
if len(self.stack) > 0:
print("Top item:", self.stack[-1])
else:
print("Stack is empty")
9. Operations in implementation of stack using list
v) Display operation
def display(self):
if len(self.stack) > 0:
print("Stack:", self.stack)
else:
print("Stack is empty")
vi) Size Operation
def size(self):
return len(self.stack)
11. Linked list Implementation of Stack ADT
• We can use linked list to implement stack. Linked list
allocates the memory dynamically.
• However, time complexity in both the scenario is same for all
the operations i.e. push, pop and peek.
• In linked list implementation of stack, the nodes are
maintained non-contiguously in the memory.
• Each node contains a pointer to its immediate successor
node in the stack.
12. Linked list Implementation of Stack ADT
The top most node in the stack always contains null in its address field.
13. Operations in linked list implementation of stack
i) Creation of stack:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.top = None
ii) Push operation:
def push(self, item):
new_node = Node(item)
new_node.next = self.top
self.top = new_node
print("Pushed item:", item)
14. Operations in linked list implementation of stack
iii) Pop operation:
def pop(self):
if self.top is not None:
item = self.top.data
self.top = self.top.next
print("Popped item:", item)
else:
print("Stack Underflow")
iv) Peek operation:
def peek(self):
if self.top is not None:
print("Top item:", self.top.data)
else:
print("Stack is empty")
15. Operations in linked list implementation of stack
v) display()
def display(self):
if self.top is not None:
current = self.top
stack_items = []
while current:
stack_items.append(current.data)
current = current.next
print("Stack:", stack_items)
else:
print("Stack is empty")
17. Advantage of stack
• Easy implementation: Stack data structure is easy to implement using arrays or
linked lists, and its operations are simple to understand and implement.
• Efficient memory utilization: Stack uses a contiguous block of memory, making
it more efficient in memory utilization as compared to other data structures.
• Fast access time: Stack data structure provides fast access time for adding and
removing elements as the elements are added and removed from the top of the
stack.
• Helps in function calls: Stack data structure is used to store function calls and
their states, which helps in the efficient implementation of recursive function calls.
• Supports backtracking: Stack data structure supports backtracking algorithms,
which are used in problem-solving to explore all possible solutions by storing the
previous states.
• Used in Compiler Design: Stack data structure is used in compiler design for
parsing and syntax analysis of programming languages.
18. Disdvantage of stack
• Limited capacity: Stack data structure has a limited capacity as it can
only hold a fixed number of elements. If the stack becomes full,
adding new elements may result in stack overflow, leading to the loss
of data.
• No random access: Stack data structure does not allow for random
access to its elements, and it only allows for adding and removing
elements from the top of the stack. To access an element in the
middle of the stack, all the elements above it must be removed.
19. Application of stack
• CD/DVD stand.
• Stack of books in a book shop.
• Call center systems.
• Undo and Redo mechanism in text editors.
• The history of a web browser is stored in the form of a stack.
• Call logs, E-mails, and Google photos in any gallery are also stored in form
of a stack.
• YouTube downloads and Notifications are also shown in LIFO format(the
latest appears first ).
• Allocation of memory by an operating system while executing a process.