Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Algorithm of Data Structure

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 77

Unit-2

Linked List
 It is an ordered collection of finite homogeneous data elements
called nodes where the linear order is maintained by means of links
or pointers.
 Each node is divided into two parts:
- the first part contains information of the element
- the second part called the link field or the next pointer field which
contains the address of the next node
DATA LINK

INFO ADDRESS Link to the next node


Representation of Linked List in memory
 In memory the linked list is stored in scattered cells (locations).The
memory for each node is allocated dynamically means as and when
required. So the Linked List can increase as per the user wish and the size
is not fixed, it can vary.
 LinkedList contains an link element called first.
 Each Link carries a data field(s) and a Link Field called next.
 Each Link is linked with its next link using its next link.
 Last Link carries a Link as null to mark the end of the list.

FIRST
Representation of Linked List in memory
Types of Linked List
i. Linear Linked List: In linear linked list, each node is divided in two parts: First part
contains the information of the element. And the second part contains the address of
the next node in the list. That is, echnode has a single pointer to the next node, and in
the last node’s case a null pointer representing that there are no nodes in the linked list.

ii. Doubly Linked List: They are also called two way lists. In the case of doubly linked list,
two link fields are maintained instead of one as in singly linked list which help in
accessing both successor and predecessor nodes.
Types of Linked List
iii. Circular Linked List: It is just like a linear linked list in which the second part of the last
node contains the address of the first node in the list i.i. the second field of the last
node does not point to the null pointer rather it points back to the beginning of the
linked list

iii. Header Linked List: It always contain a single ode called as the header node which is
present at the beginning of the linked list. This node contains some extra information
about the list like number of nodes it contains , is list sorted or unsorted.
Singly/ Linear Linked List
 The linked list is accessed from an external pointer variable say FIRST which points to first
node in the list.
 Last Node contains a special value in the ink field known as NULL and indicate end of the
list.
 The list with no nodes is called empty or null lists denoted by
FIRST=NULL
We can create a linked list in c using the following code:
struct node
{
int info; //where info is an integer variable
struct node *link; //link is an pointer variable which points to address of next node
};
struct node* FIRST;
Operations on Linked List
 The operation on linked list are:
- Creation
- Traversing
- Insertion
- Deletion
- Searching
Garbage Collection
 When an object is no longer used, heap space it occupies must be recycled so
that space is available for new object.
 OS periodically collects all the deleted space onto free storage list which does
this is called garbage collection.
 It is a list of free nodes arranged in the form of stack.
where we have AVAIL as a pointer variable just like first and it contain address of
top most free nodes of availability list.
 To insert a new node into linked list, we have to carry a free node from available
list from AVAIL position.
 Sometimes a new node are to be inserted but there is no available space that is
free storage list is empty(AVAIL=NULL), this situation is called overflow.
Algorithm for creation of linked list
1) Set PTR=AVAIL
Set AVAIL=LINK[AVAIL]
Read INFO[PTR]
FIRST=PTR
2) CH=‘Y’
3) Repeat 4) To 5) while CH==‘Y’
4) Set CPT=AVAIL
Set AVAIL=LINK[AVAIL]
Read INFO[CPT]
Set LINK[PTR]=CPT
Set PTR=CPT
5) Read choice <Y/N> into CH for more nodes
6)LINK[PTR]=NULL
7) STOP
Algorithm for Traversing a linked list
1) PTR=START
2) Repeat 3) and 4) WHILE PTR!=NULL
3) Read and Print INFO[PTR]
4) PTR=LINK[PTR]
End of while loop
5) STOP

Time Complexity:
For traversing a linked list the time taken depends on the size of the input, hence the
time complexity is: O(n)
Insertion in a Linked List
1) Insertion in a linked list is possible at different location :
- At the beginning
- At the end
- After a given position
2) Before insertion, we have to check the overflow condition which is,
If AVAIL==NULL, write list is overflow and exit
3) To create a new node we have,
PTR= AVAIL
AVAIL=LINK[AVAIL]
Algorithm to insert at the beginning
1) If AVAIL==NULL, write overflow and exit
2) Set PTR=AVAIL and AVAIL=LINK[AVAIL]
3) Set INFO[PTR]=ITEM
4) Set LINK[PTR]=START
5) Set START=PTR
6) STOP

Time Complexity :
For inserting an element at the beginning we need to insert the element at the beginning
location and update the start pointer which takes a constant time , hence the time
complexity is :O(1)
Algorithm to insert at the end
1) If AVAIL==NULL, write overflow and exit
2) Set PTR=AVAIL
Set AVAIL=LINK[AVAIL]
Read INFO[PTR]
3) Set CPT=START
4) Repeat STEP 5) while LINK[CPT]=NULL
5) Set CPT=LINK[CPT]
End of while loop
6) Set LINK[CPT]=PTR
Set LINK[PTR]=NULL
Set INFO[PTR]=ITEM
7) STOP
Algorithm to insert at the end
Time Complexity :
For inserting an element at the end:
(i) We can either keep a pointer pointing to last node, insert the element at the
end and then update the pointer kept at the last which takes a constant time
, hence the time complexity is :O(1)
(ii) Or we will traverse the entire linked list with the help of a pointer and reach
the last node then insert the element at the end and update the pointer,
this takes linear time i.e the time taken depends on the size of input, hence
the time complexity is :O(n)
Algorithm for insertion after a given
position
1) If AVAIL==NULL, then write overflow and exit
2) Read DATA as the information of node after which insertion
3) PTR=AVAIL
AVAIL=LINK[AVAIL]
Read INFO[PTR]
4) CPT=START
5) Repeat 6) while INFO[CPT]!=DATA where DATA is the node after which you want to insert
6) CPT=LINK[CPT]
End of while loop
7) LINK[PTR]=LINK[CPT]
LINK[CPT]=PTR
8) STOP
Algorithm for insertion after a given
position
Time Complexity:
In order to find the position where we want to insert the element, it
takes linear time which is O(n)
In order to insert the element at that position it takes a constant
time, which is O(1).
The time complexity to insert after a given position is O(n).
Deletion in Singly Linked List
 Before deleting, we will check underflow condition which is,
If FIRST=NULL ,then write underflow and exit
 After deletion we also can also be done:
- At the beginning
- At the end
Algorithm for Deletion in the beginning
1) If FIRST=NULL, then write underflow and exit
2) If LINK[FIRST]=NULL then
Return INFO[PTR]
FIRST=NULL
else
Return INFO[PTR]
FIRST=LINK[PTR]
3) STOP
Time Complexity :
For deleting an element from the beginning we need to update the start pointer and
delete the element at the previous beginning location takes a constant time , hence the
time complexity is :O(1)
Algorithm for deletion from the end
1) If FIRST=NULL then write underflow and exit
2) IF LINK[FIRST]=NULL then
return INFO[FIRST]
FIRST=NULL
else
CURR=FIRST
Repeat while LINK[CURR]!=NULL
PREV=CURR
CURR=LINK[CURR]
Return INFO[CURR]
3) LINK[PREV]=NULL
4) STOP
Algorithm for deletion from the end
Time Complexity :
We will traverse the entire linked list with the help of a pointer and
reach the last node , update the pointer, then delete the element at
the end and this takes linear time i.e the time taken depends on the
size of input, hence the time complexity is :O(n)
Algorithm for searching in a Linked List
1) Read DATA as information of node to be searched
2) PTR=FIRST
3) Repeat 4) and 5) while INFO[PTR]!=DATA
4) If INFO[PTR]=DATA then print search is successful
5) PTR=LINK[PTR]
6) Print this node is not present in the linked list
7) STOP
Doubly Linked List
 A doubly linked list is one in which all nodes are linked together by multiple links
which help in accessing both the successor and predecessor node for any
arbitrary node within the list.
 Every nodes in the doubly linked list has three fields: LeftPointer, RightPointer
and DATA.
 The last node has a next link with value NULL, marking the end of the list, and
the first node has a prev link with the value NULL. The start of the list is marked
by the head pointer.
Algorithm to perform insertion in a
Doubly Linked List
 At the beginning:
i. If AVAIL==NULL then write overflow and exit
ii. Set PTR=AVAIL
AVAIL=LINL[AVAIL]
Read INFO[PTR]
iii. If START==NULL then
Set NEXT[PTR]=NULL
Set PREV[PTR]=NULL
Set INFO[PTR]=DATA
Set START=PTR
Algorithm to perform insertion in a
Doubly Linked List
Else
iv. Set INFO[PTR]=DATA
Set PREV[PTR]=NULL
Set NEXT[PTR]=START
Set PREV[START]=PTR
Set START=PTR
End of if loop
v. STOP
Algorithm to perform insertion in a
Doubly Linked List
 At the end:
i. If AVAIL==NULL then write overflow and exit
ii. Set PTR=AVAIL
AVAIL=LINL[AVAIL]
Read INFO[PTR]
iii. If START==NULL then
Set NEXT[PTR]=NULL
Set PREV[PTR]=NULL
Set INFO[PTR]=DATA
Set START=PTR
Algorithm to perform insertion in a
Doubly Linked List
Else
iv. Set TEMP=START
Repeat while(NEXT[TEMP] !=NULL)
Set TEMP=NEXT[TEMP
end of while loop
Set NEXT[TEMP]=PTR
Set PREV[PTR]=TEMP
Set NEXT[PTR]=NULL
End of if
v. STOP
Algorithm to perform deletion in a
Doubly Linked List
 From the beginning
i. If START==NULL then write unferflow and exit
ii. If NEXT[START]==NULL
then Set START=NULL
Else
Set PTR=START
START=NEXT[START]
PREV[START]=NULL
End of if loop
iii. Stop
Algorithm to perform deletion in a
Doubly Linked List
 From the end
i. If START==NULL then write unferflow and exit
ii. If NEXT[START]==NULL
then Set START=NULL
Else
Set PTR=START
Repeat while (NEXT[PTR]!=NULL)
Set PTR=NEXT[PTR]
End of while loop
Algorithm to perform deletion in a
Doubly Linked List

Set NEXT[PTR]=NULL
Set PREV[PTR]=NULL
End of if loop
iii. Stop
STACKS
 Stack is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO(Last In First Out) or FILO(First
In Last Out).
Consider an example of plates stacked over one another in the canteen. The plate
which is at the top is the first one to be removed, i.e. the plate which has been
placed at the bottommost position remains in the stack for the longest period of
time. So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out)
order.

 A stack can be implemented by means of Array, Structure, Pointer, and Linked


List.
 Stack can either be a fixed size one or it may have a sense of dynamic resizing.
STACKS

Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology,
insertion operation is called PUSH operation and removal operation is called POP operation.
Basic Operations in a Stack

 Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic
stuffs, a stack is used for the following two primary operations −
i. push() − Pushing (storing) an element on the stack.
ii. pop() − Removing (accessing) an element from the stack.

 To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following
functionality is added to stacks −
i. peek() − get the top data element of the stack, without removing it.
ii. isFull() − check if stack is full.
iii. isEmpty() − check if stack is empty.

At all times, we maintain a pointer to the last pushed data on the stack. As this pointer always represents the top
of the stack, hence named top. The top pointer provides top value of the stack without actually removing it.
Basic Operations in a Stack

i. peek() − Implementation of peek() function in C programming language :


int peek()
{
return stack[top];
}
ii. isFull() -Implementation of isFull() function in C programming language :
bool isfull()
{
if(top == MAXSIZE)
return true;
else
return false; }
Basic Operations in a Stack

iii. isEmpty() Implementation of isEmpty() function in C programming language :


bool isempty()
{
if(top == -1)
return true;
else
return false;
}
Push Operation in Stack
 The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −
- Step 1 − Checks if the stack is full.
- Step 2 − If the stack is full, produces an error and exit.
- Step 3 − If the stack is not full, increments top to point next empty space.
- Step 4 − Adds data element to the stack location, where top is pointing.
- Step 5 − Returns success.
Algorithm for Push Operation in Stack
begin procedure push: stack, data
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
-
Algorithm for Push Operation in Stack
Implementation in C
void push(int data)
{
if(!isFull())
{
top = top + 1;
stack[top] = data;
}
else
{
printf("Could not insert data, Stack is full.\n");
}}
Pop Operation

 Accessing the content while removing it from the stack, is known as a Pop Operation. In
an array implementation of pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to point to the next value. But
in linked-list implementation, pop() actually removes data element and deallocates
memory space.
 A Pop operation may involve the following steps −
 Step 1 − Checks if the stack is empty.
 Step 2 − If the stack is empty, produces an error and exit.
 Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
 Step 4 − Decreases the value of top by 1.
 Step 5 − Returns success.
Pop Operation
Algorithm for Pop Operation

begin procedure pop: stack


if stack is empty
return null
endif
data ← stack[top]
top ← top – 1
return data end procedure
Algorithm for Pop Operation
Implementation in C
int pop(int data)
{
if(!isempty())
{
data = stack[top];
top = top - 1;
return data;
}
else
{
printf("Could not retrieve data, Stack is empty.\n");
}}
APPLICATIONS OF STACK

The applications are:


i. Paranthesis Matching
ii. Conversion from infix to postfix, infix to prefix and postfix to infix
iii. Evaluation of postfix
Paranthesis Matching

 Given an expression string exp , write a program to examine whether the pairs and the
orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.
 Example:
- Input: exp = “[()]{}{[()()]()}”
Output: Balanced
- Input: exp = “[(])”
Output: Not Balanced
Algorithm for Paranthesis Matching

 Algorithm:
 Declare a character stack S.
 Now traverse the expression string exp.
 If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘) then push it to
stack.
 If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then pop from
stack and if the popped character is the matching starting bracket then fine
else parenthesis are not balanced.
 After complete traversal, if there is some starting bracket left in stack then “not
balanced”
Implementation of Paranthesis Matching
Notations in Stack

 The way to write arithmetic expression is known as a notation. An arithmetic expression can be written in three different but
equivalent notations, i.e., without changing the essence or output of an expression. These notations are −
i. Infix Notation
ii. Prefix (Polish) Notation
iii. Postfix (Reverse-Polish) Notation

i. Infix Notation: We write expression in infix notation, e.g. a - b + c, where operators are used in-between operands. It is
easy for us humans to read, write, and speak in infix notation but the same does not go well with computing devices. An
algorithm to process infix notation could be difficult and costly in terms of time and space consumption.
ii. Prefix Notation: In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For
example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.
iii. Postfix Notation: This notation style is known as Reversed Polish Notation. In this notation style, the operator is postfixed
to the operands i.e., the operator is written after the operands. For example, ab+. This is equivalent to its infix notation a +
b.
Notations in Stack
Parsing Expressions

 To parse any arithmetic expression, we need to take care of operator precedence and associativity also.
i. Precedence
 When an operand is in between two different operators, which operator will take the operand first, is
decided by the precedence of an operator over others. For example −
a +b * c  a +(b * c)
As multiplication operation has precedence over addition, b * c will be evaluated first.

ii. Associativity
 Associativity describes the rule where operators with the same precedence appear in an expression.
 For example, in expression a + b − c, both + and – have the same precedence, then which part of the
expression will be evaluated first, is determined by associativity of those operators.
 Here, both + and − are left associative, so the expression will be evaluated as (a + b) − c.
Parsing Expressions
 Precedence and associativity determines the order of evaluation of an expression. Following
is an operator precedence and associativity table (highest to lowest) −

 The above table shows the default behavior of operators. At any point of time in expression
evaluation, the order can be altered by using parenthesis.
For example − In a + b*c, the expression part b*c will be evaluated first, with multiplication as
precedence over addition. We here use parenthesis for a + b to be evaluated first, like (a + b)*c.
Algorithm for Conversion from infix to
postfix
Let, X is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression Y.

 Push “(“onto Stack, and add “)” to the end of X.

 Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack is empty.

 If an operand is encountered, add it to Y.

 If a left parenthesis is encountered, push it onto Stack.

 If an operator is encountered ,then:


 Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which has the same precedence as or higher precedence than
operator.

 Add operator to Stack.


[End of If]

 If a right parenthesis is encountered ,then:


 Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a left parenthesis is encountered.

 Remove the left Parenthesis.


[End of If]
[End of If]

 END.
Conversion from infix to postfix
 Infix Expression: A+ (B*C-(D/E^F)*G)*H, where ^ is an exponential operator.
Conversion from postfix to infix
 Algorithm
1.While there are input symbol left
…1.1 Read the next symbol from the input.
2.If the symbol is an operand
…2.1 Push it onto the stack.
3.Otherwise,
…3.1 the symbol is an operator.
…3.2 Pop the top 2 values from the stack.
…3.3 Put the operator, with the values as arguments and form a string.
…3.4 Push the resulted string back to stack.
4.If there is only one value in the stack
…4.1 That value in the stack is the desired infix string.
Conversion from infix to prefix
 Step 1. Push “)” onto STACK, and add “(“ to end of the A
 Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty
 Step 3. If an operand is encountered add it to B
 Step 4. If a right parenthesis is encountered push it onto STACK
 Step 5. If an operator is encountered then:
 a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same
 or higher precedence than the operator.
 b. Add operator to STACK
 Step 6. If left parenthesis is encontered then
 a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is
encounterd)
 b. Remove the left parenthesis
 Step 7. Exit
Conversion from infix to prefix
Evaluation of a PostFix notation
Algorithm :
 1) Add ) to postfix expression.
2) Read postfix expression Left to Right until ) encountered
3) If operand is encountered, push it onto Stack
[End If]
4) If operator is encountered, Pop two elements
i) A -> Top element
ii) B-> Next to Top element
iii) Evaluate B operator A
push B operator A onto Stack
5) Set result = pop
6) END
Evaluation of a PostFix notation
Recursion
 Recursion can be defined as defining anything in terms of itself. It can be also defined as repeating items in a self-similar way.

 Recursion involve a situation in which a function call itself directly or indirectly.

 It must have a well defined base case.

 Must have well defined steps that lead to the stopping state.

 It is the process of repeating items in a self-similar way where a program allows you to call a function inside the same function.

 For a recursive algorithm, there are one or more base cases for which no recursion is required and all chains of recursion eventually end
up at one of the base cases.

 Example:

fact(n):

if n == 0: return 1

else: return n * fact(n-1) // Recursive call


Recursion
Tower of Hanoi

 Tower of Hanoi is a mathematical puzzle where we have three rods and n disks.
 The objective of the puzzle is to move the entire stack to another rod, obeying
the following simple rules:

1) Only one disk can be moved at a time.


2) Each move consists of taking the upper disk from one of the stacks and placing it
on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a
stack.
3) No disk may be placed on top of a smaller disk
Algorithm for Tower Of Hanoi

 Tower(n, Beg,Aux,End)
{
If (n==1) then Move the top disk from Beg to End
Else
Tower(n-1, Beg,End, Aux)
Tower(1, Beg,Aux,End)
Tower(n-1, Aux,Beg,End)
}
Queue

 It is a linear list which has two ends, one for insertion of elements and other for deletion
of elements.
 The first end is called ‘Rear’ and the later is called ‘Front’.
 Elements are inserted from the Rear end and are deleted from Front end.
 Queues are called First-In-First-Out(FIFO) List, since the first element in the queue will be
the first element out of the queue.
 The order in which the elements enter a queue is the order in which they leave.
 In short we can say that Queue is a homogeneous collection of elements in which new
elements are added at one end called rear, and the existing elements are deleted from
other end called front.
Representation of Queues

 Queues being the linear data structure, can be represented using both arrays and linked list.
Array Representation of Queues
Array is a data structure that stores fixed number of elements. One of the major limitations of an array is that its size should be fixed prior to using it. But
the size of the queue keeps on changing as he elements are either removed from the front end or added at the rear end. One of the solution to this
problem would be to declare an array with be to declare an array with a maximum size. Thus, if queue is implemented using array, we must be sure about
the exact number of elements we want to keep in the queue as we have to declare the size of the array at the design time.
The array representation in C is:
#define MAX 20
struct queue
{
int front;
int end;
int arr[MAX];
};
struct queue q;
Operations on Queue

 Only two basic operations can be performed on queue data structure. These are:
i. Insertion
ii. Deletion

Conditions
1. Overflow Condition: If REAR=MAX then queue is overflow.
2. Underflow Condition: If FRONT=o then queue is underflow.
3) One Element: If FRONT=REAR then queue containing only one element.
Algorithm for Insertion in Queue

i. If REAR=MAX the write “Queue is Overflow and stop”


ii. READ DATA
iii. If Front=0 then
a) FRONT <- FRONT + 1
b) REAR <-REAR + 1
c) Q(REAR) <- DATA
else REAR <- REAR + 1
Q(REAR) <-DATA
iv. STOP
Algorithm for Deletion from a Queue

i. If FRONT=0 THEN WRITE “Queue is UNDERFLOW & STOP”


ii. If FRONT=REAR then
Q(FRONT) <- NULL
FRONT<-0
Else
Q(FRONT) <- NULL
FRONT <- FRONT+1
iii. STOP
Circular Queue

1. In circular queues the element Q[0], Q[1], Q[2] … Q[n-1] is represented in a circular
fashion.
2. A circular queue is one in which the insertion of a new element is done at the very first
location of the queue if the last location at the queue is full.
3. After inserting an element at last location Q[5], the next element will be inserted at the
very first location that is circular queue is one in which the first element comes just after
the last element
How Circular Queue Works
 Circular Queue works by the process of circular increment i.e. when we try to increment
any variable and we reach the end of queue, we start from the beginning of queue by
modulo division with the queue size.
i.e if REAR + 1 == 5 (overflow!), REAR = (REAR + 1)%5 = 0 (start of
queue)
Operations on Circular Queue
 Enqueue
1) If (rear=max-1 && front=0) || if(rear=front-1)
Then display overflow and exit
2) Read data you want to insert
3) If front=-1 then
Set front=rear=0
Else if rear=max-1
then set rear=0
Else set rear=rear+1
4) Q[rear]=data
5) Stop
Operations on Circular Queue
 De-queue
1) If (front=-1)
Then display underflow and exit
2) If front=rear then
Set front=rear=-1
Else if front=max-1
then set front=0
Else
set front=front+1
4) Stop
Doubly Ended Queue

 Double ended queue is a more generalized form of queue data structure which allows
insertion and removal of elements from both the ends, i.e , front and back.
 However, no element can be added and deleted from the middle
Doubly Ended Queue

 There are two variants of a double-ended queue. They include:


 Input restricted deque: In this dequeue,insertions can be done only at one of the
ends,while deletions can be done from both ends.
 Output restricted deque: In this dequeue,deletions can be done only at one of
the ends,while insertions can be done on both ends.
Operation on Double ended Queue

 There are four basic operations in usage of Deque that we will explore:
 Insertion at rear end
 Insertion at front end
 Deletion at front end
 Deletion at rear end
Operation on Double ended Queue
 Algorithm for Insertion at rear end
 Step-1: [Check for overflow]
if(rear==MAX)
then Print Queue is Overflow and exit
Step-2: else
Enter data you want to insert and place it in no
Set rear=rear+1;
Set q[rear]=no;
[Set rear and front pointer]
if rear=-1
Set rear=0;
if front=-1
Set front=0;
Step-3: return
Operation on Double ended Queue
 Algorithm for Insertion at front end
 Step-1 : [Check for the front position]
if(front=-1)
Print Overflow condition and Cannot add item at the front and exit
Step-2 : [Insert at front]
else
Set front=front-1;
Set q[front]=no;
Step-3 : Return
Operation on Double ended Queue
 Algorithm for deletion from front end
Step-1 [ Check for front pointer]
if front=0
print(" Queue is Underflow”);
return;
Step-2 [Perform deletion]
else
no=q[front];
print(“Deleted element is”,no);
[Set front and rear pointer]
if front=rear
front=0;
rear=0;
else
front=front+1;
Step-3 : Return
Operation on Double ended Queue
 Algorithm for deletion from rear end
Step-1 : [Check for the rear pointer]
if rear=0
print(“Cannot delete value at rear end”);
return;
Step-2: [ perform deletion]
else
no=q[rear];
[Check for the front and rear pointer]
if front= rear
front=0;
rear=0;
else
rear=rear-1;
print(“Deleted element is”,no);
Step-3 : Return

You might also like