L3
L3
begin
if
topless than 1
return true
else
return false
else if
end
Types of Stacks:
• Register Stack:
This type of stack is also a memory element present in the
memory unit and can handle a small amount of data only.
The height of the register stack is always limited as the size of
the register stack is very small compared to the memory.
• Memory Stack:
This type of stack can handle a large amount of memory data.
The height of the memory stack is flexible as it occupies a
large amount of memory data.
Implementation of Stack:
There are two ways to implement a stack
• Using array
• Using linked list
Implementing Stack using Arrays:
• In array implementation, the stack is formed using an array. All the
operations are performed using arrays.
Advantages of array implementation:
• Easy to implement.
• Memory is saved as pointers are not involved.
Disadvantages of array implementation:
• It is not dynamic.
• It doesn’t grow and shrink depending on needs
at runtime.
Implementing Stack using Linked List:
• Every new element is inserted as a top element
in the linked list implementation of stacks in data
structures.
• That means every newly inserted element is
pointed to the top.
• Whenever you want to remove an element from
the stack, remove the node indicated by the top,
by moving the top to its previous node in the list.
Advantages of Linked List
implementation:
• The linked list implementation of a stack can grow and shrink
according to the needs at runtime.
• It is used in many virtual machines like JVM.
• Stacks are more secure and reliable as they do not get corrupted
easily.
• Stack cleans up the objects automatically.
Disadvantages of Linked List implementation:
• Requires extra memory due to the involvement of pointers.
• Random accessing is not possible in stack.
• The total size of the stack must be defined before.
• If the stack falls outside the memory it can lead to abnormal
termination.
Application of Stack in Data Structures:
• Expression Evaluation and Conversion
• Backtracking
• Function Call
• Parentheses Checking
• String Reversal
• Syntax Parsing
• Memory Management
Storing two stacks into one Array:
To store two stacks into one array, you can divide the array
into two parts and allocate each part to a separate stack.
One way to do this is to allocate the first half of the array to
one stack and the second half to the other stack.
Here's an example implementation in Python:
class TwoStacks:
def __init__(self, n):
self.n = n
self.array = [None] * n
self.top1 = -1
self.top2 = n
def push1(self, item):
if self.top1 < self.top2 - 1:
self.top1 += 1
self.array[self.top1] = item
else:
print("Stack Overflow")
return
def push2(self, item):
if self.top1 < self.top2 - 1:
self.top2 -= 1
self.array[self.top2] = item
else:
print("Stack Overflow")
def pop1(self):
if self.top1 == -1:
print("Stack Underflow")
return None
else:
item = self.array[self.top1]
self.top1 -= 1
return item
def pop2(self):
if self.top2 == self.n:
print("Stack Underflow")
return None
else:
item = self.array[self.top2]
self.top2 += 1
return item