Unit 2
Unit 2
o o Initially
Description: This algorithm inserts new element ITEM in linear array DATA with N elements
o If LOC=1 it means the element has to insert in beginning
o If LOC =N+1 it means the element have to be inserted at the end
o If LOC = J it means the elements have to be inserted at Jth Location
o o After movement of elements
Begin
o Step 1: [Initialize counter I with index of last element]
o I=N
o Step 2: While I>=LOC repeat steps 3 and 4
o Step 3: [Move the current element one position backwards]
o o After Insertion
DATA[I+1]=DATA[I]
o Step 4: [Decrement counter I]
o I=I-1
o Step 5:[Insert new element at the Location]
o DATA[LOC]=ITEM
o o Note:- for each one of these three cases the count of elements
Step 6:[ Update total under of array elements]
o N=N+1 increases by 1
o Exit
Arrays
Operation on arrays: o In the example array, elements from index J (4) to index 8 have to
Deletion: moved one position backwards so that a new element can be deleted
o o Initially
Description: This algorithm Delete element ITEM in linear array DATA with N elements
o If LOC=1 it means the element has to delete in beginning
10 30 21 56 27 48 89 51
o If LOC =N it means the element have to be deleted at the end
1 2 3 4 5 6 7 8 9 10 11
o If LOC = J it means the elements have to be deleted at Jth Location 12
o Begin
o o After movement of elements
Step 1: [Initialize counter I with index of last element]
o I=N
10 30 21 56 27 48 89 51
o Step 2: While I>=LOC repeat steps 3 and 4
o Step 3: [Move the current element one position backwards] o 1 2 3 4 5 6 7 8 9 10 11
o DATA[LOC]=DATA[LOC+1] 12
Push(top, item)
begin
if top = SIZE-1 then stack full
top = top + 1
stack (top) : = item;
end
Stack
Operation on stack:
Item denotes the ith item in the stack;
0 and SIZE-1 denote the index range of the array in use;
TOP is the position of the array up to which it is filled with the items of the stack.
Pop: Removes an item from the stack. The items are popped in the reverse order from when they are pushed. If the stack
is empty, then it is said to be an underflow condition.
Pop(top)
begin
if top = -1 then stack empty;
item := stack(top);
top = top - 1;
end;
Application of Stack
Infix: An infix expression is an expression in which operators (+, -, *, /) are written between the two operands.
For example
A+B
A+B-C
(A + B) + (C - D)
Postfix: Operator is written after the operand. It is :also known as Reverse Polish Notation.
For example:
AB+
AB+C-
AB+CD-+
Application of Stack
Why postfix representation of the expression?
The compiler scans the expression either from left to right or from right to left.
Consider the below expression: a + b * c + d
The compiler first scans the expression to evaluate the expression b * c, then again scan the expression to add a to it.
The result is then added to d after another scan.
The repeated scanning makes it very in-efficient.
It is better to convert the expression to postfix(or prefix) form before evaluation.
The corresponding expression in postfix form is: abc*+d+.
The postfix expressions can be evaluated easily using a stack.
We will cover postfix expression evaluation in a separate post.
Application of Stack
Infix to Postfix:
1. Add ) on the right of infix expression.
2. Scan the infix expression from left to right.
3. If the scanned character is an operand, output it.
4. Else,
1 If the precedence of the scanned operator is greater than
the precedence of the operator in the stack(or the stack is
empty or the stack contains a ‘(‘ , push it.
2 Else, Pop all the operators from the stack which are
greater than or equal to in precedence than that of the
scanned operator. After doing that, Push the scanned
operator to the stack. (If you encounter parenthesis while
popping then stop there and push the scanned operator in
the stack.)
5. If the scanned character is an ‘(‘, push it to the stack.
6. If the scanned character is an ‘)’, pop the stack and and
output it until a ‘(‘ is encountered, and discard both the
parenthesis.
6. Repeat steps 3-6 until infix expression is scanned.
7. Print the output
Application of Stack
Evaluation of Postfix Expression
The expressions written in postfix form are evaluated
faster compared to infix notation as parenthesis are not
required in postfix.
When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling.
When data is transferred asynchronously (data not necessarily received at same rate as sent) between two processes. Examples
include IO Buffers, pipes, file IO, etc.
Circular Queue
Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out)
principle and the last position is connected back to the first position to make a circle. It is also called ‘Ring Buffer’.
Circular Queue
Circular Queue
Operations on Circular Queue:
Front: Get the FRONT item from queue.
Rear: Get the last item from queue.
enQueue(value)
This function is used to insert an element into the circular queue.
In a circular queue, the new element is always inserted at Rear position.
deQueue()
This function is used to delete an element from the circular queue.
In a circular queue, the element is always deleted from FRONT position.
Circular Queue
enQueue(value)
First, we will check whether the Queue is full or not. Algorithm to insert an element in a circular queue
There are two cases in which the element cannot be inserted:
When FRONT ==0 && REAR = MAX-1, which Step 1: IF (REAR+1)%MAX == FRONT
means that FRONT is at the first position of the Queue Write " OVERFLOW "
and REAR is at the last position of the Queue.
FRONT== REAR + 1; Goto step 4
The above two condition can be combined as [End OF IF]
(REAR+1)%MAX== FRONT
Step 2: IF FRONT == -1 and REAR == -1
Initially the FRONT and REAR are set to -1. When we insert SET FRONT = REAR = 0
the first element in a Queue, FRONT and REAR both are set
ELSE
to 0.
Scenarios for inserting an element when Q is not empty. SET REAR = (REAR + 1) % MAX
There are two scenarios in which queue is not full or empty [END OF IF]
If REAR != MAX - 1, When we insert a new element,
the REAR gets incremented, i.e., REAR=REAR+1. Step 3: SET QUEUE[REAR] = VAL
If FRONT != 0 and REAR = MAX - 1, it means that Step 4: EXIT
queue is not full, then set the value of REAR to 0 and
insert the new element there.
The above two conditions can be combined and written as
REAR=(REAR+1)%MAX
Circular Queue
deQueue()
Algorithm to delete an element from the circular queue
The steps of dequeue operation are given below:
Step 1: IF FRONT == -1
First, we check whether the Queue is empty or not. If Write " UNDERFLOW "
the queue is empty, we cannot perform the dequeue Goto Step 4
[END of IF]
operation.
Step 2: SET VAL = QUEUE[FRONT]
If there is only one element left which is to be deleted,
Step 3: IF FRONT == REAR
then the FRONT and REAR are reset to -1. SET FRONT = REAR = -1
When there is more than one element then there are ELSE
two situations: SET FRONT = (FRONT + 1)%MAX