DSA_ STACK
DSA_ STACK
DSA_ STACK
INTRODUCTION:
• Java Stack is a fundamental data structure that follows
the Last-In-First-Out (LIFO) principle. It is widely used in
computer programming to manage function calls,
expression evaluation, and various algorithmic
problems.
• The size of a stack can grow or shrink dynamically as
elements are pushed and popped from the top.
• Stacks are typically implemented using arrays or linked
lists, and the memory for a stack is allocated on the
system's call stack.
REPRESENTATION:
• A Stack can be a fixed specific size, or it can be
dynamic, i.e., the Stack size can be changed
dynamically. It can be represented by means of Pointer,
Array, Structure, and Linked List.
• TOP: The top of the stack represents the most recently
added element, which is the next element to
be removed.
• PUSH: Adding an element to the top of the stack is
called "pushing" the element onto the stack.
• POP: Removing an element from the top of the stack is
OPERATIONS:
• Push: Adds an element to the top of the stack.
Operation: ’push(element)’
Example: If the stack is [1,2,3] and you push, the element 4, then the stack become
[1,2,3,4].
• Pop: Removes and returns the element from the top of the stack.
Operation : pop()
Example: If the stack is [1,2,3,4] and you pop, the element 4, is removed and returned
leaving the stack as[1,2,3[.
• Peek: Returns the element at the top of the stack without removing it.
Operation: peek().
Example: If the stack is [1,2,3] peeking returns 3,and the stack remains [1,2,3].
• IsEmpty: Checks if the stack is empty.
Operation: isEmpty(),
• Size: Returns the number of elements in the stack.
Operation: size().
APPLICATIONS OF STACK:
• Browser History
Description:Web browsers use stacks to manage navigation history. Each visited page is pushed onto the stack, and the "Back"
button pops the most recent page off, allowing users to navigate backward through their history.
• Undo Mechanisms:
Description: Many applications use stacks to implement undo functionality. Each action is pushed onto a stack, and when an
undo is requested, the most recent action is popped off and reversed.
QUEUE:
INTRODUCTION:
• A queue is a linear data structure that is open at both
ends and the operations are performed in first in first
out (FIFO) order.
• FIFO Principle: The first element added to the queue will be the first
one to be removed. This is akin to people lining up in a queue; the
person who arrives first is the first to be served.
REPRESENTATION:
• Linear array is the best and easiest way to represent
the Queue. The Queue contains two ends, rare and
front. These ends point to the position from where the
insertion and deletion take place. The rare points to the
variable from where the insertion takes place and the
front points to the variable from where deletion takes
place.
OPERATIONS:
• The enqueue and dequeue are the two basic
operations performed in Queue.
Enqueue: Used to insert any data into the Queue.
Dequeue: Used to remove any data from Queue
TYPES OF QUEUE:
• Simple Queue: The basic form where elements are enqueued at the rear and
dequeued from the front.
• Circular Queue: A more efficient version of a simple queue where the last
position is connected back to the first position, forming a circle. This helps in
utilizing space more effectively.
• Priority Queue: A special type of queue where each element has a priority
associated with it. Elements with higher priority are dequeued before elements
with lower priority, regardless of their order of insertion.
• Double-ended Queue (Deque): A queue that allows insertion and deletion of
elements from both the front and the rear. It supports operations like ‘addFirst’,
‘addLast’, ‘removeFirst’, removeLast’.
APPLICATIONS OF QUEUE:
• Task Scheduling:
CPU Scheduling: Processes are scheduled using queues. Ready processes waiting for CPU time are
placed in a
queue
Disk Scheduling: Requests to read/write data from/to the disk are managed using a queue to ensure
fair access.
• Breadth-First Search (BFS) :
Traversal: BFS algorithm uses a queue to traverse nodes level by level. It ensures that nodes
closer to the start
node are processed first.
Shortest Path: BFS can be used to find the shortest path in an unweighted graph.
EXAMPLE OF STACK: