DSSlides M2
DSSlides M2
MODULE – II
&
MODULE-III
By
Dr. M. Lakshmi Prasad
Associate Professor
CSE (DS)
Data Structures
MODULE – II
LINEAR DATA STRUCTURES
Abstract Data Type
➢ Abstract Data Type (or ADT), there is a set
of rules or description of the operations that
are allowed on data.
➢ It is based on a user point of view i.e., how a
user is interacting with the data.
➢ It does not specify how data will be organized
in memory and what algorithms will be used
for implementing the operations.
➢ The process of providing only the essentials
and hiding the details is known as abstraction.
However, we can choose to implement those
set of rules differently.
STACK ADT
Interface Stack
{
public Boolean empty();
//Boolean, true when the stack is empty
public void push (String str);
//Add new element into stack
public String pop()
//Delete element from stack
public String peek ( )
//return top element of stack
}
STACK
STACK ADT
➢ A stack is an ordered list in which all
insertions and deletions are made at one end
called the top.
Example:
STACK
STACK ADT
➢ In stack always the last item to be put in to the
stack is the first item to be removed. So stack
is a Last In First Out or LIFO data structure.
STACK
STACK ADT
➢ Push and Pop are the operations that are
provide for insertion of an element in to the
stack and the removal of an element from the
stack.
STACK
STACK ADT
➢ The add operation of the stack is called push operation
➢ The delete operation is called as pop operation.
➢ Push operation on a full stack causes stack overflow.
➢ Pop operation on an empty stack causes stack
underflow.
➢ SP is a pointer, which is used to access the top element
of the stack.
➢ If you push elements that are added at the top of the
stack;
➢ In the same way when we pop the elements, the
element at the top of the stack is deleted.
STACK OPERATIONS
Push:
Push operation is used to add new elements in to the
stack. At the time of adding first check the stack is
full or not. If the stack is full it generates an error
message "stack overflow".
STACK OPERATIONS
Pop:
Pop operation is used to delete elements from the
stack. At the time of deletion first check the stack is
empty or not. If the stack is empty it generates an
error message "stack underflow".
STACK OPERATIONS
Peep:
Find an element from the top of the stack.
REPRESENTATION OF A STACK USING
ARRAYS
➢ Let us consider a stack with 6 elements
capacity. This is called as the size of the stack.
➢ The number of elements to be added should
not exceed the maximum size of the stack.
➢ If we attempt to add new element beyond the
maximum size, we will encounter a stack
overflow condition.
➢ Similarly, you cannot remove elements beyond
the base of the stack. If such is the case, we
will reach a stack underflow condition.
ALGORITHMS FOR PUSH AND POP
Example:
(A + B) * (C - D)
a+b
((6-(3+2)*5)^2+3)
(a+b+c)/2
a*b/2
PREFIX NOTATION
1. Infix to postfix
2. Infix to prefix
3. Postfix to infix
4. Postfix to prefix
5. Prefix to infix
6. Prefix to postfix
ALGORITHM - INFIX TO POSTFIX
1)If the character is LEFT PARANTHESIS, push to the STACK.
2)If the character is an OPERAND, ADD to the POSTFIX
EXPRESSION.
3)If the character is an OPERATOR, check whether STACK is Empty
a) If the STACK is empty, push operator into STACK.
b) If the STACK is not empty, check the priority of the operator.
i) If the priority of scanned operator > operator present at
TOP of the STACK, then push operator into STACK.
ii) If the priority of scanned operator <= operator present at
the TOP of the STACK, then pop operator from STACK
and ADD to POSTFIX EXPRESSION and go to step i.
4)IF the character is RIGHT PARANTHESIS, then POP all operator and
operands from STACK until it reaches LEFT PARANTHESIS and ADD
to POSTFIX EXPRESSION.
5)After reading all characters, if STACK is not empty then POP and
ADD to POSTFIX EXPRESSION.
EXAMPLE - INFIX TO POSTFIX
Convert the following infix expression A + B * C – D / E * H into
its equivalent postfix expression.
EXAMPLE - INFIX TO POSTFIX
Convert ((A – (B + C)) * D) ↑ (E + F) infix expression to postfix
form:
SYMBOL POSTFIX STRING STACK REMARKS
( (
( ((
A A ((
- A ((-
( A ((-(
B AB ((-(
+ AB ((-(+
C AB C ((-(+
) AB C + ((-
) AB C + - (
* AB C + - (*
D AB C + - D (*
) AB C + - D*
^ AB C + - D* ^
( AB C + - D* ^(
E AB C + - D* E ^(
+ AB C + - D* E ^(+
F AB C + - D* E F ^(+
) AB C + - D* E F+ ^
End of String AB C + - D* E F+ ^ The input is now empty. Pop the output
Symbols from the stack until it is empty.
EXAMPLE - INFIX TO POSTFIX
Convert a + b * c + (d * e + f) * g the infix expression into postfix
form:
INFIX TO PREFIX
) )
D D )
- D )-
C CD )-
( -CD
* -CD *
) -CD *)
B B-CD *)
+ B-CD *)+
A AB-CD *)+
( +AB -C D *
End of * + A B - C D The input is now empty. Pop the output
String symbols from the stack until it is empty.
INFIX TO PREFIX
1. Reversed string: (H + G) / F / E + D – C * B ↑ A
2. Postfix of (H + G) / F / E + D – C * B ↑ A:
HG+F/E/D+CBA^*-
3. Reversed string of H G + F / E / D + C B A ^ * -:
-*^ABC+D/E/F/+GH
EVALUATION OF POSTFIX EXPRESSION
QUEUE ADT
➢ A queue can be defined as an ordered list which enables insert
operations to be performed at one end called REAR and delete
operations to be performed at another end called FRONT.
➢ Queue is referred to be as First In First Out (FIFO) list.
Example: people waiting in line for a rail ticket form a queue.
QUEUE
QUEUE ADT
➢ Enqueue: The enqueue operation is used to insert the element
at the rear end of the queue.
➢ Dequeue: The dequeue operation performs the deletion from
the front-end of the queue. It also returns the element which has
been removed from the front-end.
Queue ADT
1. If (FRONT == 0) then
2. print “Queue is Empty”
3. Exit
4. Else
5. ITEM = QUEUE[FRONT]
6. If ( FRONT == REAR) then
7. FRONT = 0
8. REAR = 0
9. Else
10. FRONT = FRONT + 1
11. End If
12. End If
13. Stop
Example
Representation of a Queue using Array
Example
Representation of a Queue using Array
Example
Representation of a Queue using Array
Example
Representation of a Queue using Array
Example
Representation of a Queue using Array
Representation of a Queue using Linked List
Circular Queue
➢ In a circular queue, the last element points to the first element
making a circular link.
➢ The main advantage of a circular queue over a simple queue is
better memory utilization.
➢ If the last position is full and the first position is empty, we
can insert an element in the first position. This action is not
possible in a simple queue.
Types of Queue
Priority Queue
➢ A priority queue is a special type of queue in which each
element is associated with a priority and is served according to
its priority.
➢ If elements with the same priority occur, they are served
according to their order in the queue.
➢ Insertion occurs based on the arrival of the values and removal
occurs based on priority.
Types of Queue
Algorithm:
Algorithm:
9
Deque Operations
Deque Operations