DS Module 2
DS Module 2
STACK
• Stack is a linear data structure which follows a particular order in
which the operations are performed.
• It is a linear list where all insertions and deletions are permitted only
STACK at one end of the list that is called TOP of the list.
• TOP is a pointer which points the last or top most element of Stack.
DS Module 2 • Insertion and Deletion in stack can only be done from top only.
• Definition
“Stack is a collection of similar data items in which both insertion and
deletion operations are performed based on LIFO principle”.
Stack Implementation
Operations on Stack
• Two ways
• There are two basic operations performed in a Stack:
1. Stack implementation using array.
1. Push
2. Pop 2. Stack implementation using linked list.
Overflow condition
1. Push operation is used to add or insert new
elements into the stack. That is insertion operation in Overflow occurs when we try to insert an
stack.
item into a filled stack.
2. Pop operation is used to delete or remove an Underflow condition
element from the stack. That is deletion operation in
stack. Underflow occurs during deletion that is
When a stack is completely full, it is said to be when we try to delete from empty stack.
Overflow state and if stack is completely empty, it is
said to be Underflow state.
• If the list is empty then the item is to be pushed as the start node of the list.
• This includes assigning value to the data part of the node and assign null to
the address part of the node.
• If there are some nodes in the list already, then we have to add the new
element in the beginning of the list (to not violate the property of the stack).
• For this purpose, assign the address of the starting element to the address
field of the new node and make the new node, the starting node of the list.
13-07-2023
• In a + b*c, the expression part b*c will be evaluated first, 3. If the scanned character is an operand, add it to
with multiplication as precedence over addition. postfix expression.
• We here use parenthesis for a + b to be evaluated first, like 4. Else if the scanned character is an “(“, push it to the
(a + b)*c. stack.
13-07-2023
Example 2
• Infix Expression: (3 + 4) * (2 / 2)
• Postfix Expression: 34+22/* QUEUES
• Evaluation: 7
DS Module 2
Representation of Queues:
Queues
• Linear data structure • 1. Array representation
• FIFO structure (First In First Out) • 2.Linked list representation (Using pointers)
• 2 pointers: Front & Rear • Conditions
• 2 operations: Insertion & deletion • Overflow: rear=maxsize (rear=maxsize-1)
• Through front----deletion
• Underflow: front=0 (front=-1)
• Through rear------Insertion
• One element: front==rear!=0
• During insertion :rear=rear+1
• During deletion: front=front+1
Types of Queue
1.Normal Queue
2. Circular Queue
3.Deque
4.Priority Queue
3.DEQUE
• Stands for Double Ended Queue
• Pronounced as either “deck” or “DQ”
• It is a list in which elements can be added or removed
at either end but not in the middle.
• DQ is maintained by a circular array with LEFT(FRONT)
and RIGHT(REAR) pointers.
13-07-2023
Operation on Deque
• There are 3 basic operations on input restricted
deque
• - Insertion at rear end
• - Deletion at rear end
• - Deletion at front end
• There are 3 basic operations output restricted
deque
• - Insertion at rear end
• - Insertion at front end
• - Deletion at front end
• A node ‘x’ precedes a node ‘y’ in the list when ‘x’ has
higher priority than ‘y’ or when both have same
priority but ‘x ‘was inserted to the list before ‘y’.
• Priority numbers will operate in the usual way, ie the
lower the priority number, the higher the priority.
• The first element in the list is the element with
maximum priority. So it is very simple to delete an
element from priority queue.
• Adding an element to priority queue is much more
complicated. Suppose we adds an item with priority
number N to a priority queue , then
– Traverse the list until finding a node ‘x’ whose priority
number exceeds N.
– Insert the node item in front of node ‘x’.
– If no such node is found , insert item as the last node of
the list.