Professional Documents
Culture Documents
Objectives
1
Data Abstraction
The basic data types includes char , int , float & double.
Some data types may be modified by keywords short , long , unsigned
Data types:
2
Data Abstraction
Abstract Data Type(ADT):
It is a data type i.e, organised in such a way that the specification of the objects & the
specification of the operations on the objects is separated from the representation of the
objects & implementation of the operations.
3
Data Abstraction
1. Creator / constructor:
2. Transformers :
3. Observers / reporters:
These functions provide information about an instance of the type , but they
do not change the instance
4
PERFORMANCE ANALYSIS 1 of 4
When several algorithms can be designed for the solution of a problem, there arises the
need to determine which among them is the best. The efficiency of a program or an
algorithm is measured by computing –
• Time Complexity
• Space Complexity
5
Introduction to Linear & Non-linear
Data Structure 1 of 3
The data structure can be defined as the collection of elements and all the possible
operations which are required for those set of elements. In other words data structure will
tell us specific set of elements and corresponding set of operations.
A data structure can be defined as a way of organizing and storing data in a computer so
that it can used efficiently.
D= data structure
d= a set of variables (data objects)
f= a set of functions
a= set of rules to implement the functions.
6
Introduction to Linear & Non-linear
Data Structure 2 of 3
Data Structures:
A data structure is an arrangement of data in a computer's memory or even disk
storage.
8
Difference between linear and non linear
data structures
Linear DS Non Linear DS
data elements are organized a data element can be attached
sequentially and therefore they to several other data elements to
are easy to implement in the represent specific relationships
computer’s memory. that exist among them. Due to
this nonlinear structure, they
might be difficult to be
implemented in computer’s
linear memory compared to
implementing linear data
structures.
10
Difference between linear and non linear
data structures
Linear DS Non Linear DS
Syntax Syntax
Struct node Struct node
{ {
int data; int data;
struct node*link; struct node*left;
}; struct node*right;
};
11
SINGLY LINKED LIST 1 of 14
Definition of linked list
A linked list is a linear collection of homogeneous data elements, called nodes, where
linear order is maintained by means of links or pointers.
Data part of the link can be an integer, a character, a String or an object of any kind.
12
SINGLY LINKED LIST 2 of 14
node
NODE
link struct Node
{
int data;
struct Node *link;
};
data
LINKED LIST
Start Null
A B C D
13
SINGLY LINKED LIST 3 of 14
• Subsequent nodes are accessed via the link-pointer member stored in each node.
• Link pointer in the last node is set to null to mark the end of the list.
• Becomes full only when the system has insufficient memory to satisfy dynamic
storage allocation requests.
14
SINGLY LINKED LIST 4 of 14
15
SINGLY LINKED LIST 5 of 14
A singly linked list, or simply a linked list, is a linear collection of data items. The linear
order is given by means of POINTERS. These types of lists are often referred to as
linear linked list.
• The last node in the list contains NULL pointer to indicate that it is the end of the list.
16
SINGLY LINKED LIST 6 of 14
OPERATIONS
• Creation of a node
• Insertions
• Deletions
• Traversing the list
struct node
{
int data;
struct node *next;
};
17
SINGLY LINKED LIST 7 of 14
CREATION OF A NODE:
Read cur->data
cur->link=NULL
18
SINGLY LINKED LIST 8 of 14
Algorithm for creating a node
How do we place elements in the list: Usually, there are 4 cases we can use:
• at the beginning
20
SINGLY LINKED LIST 10 of 14
Case 1 : at the beginning Pseudo code:
1. Inserting at beginning of list.
2. if (first! = NULL)
3. {
4. Cur=(struct node*) malloc
(sizeof(struct node))
5. Read cur->data
6. Cur->lnk=first
7. First=cur
8. }
21
SINGLY LINKED LIST 11 of 14
Case 2 : end of the list Pseudo code:
1. Inserting at end of list.
2. if (first! = NULL)
3. {
4. Cur=(struct node*) malloc
(sizeof(struct node))
5. Read cur->data
6. Next=first
7. While(next!=NULL)
8. {
1. Prev=next;
2. Next=next->link;
}
Cur->link=prev->link;
9. prev->link=cur
10. }
22
SINGLY LINKED LIST 12 of 14
Case 3 : after a given element Pseudo code:
1. Inserting at end of list.
2. if (first! = NULL)
3. {
4. Cur=(struct node*) malloc
(sizeof(struct node))
5. Read cur->data
6. Next=first
7. while(next!=NULL)
8. {
9. if(next->data = = key)
10. break;
11. else
12. next=next->link;
13. }
14. cur -> link = next->link;
15. next->link = cur;
16. }
23
SINGLY LINKED LIST 12 of 14
Case 4 : Before a given element
Pseudo code:
1. Inserting at end of list.
2. if (first! = NULL)
3. {
4. Cur=(struct node*) malloc
(sizeof(struct node))
5. Read cur->data
6. Next=first
7. while(next!=NULL)
8. {
9. if(next->data = = key)
10. break;
11. else
1. { prev=next
12. next=next->link; }
13. }
14. cur -> link = prev->link;
15. prev->link = cur;
16. }
24
SINGLY LINKED LIST 12 of 14
DELETION: Removing an element from the list, without destroying the integrity of the list
itself.
When we delete a node we logically remove the node from the list by changing links and
physically remove it from heap
25
SINGLY LINKED LIST 13 of 14
DELETION at the beginning of list:
Pseudo code:
1. If (first==NULL)
2. Print list is empty
3. else if(first->link==NULL)
4. Deleted element first->data
5. first-=NULL
6. Else
7. {
8. cur=first
9. First=cur->link
10. Deleted element cur->data
11. Free(cur)
12. Cur=NULL
26
SINGLY LINKED LIST 13 of 14
DELETION at the end of list:
Pseudo code:
1. If (first==NULL)
2. Print list is empty
3. Else
4. {
5. cur=first
6. While(cur->link!=NULL)
7. Prev=cur
8. Deleted element is cur-.>data
9. Free(cur)
10. Prev->link=NULL
27
SINGLY LINKED LIST 13 of 14
DELETION at a given position other than first or last Pseudo code:
1. If (first==NULL)
2. Print list is empty
3. Else
4. {
5. Read position of
deletion
6. next=first
7. While(c<pos) {
8. Cur=next
9. Next=next->link
10. C++
11. }
12. Cur->link=next->link
13. Deleted element is
next-.>data
14. Free(next)
15. next->link=NULL
28
SINGLY LINKED LIST 14 of 14
TRAVERSAL:
Pseudo code:
1. temp = first;
2. while(temp!=NULL)
3. {
4. print temp->data;
5. temp = temp->link;
6. }
29
C program example1 , also show error simulation
SINGLY LINKED LIST 14 of 14
Concatenation of two single linked lists:
struct node *concat( struct node *start1,struct node *start2)
{
struct node *ptr;
if(start1==NULL)
{
start1=start2;
return start1;
}
if(start2==NULL)
return start1;
ptr=start1;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=start2;
return start1;
}
30
CIRCULARLY LINKED LIST
Insertion at the front of Circular linked list
Procedure for insertion a node at the beginning of list
Step1. Create the new node
Step2. Set the new node’s next to itself (circular!)
Step3. If the list is empty, return new node.
Step4. Set our new node’s next to the front.
Step5. Set tail’s next to our new node.
Step6. Return the end of the list.
31
CIRCULARLY LINKED LIST
Insertion in the middle of the Circular linked list
32
CIRCULARLY LINKED LIST
Insertion at the end of Circular linked list
Procedure for insertion a node at the end of list
Step1. Create the new node
Step2. Set the new node’s next to itself (circular!)
Step3. If the list is empty,return new node.
Step4. Set our new node’s next to the front.
Step5. Set tail’s next to our new node.
Step6. Return the end of the list.
33
CIRCULARLY LINKED LIST
Deletion In Circular linked list
There are three situation for Deleting element in list.
1.Deletion at beginning of the Circular linked list.
2.Deletion at the middle of the Circular linked list.
3.Deletion at the end of the Circular linked list.
34
CIRCULARLY LINKED LIST
1.Deletion at beginning of the Circular linked list.
After Deletion
35
CIRCULARLY LINKED LIST
Deletion at beginning in Circular linked list
Pseudo code:
node=start->next;
ptr=start;
if(i==0)
{
printf("\n List is empty");
exit(0);
}
ptr->next=node->next;
free(node);
36
CIRCULARLY LINKED LIST
Deletion at the middle of the Circular linked list
After Deletion
37
CIRCULARLY LINKED LIST
Deletion at location in Circular linked list
Pseudo code:
if(node_no==delete_no) {
ptr->next=node->next;
free(node);
flag=1;
break;
}
else {
ptr=ptr->next;
node=node->next;
}
node_no++;
count--; }
if(flag==0) {
printf("\n Position not found");
38
CIRCULARLY LINKED LIST
Deletion at the end of the Circular linked list
After Deletion
39
CIRCULARLY LINKED LIST
node=start->next;
ptr=start; count=i;
if(i==0) {
printf("\n List is empty"); exit(0);
}
while(count) { node_no++;
ptr=ptr->next; node=node->next;
count--; } node=start->next;
ptr=start;
while(node_no!=1) {
node_no--;
ptr=ptr->next;
node=node->next;
} if(node_no==1) { ptr->next=node-
>next; free(node);}
40
C program example: 2 , also show error simulation
CIRCULARLY LINKED LIST
A circularly linked list, or simply circular list, is a linked list in which the last
node is always points to the first node. This type of list can be build just by
replacing the NULL pointer at the end of the list with a pointer which points to the
first node. There is no first or last node in the circular list.
Advantages:
Any node can be traversed starting from any other node in the list.
There is no need of NULL pointer to signal the end of the list and hence, all
pointers contain valid addresses.
In contrast to singly linked list, deletion operation in circular list is simplified as
the search for the previous node of an element to be deleted can be started from
that item itself.
41
DOUBLY LINKED LIST
In a singly linked list one can move from the header node to any node in one
direction only (left-right).
A doubly linked list is a two-way list because one can move in either direction
i.e., either from left to right or from right to left.
42
DOUBLY LINKED LIST
struct node
{
int data;
struct node *next; // Pointer to next node in DLL
struct node *prev; // Pointer to previous node in DLL
};
43
DOUBLY LINKED LIST
Advantages over singly linked list
2) The delete operation in DLL is more efficient if pointer to the node to be deleted is
given.
In singly linked list, to delete a node, pointer to the previous node is needed. To get this
previous node, sometimes the list is traversed. In DLL, we can get the previous node using
previous pointer.
1) Every node of DLL Require extra space for an previous pointer. It is possible to
implement DLL with single pointer though.
45
DOUBLY LINKED LIST
Creating a New Node
Pseudo code:
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
if(*start==NULL) {
ptr->prev = ptr->next = NULL ;
*start = *end = ptr ;
}
else {
ptr->prev = *end;
(*end)->next = ptr ;
ptr->next= NULL;
(*end)=ptr;
}
46
DOUBLY LINKED LIST
Insertion in doubly linked list
There are three situation for inserting element in list.
1.Insertion at the front of list.
2.Insertion in the middle of the list.
3.Insertion at the end of the list.
47
DOUBLY LINKED LIST
Insert at front of the list Algorithm
Pseudo code: InsertAtFrontDll(info,prev,next,start,end
)
ptr=(node*)malloc(sizeof(node)); 1.create a new node and address in
ptr->info=item; assigned to ptr.
if(*start==NULL) 2.check[overflow] if(ptr=NULL)
{ write:overflow and exit
*start=ptr; 3.set Info[ptr]=item;
*end=ptr; 4.if(start=NULL)
} set prev[ptr] = next[ptr] = NULL
else set start = end = ptr
{ else
ptr->prev = NULL; set prev[ptr] = NULL
ptr->next=*start; next[ptr] = start
(*start)->prev=ptr; set prev[start] = ptr
*start=ptr; set start = ptr [end if]
} 5.Exit.
48
DOUBLY LINKED LIST
2.Insertion in the middle of the list
49
DOUBLY LINKED LIST
2.Insertion in the middle of the list
Pseudo code:
ptr=(node*)malloc(sizeof(node));
ptr->info=item;
loc = *start ;
if(*start==NULL) {
ptr->prev = ptr->next = NULL ;
*start = *end = ptr ; }
else if(i<=size) {
while(n != i) {
loc=loc->next;
n++;
}
ptr->next = loc->next ;
ptr->prev = loc ;
(loc->next)->prev = ptr ;
loc->next = ptr ;
}
else{
ptr->prev = *end;
(*end)->next = ptr ;
ptr->next= NULL;
(*end)=ptr; }}
50
DOUBLY LINKED LIST
3.Insertion at the end of the list
51
DOUBLY LINKED LIST
• Linear list.
• One end is called top.
• Other end is called bottom.
• Additions to and removals from the top end
only.
53
STACK ADT
A stack is a Last In First Out (LIFO) data structure in which all insertions and
deletions are takes place at one end, called the top.
Stack maintains a variable called top, which keeps track of the top most element in
the stack.
In the stack, the elements are removed in the reverse order of that in which they were
added to the stack that is last element inserted is to be deleted first.
54 54
STACK ADT
Example 1:
When the elements are inserted in the order as A,B,C,D then the size
of the stack is 4 and the top most element in the stack is D.
When deletion operation is performed on stack continuously then the order in which
the elements are deleted is D,C,B,A.
55 55
STACKS
Standard operations:
IsEmpty … return true iff stack is empty
IsFull … return true iff stack has no remaining capacity
Top … return top element of stack
Push … add an element to the top of the stack
Pop … delete the top element of the stack
56
STACKS
ADT Stack is
Object: a finite ordered list with 0 / more elements.
Functions:
for all stack belong Stack, item belong element, maxStackSize belongs
positive integer.
Stack Create(maxStackSize)::= create an empty stack whose maximum
size is maxStackSize
Boolean IsFull(stack ,maxStackSize)::=
if (number of elements in stack == maxStazkSize) return TRUE else
return FALSE
Stack Push(stack) ::=
if (IsFull(stack)) stackFull else insert item into top of stack and return
Boolean IsEmpty(stack) ::=
if (stack == CreateS(maxStackSize)) return TRUE else return FALSE
Element Pop(stack) ::=
if (IsEmpty(stack)) return else remove and return the element at the top of
the stack 57
STACK ADT
IMPLEMENTATION
Stack can be implemented in two ways:
1. using arrays
2. using linked list
58 58
STACK ADT USING ARRAYS
STACK IMPLEMENTATION USING ARRAYS
Procedure:
Initialize the stack by assigning -1 to the top variable to indicate that the array based
stack is empty.
A top is an integer value, which contains the array index for the top of the stack.
59 59
STACK ADT USING ARRAYS
Algorithm for inserting an element into the stack
push()
1. if top>=size then
print “stack is full”
2. else
read item
top=top+1
stack[top]=item
endif
3. stop
Overflow occurs when we are trying to insert an item into a stack which is full.
If the top value reaches to maximum size of the stack then items cannot
be inserted into the stack i.e. stack is full.
61
STACK ADT USING ARRAYS
Algorithm for deleting an element from the stack
pop()
1. if top = -1 then
print “stack is empty”
2. else
1. item = stack[top]
2. top=top-1
3.endif
4.stop
Takeout the element from the location where the top is pointing and
store it in the variable then decrement top by one.
62 62
STACK ADT USING ARRAYS
POP Functon
void pop()
{
if(top==-1)
printf("\n stack is empty(underflow)\n");
else
printf("the pop element is:%d",stack[top]);
top--;
}
63
STACK ADT USING ARRAYS
Algorithm for displaying an elements of the stack
traversal() void display()
{
1. if top = -1 if(top==-1)
1.print “stack empty” {
2. else
1. for(i = top; i >= 0;i--) printf("\n stack is empty(underflow)");
1.print “stack[i]” return
3.endif }
4. stop printf("\n the elements in stack are:\n");
for(i=top;i>=0;i--)
printf("->%d",stack[i]);
}
If the top value is -1 then the stack is empty.
If the stack is not empty, assign a top value to variable i, display the item
which is pointed at stack[i] , decrement the top value by one and repeat this
process until top becomes zero.
65 65
STACK ADT USING LINKED LIST
Example:
The following list consists of five cells, each of which holds a data object
and a link to another cell.
66 66
STACK ADT USING LINKED LIST
Push function POP function
void push() void pop()
{ {
struct node*temp; struct node*temp;
temp=(struct node*)malloc(sizeof(struct if(top==NULL)
node)); printf("\n stack is empty \n");
printf("\n enter data of item:"); else
scanf("%d",&temp->data); {
temp->link=top; temp=top;
top=temp; printf("popped item is %d \
} n",temp->data);
top=top->link;
free(temp);
}
}
67 67
STACK ADT USING LINKED LIST
Traversal function
void display()
{
struct node*temp;
temp=top;
if(top==NULL)
printf("stack is empty \n");
else
{
printf("stack elements:\n");
while(temp!=NULL)
{
printf("->%d",temp->data);
temp=temp->link;
}
}
}
C program example2 , also show error simulation
68
STACK APPLICATIONS
APPLICATIONS
69 69
STACK APPLICATIONS
TYPES OF EXPRESSIONS
70 70
STACK APPLICATIONS
CONVERSIONS
71 71
STACK APPLICATIONS
Algorithm to convert infix expression to Postfix:
1.Initialize an empty stack.
2.Repeat the following process until the end of the infix expression.
Get next input token (constant, variable, arithmetic operator, left
parenthesis, right parenthesis) in the infix expression.
If the token is
A left parenthesis: Push it onto the stack.
A right parenthesis:
Pop and display stack elements until a left parenthesis is on the top of
the stack.
Pop the left parenthesis also, but do not display it.
An operator:
While the stack is nonempty and token has higher or equal priority than
the stack’s top element, pop and display.
Push token onto the stack.
An operand: Display it.
3.When the infix expression is reached to the end, pop and display stack items
until the stack is empty.
(Note: Left parenthesis in the stack has lowest priority)
72 72
STACK APPLICATIONS
73 73
STACK ADT
1. Algorithm for postfix conversion:
void postfix(char inf[15])
Step 1: repeat the steps a to c until inf[i]!='\0'
a. check if infix expression is opening brace push it onto stack
check if(inf[i]='(‘) push(inf[i])
b. check if infix expression is alphabetic character add it to postfix express
if(isalpha(inf[i])) pf[j++]=inf[i]
c. check if infix expression is closing brace
if(inf[i]==')')
repeat 1 to 2 until stack[top]!=opening brace
1.pf[j++]=pop()
2.x=pop()
Otherwise
repeat 1 to 2 until prec(stack[top])>=prec(inf[i])
1.pf[j++]=pop()
2.push(inf[i])
Step 2: repeat the steps a to b until top!=-1
a.pf[j++]=pop()
b.pf[j]='\0'
step 3: stop
74 74
STACK ADT
2. Algorithm for push operation on stack:
Step 1:increment top by 1
Step 2: stack[++top]=y
Step 3:stop
3. Algorithm for pop operation on stack:
Step 1:return stack[top]
Step 2: decrement top by 1
Step 3:stop
75 75
STACK ADT
76 76
STACK APPLICATIONS
Postfix conversion function
int prec(char c)
{
if(c=='*' || c=='/')
return(5);
else if(c=='+' || c=='-')
return(3);
else if(c=='(')
return(1);
}//end of prec()
79 79
STACK APPLICATIONS
Applications of Stacks
• Direct applications
Page-visited history in a Web browser
Undo sequence in a text editor
Chain of method calls in the Java Virtual Machine
• Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
82 82
STACK APPLICATIONS
Demonstration of Manual infix to postfix conversion
83
STACK APPLICATIONS
Infix Transformations
A + B * C – D / E Converts to A B C * + D E / -
Above transformation is illustrated in following figure.
85
STACK APPLICATIONS
Evaluation of Postfix Expression
For example given expression is A B C + *
And assuming that A is 2, B is 4, and C is 6 ,
the following figure shows the evaluation of postfix expression.
87 87
Queues
ADT Queue is
Object: a finite ordered list with 0 / more elements.
Functions:
for all queue belong Queue, item belong element, maxQueueSize belongs positive
integer.
Queue Create(maxQueueSize)::= create an empty queue whose maximum size is
maxQueueSize
Boolean IsFull(queue ,maxQueueSize)::=
if (number of elements in queue == maxQueueSize) return TRUE else return
FALSE
Queue AddQ(queue , item) ::=
if (IsFullQ(queue)) queueFull else insert item at rear of queue and return queue
Boolean IsEmptyQ(queue) ::=
if (queue == CreateQ(maxQueueSize)) return TRUE else return FALSE
Element DeleteQ(queue) ::=
if (IsEmptyQ(queue)) return else remove and return the item at front of queue
88 88
Queues
• Linear list.
• One end is called front.
• Other end is called rear.
• Additions are done at the rear only.
• Removals are made from the front only.
89
Bus Stop Queue
Bus
Stop
90
Bus Stop Queue
Bus
Stop
91
Bus Stop Queue
Bus
Stop
front rear
rear
92
Bus Stop Queue
Bus
Stop
front rear
rear
93
Revisit Of Stack Applications
• Applications in which the stack cannot be
replaced with a queue.
1. Parentheses matching.
2. Towers of Hanoi.
3. Switchbox routing.
4. Method invocation and return.
5. Try-catch-throw implementation.
Objectives:
Queue Operations
Queue Linked List Design
Queue Functions
Queue Demonstration
95 95
Queues
Queue operations:
•
Data can be inserted at the rare in queue
•
Data can be deleted at front in queue
96 96
QUEUE ADT
IMPLEMENTATION
Queue can be implemented in two ways:
1. using arrays
2. using linked list
97 97
Queues using arrays
• We can also implement queue using arrays.
• There are mainly two functions in implementation of queue:
1) enqueue
2) dequeue
Enqueue
• An enqueue to an empty queue is placed in the first element of the array.
which becomes both front and rear.
• Subsequent enqueues are placed at the array locations following rear i.e if the
last element is stored at array location 11,the data for next enqueue is placed
in element 12.
98 98
Queues using arrays
Dequeue
• A dequeue takes place at the front of the queue.
• As an element is deleted from the queue, the queue front is advanced to the next
location
• Fig-8 shows about physical structure of queue
Queue ary
count Max size front rare
7 17 5 11
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
99 99
QUEUE USING ARRAY
1.if(front==-1)
2. print queue is empty / underflow
3. return;
4. print the element in queue are
5. for(i=front;i<=rear;i++)
6. print arr[i]
102 102
Queues using arrays
Disavantages:
• In a queue, we delete elements at front and insert elements at rear.
• when rear reaches maximum size of an array, we can’t Insert new element though we
have space at front end of a queue.
• Therefore queue is not full because there are empty elements at the beginning of the
array. It shows queue is full.
• Fig shows array queue with last elements filled
Queue Queue
front rare
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[12]
104 104
Queues Linked List
Dequeue
• If the queue contains data, then set the front pointer to the next
item in the queue
107 107
QUEUE USING LINKED LIST
Enqueue function
Dequeue function
1. struct node*temp;
2. if(front==NULL||rear==NULL)
3. print queue underflow
4. else
5. temp=front;
6. print deleted element temp->data
7. front=front->link
8. free(temp)
109 109
QUEUE USING LINKED LIST
Display function
110 110
Queues Linked List
111 111
Circular Queues
• This disadvantage is overcome by shifting all the elements from end to the
beginning of the array
• In the above example, we shift from element 5 to 0 and 6 to 1 and so on.
• A more efficient alternative is found in circular queue
• In a circular queue, the last element is logically followed by the first element
112 112
Circular Queues
Circular queue can be implemented in two ways
113
Circular Queues
Circular queue using arrays
[2] [3]
Initially circular queue is
empty
[0] [5]
114
Circular Queues
115
Circular Queues
116
Circular Queues
117
Circular Queues
Circular queue using arrays
118
Circular Queues
119
Circular Queues
Circular queue using arrays
121
Circular Queue
122 122
Circular Queue
if(front == -1)
printf("\n\nCIRCULAR QUEUE IS UNDERFLOW");
else
{
a=cqueue[front];
if(front==rear)
front=rear=-1;
else
front = (front+1)%MAXSIZE;
printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",a);
}
123 123
Circular Queue
if(front == -1)
printf("\n\nCIRCULAR QUEUE IS UNDERFLOW");
else
{
for(i=front;i<=rear;i++)
{
printf(“ %d \t“,cqueue[i]);
}
}
124
Circular Queue
128 128
DEQUEUE
129
DEQUEUE
TYPES OF DEQUE
Input restricted Deque
• Elements can be inserted only at one end.
• Elements can be removed from both the ends.
130
DEQUEUE
As Queue
• When items are inserted at one end and removed at the other
end.
131
DEQUEUE
Dequeue is also called as double ended queue and it allows user to perform
insertion and deletion from the front and rear position. And it can be easily
implemented using doubly linked list. On creating dequeue, we need to add two
special nodes at the ends of the doubly linked list(head and tail). And these two
nodes needs to be linked between each other(initially).
head tail
--------------------------- --------------------------------
| | | ---|---------->| | | |
| NULL | 0 | | | | 0 | NULL |
| | | |<----------|--- | | |
---------------------------- -------------------------------
132 132
DEQUEUE
So, the header node goes before the first node of the queue and the trailer node
goes after the last node in the queue. To do insertion at the front position, place
the new node next to the head. And to do insertion at the rear position, place the
new node before the tail. Similarly, dequeue operation can also be performed at
the front and rear positions.
Operations
Insert a New Element From front
Insert a New Element From rear
Delete an Element From front
Delete an Element From rear
133 133
DEQUEUE using Array
1. Algorithm for insertion at rear end:
Step 1: check if(front==-1)
front++
rear++
read the element to insert a[rear]
count++;
Step 2: check if(rear>=SIZE-1)
print Insertion is not possible, overflow!!!
return
Step 3: Increment the rear by 1
read the element to insert a[rear]
count++;
Step 4:stop
134 134
DEQUEUE using Array
2. Algorithm for insertion at front end:
Step 1: check if(front==-1)
front++
rear++
read the element to insert a[rear]
count++
Step 2: check if(rear>=SIZE-1)
print Insertion is not possible, overflow!!!
return
Step 3: Repeat the steps a to d until i>0
a[i]=a[i-1]
read the element to insert a[i]
count++
rear++;
Step 4:stop
135 135
DEQUEUE using Array
3. Algorithm for deletion at rear end:
Step 1: check if (front==-1)
Print Deletion is not possible: Dequeue is empty
return
Step 2: otherwise
Print The deleted element is a[rear]
Check if(front==rear)
front=rear=-1
otherwise
rear=rear-1
Step 3:stop
136 136
DEQUEUE using Array
Algorithm for deletion at front end:
1: check if (front==-1)
Print Deletion is not possible: Dequeue is empty
return
Step 2: otherwise
Print The deleted element is a[front]
Check if(front==rear)
front=rear=-1
otherwise
front=front-1
Step 3:stop
5.Algorithm for displaying the dequeue:
Step 1: check if (front==-1)
Print Dequeue is empty
return
Step 2: otherwise
Repeat step a until i<=rear (i=front)
Print a[i] 137 137
Step 3:stop
DEQUEUE using Linked list
ALGORITHM:
Initialize the front and rear nodes with NULL values
struct node *front=NULL,*rear=NULL,*cur,*temp
1. Algorithm for insertion at rear end:
Step 1: Create a new node
cur=(struct node*) malloc (sizeof (struct node))
Step 2: Read the content of node (cur->data)
Step 3: Assign new node right link to NULL
cur->right=NULL
Step 4: Assign new node to front & rear node
Check if(rear==NULL&&front==NULL)
rear=front=cur
Step 5: otherwise
rear->right=cur
cur->left=rear
rear=cur
Step 6:stop
138 138
DEQUEUE using Linked list
2. Algorithm for insertion at front end:
Step 1: Create a new node
cur=(struct node*) malloc (sizeof (struct node))
Step 2: Read the content of node (cur->data)
Step 3: Assign new node right link to NULL
cur->right=NULL
Step 4: Assign new node to front & rear node
Check if(rear==NULL&&front==NULL)
rear=front=cur
Step 5: otherwise
cur->right=front
front->left=cur
front=cur
Step 6:stop
139 139
DEQUEUE using Linked list
3. Algorithm for deletion at front end:
Step 1: check if(front==NULL)
Print deQueue is empty
Step 2: otherwise
Check if(front==rear)
Print Deleted data is front->data
front=rear=NULL
Step 3: otherwise
temp=front;
print Deleted data is temp->data
front=front->right
front->left=NULL
free(temp)
Step 4: stop
140 140
DEQUEUE using Linked list
4. Algorithm for deletion at rear end:
Step 1: check if(front==NULL)
Print deQueue is empty
Step 2: otherwise
Check if(front==rear)
Print Deleted data is rear->data
front=rear=NULL
Step 3: otherwise
temp=rear;
print Deleted data is temp->data
if(front==rear)
front=rear=NULL
rear=rear->left
rear->right=NULL
free(temp)
Step 4: stop
141 141
DEQUEUE using Linked list
5.Algorithm for displaying the dequeue:
Step 1: check if(front==NULL)
Print deQueue is empty
Step 2: otherwise
temp=front;
repeat steps a-b until temp!= NULL
a. printf("<-%d ->",temp->data)
b. temp=temp->right;
Step 3: stop
142 142
APPLICATIONS OF DEQUE
Palindrome-checker
143
APPLICATIONS OF DEQUE
144
APPLICATIONS OF DEQUE
145
End
146