SlideShare a Scribd company logo
1 of 60
Data Structures
1 Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Dr. G. Jasmine Beulah
Dept. Computer Science,
Kristu Jayanti College(UG)
Bengaluru
Stack
Stack - Definition; Array representation of stack; Operations on
stack; Polish notation; Reverse Polish notation; Applications of
stack, Conversion of an infix arithmetic expression to postfix,
Evaluation of postfix expression
2 Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Today’s discussion…
3
*Stack
*Basic principles
*Operation of stack
*Stack using Array
*Applications of stack
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Stack
4 Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Basic Idea
• A stack is an Abstract Data Type (ADT), commonly used in most
programming languages. It is named stack as it behaves like a real-
world stack, for example – a deck of cards or a pile of plates, etc.
5 Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Stack Representation
• Can be implemented by means of Array, Structure, Pointers and
Linked List.
• Stack can either be a fixed size or dynamic.
6 Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
7
STACK
push
create
pop
isfull
isempty
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Operations
STACK: Last-In-First-Out (LIFO)
*An abstract data type (ADT) is an abstraction of a data structure
*An ADT specifies:
*Data stored
*Operations on the data
8
Assumption: stack contains integer elements!
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Objects:
* A finite sequence of nodes
Operations:
*Create
*push(): Insert element at top on the stack
*peek(): Return top element without removing it
*pop(): Remove and return top element
*isEmpty(): check if stack is empty
*isfull() : test if stack is full
*Stack ADT
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES 9
Stack using Array
10 Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
11
top
top
PUSH
Push using Stack
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
12
top
top
POP
Pop using Stack
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
• In the array implementation, we would:
• Declare an array of fixed size (which determines the maximum
size of the stack).
• Keep a variable which always points to the “top” of the stack.
• Contains the array index of the “top” element.
13
Basic Idea
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Declaration & Stack Creation
14
#define MAXSIZE 10
int stack[MAXSIZE];
int top =-1;
ARRAY
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Checking for Stack Full
15
int isfull()
{
if(top == MAXSIZE)
return 1;
else
return 0;
}
ARRAY
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Checking for stack empty
16
int isempty ()
{
if (top == -1)
return 1;
else
return (0);
}
ARRAY
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Pushing an element into stack
17
int push(int data)
{
if(!isfull())
{
top = top + 1;
stack[top] = data;
}
else
{
printf("Could not insert
data, Stack is full.n");
}
}
ARRAY
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Popping an element from stack
18
int pop()
{
int data;
if(!isempty())
{
data = stack[top];
top = top - 1;
return data;
}
else
{
printf("Could not retrieve data,
Stack is empty.n");
}
}
ARRAY
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Example: A Stack using an Array
19
#include <stdio.h>
#define MAXSIZE 100
struct lifo
{
int st[MAXSIZE];
int top;
};
typedef struct lifo stack;
main() {
stack A, B;
create(&A);
create(&B);
push(&A,10);
push(&A,20);
push(&A,30);
push(&B,100);
push(&B,5);
printf (“%d %d”, pop(&A), pop(&B));
push (&A, pop(&B));
if (isempty(&B))
printf (“n B is empty”);
return;
}
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Applications of Stacks
• Direct applications:
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Saving local variables when one function calls another, and this one
calls another
20 Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES 21
*
Notation
*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 −
Infix Notation
Prefix (Polish) Notation
Postfix (Reverse-Polish) Notation
BCA204A21: DATA STRUCTURES
22 Dr.G.Jasmine Beulah
Infix Notation
*In an Infix Notation, operators are used in-between operands.
*e.g. a - b + c, where operators are used in-between operands.
* Humans can easily read, write, and speak in infix notation but the same
does not go well with computing devices.
BCA204A21: DATA STRUCTURES
23 Dr.G.Jasmine Beulah
Prefix Notation
*In the Prefix 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
BCA204A21: DATA STRUCTURES
24 Dr.G.Jasmine Beulah
Postfix Notation
*Postfix notation is also known as Reversed Polish Notation
*In the Postfix notation style, the operator is post-fixed to the operands
i.e., the operator is written after the operands.
*For example, ab+
*This is equivalent to its infix notation a + b.
BCA204A21: DATA STRUCTURES
25 Dr.G.Jasmine Beulah
A simple example to show the difference of
the three Notation
BCA204A21: DATA STRUCTURES
26 Dr.G.Jasmine Beulah
Infix and Postfix Notations
• Infix: operators placed between operands:
A+B*C
• Postfix: operands appear before their operators:-
ABC*+
• There are no precedence rules to learn in postfix notation, and
parentheses are never needed
BCA204A21: DATA STRUCTURES
27 Dr.G.Jasmine Beulah
Infix Postfix
A + B A B +
A + B * C A B C * +
(A + B) * C A B + C *
A + B * C + D A B C * + D +
(A + B) * (C + D) A B + C D + *
A * B + C * D A B * C D * +
A + B * C  (A + (B * C))  (A + (B C *) )  A B C * +
A + B * C + D  ((A + (B * C)) + D )  ((A + (B C*) )+ D) 
((A B C *+) + D)  A B C * + D +
28
BCA204A21: DATA STRUCTURES
Infix to Postfix
Dr.G.Jasmine Beulah
Infix to postfix conversion
• Use a stack for processing operators (push and pop operations).
• Scan the sequence of operators and operands from left to right and
perform one of the following:
• output the operand,
• push an operator of higher precedence,
• pop an operator and output, till the stack top contains operator of a lower
precedence and push the present operator.
BCA204A21: DATA STRUCTURES 29 Dr.G.Jasmine Beulah
The Algorithm Steps…
1. Print operands as they arrive.
2. If the stack is empty or contains a left parenthesis on top, push the incoming operator
onto the stack.
3. If the incoming symbol is a left parenthesis, push it on the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print the operators
until you see a left parenthesis. Discard the pair of parentheses.
5. If the incoming symbol has higher precedence than the top of the stack, push it on the
stack.
6. If the incoming symbol has equal precedence with the top of the stack, use association.
If the association is left to right, pop and print the top of the stack and then push the
incoming operator. If the association is right to left, push the incoming operator.
7. If the incoming symbol has lower precedence than the symbol on the top of the stack,
pop the stack and print the top operator. Then test the incoming operator against the
new top of stack.
8. At the end of the expression, pop and print all operators on the stack. (No parentheses
should remain.)
BCA204A21: DATA STRUCTURES 30 Dr.G.Jasmine Beulah
Infix to Postfix Conversion
Requires operator precedence information
Operands:
Add to postfix expression.
Close parenthesis:
pop stack symbols until an open parenthesis appears.
Operators:
Pop all stack symbols until a symbol of lower precedence appears. Then push
the operator.
End of input:
Pop all remaining stack symbols and add to the expression.
BCA204A21: DATA STRUCTURES 31 Dr.G.Jasmine Beulah
Current
symbol
Operator
Stack
Postfix string
1 A A
2 * * A
3 ( * ( A
4 B * ( A B
5 + * ( + A B
6 C * ( + A B C
7 * * ( + * A B C
8 D * ( + * A B C D
9 ) * A B C D * +
10 + + A B C D * + *
11 E + A B C D * + * E
12 A B C D * + * E +
Expression:
A * (B + C * D) + E
becomes
A B C D * + * E +
Postfix notation
is also called as
Reverse Polish
Notation (RPN)
32
BCA204A21: DATA STRUCTURES
Infix to Postfix Rules
Dr.G.Jasmine Beulah
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES 33
Evaluation of Postfix Expression
Algorithm:
1. Scan the postfix expression from left to right.
2. If we encounter an operand, push it on to stack.
3. If we encounter an operator, pop two operands
from the stack.
The first operand popped is an operand 2 and second
popped is operand 1.
4. Perform the desired operation on the operands.
i.e. operand 1 operator operand 2
5. Push the result on to the stack.
6. Repeat the above procedure till the end of inputs
encountered.
Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES 34
Queue :
BCA204A21: DATA STRUCTURES 35 Dr.G.Jasmine Beulah
Queue
Basic Idea(FIFO)
• Queue is an abstract data structure, somewhat similar to Stacks. Unlike
stacks, a queue is open at both its ends. One end is always used to
insert data (enqueue) and the other is used to remove data (dequeue).
BCA204A21: DATA STRUCTURES 36 Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES 37 Dr.G.Jasmine Beulah
1. 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.
2. Queue is referred to be as First In First Out list.
3. For example, people waiting in line for a rail ticket form a queue.
Queue (FIFO)
Queue Representation
• As in stacks, a queue can also be implemented using Arrays, Linked-
lists, Pointers and Structures.
BCA204A21: DATA STRUCTURES 38 Dr.G.Jasmine Beulah
BCA204A21: DATA STRUCTURES 39 Dr.G.Jasmine Beulah
QUEUE
enqueue
create
dequeue
size
isempty
Operations of Queue
BCA204A21: DATA STRUCTURES 40 Dr.G.Jasmine Beulah
Array representation of Queue
 Represent queue by using Linear Arrays.
 There are two variables i.e. front and rear, that are implemented in the case of every
queue.
 Front and rear variables point to the position from where insertions and deletions are
performed in a queue.
 Initially, the value of front and queue is -1 which represents an empty queue.
Initial:
int front =-1
int rear = -1;
BCA204A21: DATA STRUCTURES 41 Dr.G.Jasmine Beulah
Queue Insertion
Points to remember when we add an element in the queue:
 Check whether the queue has any space to add an element into it.
 If the queue has spaces then add the element into the queue by incrementing the REAR
pointer.
 Example: Increment the REAR as REAR+1 or REAR++
 Queue [Rear] = Item which is used to add an element into the queue
 Note: Rear is the position where the item is to be inserted.
void qinsert()
{
int item;
if(rear==max-1)
printf("Queue is full");
else
{
printf("Enter the value to
insertn");
scanf("%d",&item);
rear++;
queue[rear]=item;
} }
BCA204A21: DATA STRUCTURES 42 Dr.G.Jasmine Beulah
Queue Overflow
 Insertion of an element in a queue is possible only if there is room in the array to fit
in elements.
 If the maximum size of the array is reached and if an attempt to insert an element
into the queue that is full, is tried, the condition that occurs is called ‘queue
overflow’.
 A function to check whether queue is full or not returns 1 when the queue is full and
returns 0 when the queue is not full.
 This function is used to check for queue overflow.
 A queue is full when REAR reaches the maximum size of the array used for the
queue i.e. REAR=max-1. FRONT = 0 and REAR = n-1 indicates this condition.
int qfull( )
{
if(rear = = max-1)
return 1;
else
return 0;
}
BCA204A21: DATA STRUCTURES 43 Dr.G.Jasmine Beulah
Deletion of element from the queue:
Points to remember when we remove an element from the queue:
 Check whether the queue has any element to be deleted.
 If so, delete the element from the queue by incrementing the FRONT
pointer. Example: Item= queue[rear]
 Increment the front using front++
 Note: Rear is the position where the item is to be inserted.
void qdelete()
{
int item;
if(front==rear+1)
printf("Queue is
empty");
else
{
item=queue[front];
printf("%d is
deletedn",item);
front++;
}
}
BCA204A21: DATA STRUCTURES 44 Dr.G.Jasmine Beulah
Queue Underflow
 Deletion or removal of an element from the queue can be done only if at least one
item is there in the queue.
 It means that the queue must be non-empty. An attempt to remove an element from
an empty queue is called ‘queue underflow’.
 A function to check whether queue is empty or not return 1 when the queue is empty
and returns 0 when the queue is not empty.
This function is used to check for queue underflow.
int qempty()
{
if(front == rear+1)
return 1;
else return 0;
}
BCA204A21: DATA STRUCTURES 45 Dr.G.Jasmine Beulah
Queue display
 After deleting an element, the value of front will increase from -1 to 0.
 however, the queue will look something like following.
void qdisplay()
{
int item;
int p=front;
if(p==rear+1)
printf("Queue is empty");
else
{
printf("nQueue Elements");
while(p<=rear)
{
printf("%dt",queue[p]);
p++;
}
}
}
BCA204A21: DATA STRUCTURES 46 Dr.G.Jasmine Beulah
Types of Queue
BCA204A21: DATA STRUCTURES 47 Dr.G.Jasmine Beulah
Disadvantage of Linear Queue:
 Using array to implement the queue, will create a problem when the queue is full –
memory space are fixed.
Problem solution
Create a circular queue.
Circular queue wraps around to the
beginning whenever the Head(Front) or the
Tail(Rear) reaches the end of the queue.
BCA204A21: DATA STRUCTURES 48 Dr.G.Jasmine Beulah
Circular Queue:
 The method of arranging the queue elements Q[1], Q[2], …….. Q[n] in a circular fashion
with Q[1] following Q[n] is called Circular Queue.
 A circular queue is similar to a linear queue as it is also based on the FIFO (First
In First Out) principle except that the last position is connected to the first position
in a circular queue that forms a circle.
 It is also known as a Ring Buffer.
BCA204A21: DATA STRUCTURES 49 Dr.G.Jasmine Beulah
Circular Queue:
Enqueue operation
The steps of enqueue operation are given below:
 First, we will check whether the Queue is full or not.
 Initially the front and rear are set to -1. When we insert the first element in a Queue, front
and rear both are set to 0.
 When we insert a new element, the rear gets incremented, i.e., rear=rear+1.
Let's understand the enqueue and dequeue operation through the
diagrammatic representation.
BCA204A21: DATA STRUCTURES 50 Dr.G.Jasmine Beulah
Circular Queue:
BCA204A21: DATA STRUCTURES 51 Dr.G.Jasmine Beulah
Circular Queue:
BCA204A21: DATA STRUCTURES 52 Dr.G.Jasmine Beulah
Circular Queue:
void insert(int ele)
{
if(front==rear+1)||(rear==size-1)
{
printf(“Cqueue is full”);
}
elseif(front==-1 && rear==-1)
{
front=rear=0;
cqueue[rear]=ele;
rear++;
}
else if(rear=size-1)
{
rear=0;
cqueue[rear]=ele;
}
else { rear++;
cqueue[rear]=ele;
}
void delete()
{
if(front==-1 && rear==-1)
{
printf(“Cqueue is empty”);
}
elseif(front==rear)
{
ele=cqueue[front];
front=rear-1;
else
{
ele=cqueue[front];
front++;
}
elseif(front=size-1)
{
ele=cqueue[front];
front=0;
}
else { ele=cqueue[front];
BCA204A21: DATA STRUCTURES 53 Dr.G.Jasmine Beulah
Priority Queue:
 A Queue in which it is able to insert elements or remove elements
from any position on some property (such as priority of the task to
be processed) is referred as priority Queue.
 An element of higher priority is processed before any element of
lower priority.
 Two elements with the same priority are processed according to
the order in which they were added to the queue.
 In priority queue, every element has been assigned with a priority
value called priority.
 The elements can be inserted or deleted randomly any where in
the queue. Priority queue is a data structure in which prioritized
insertion and deletion operations on elements can be performed
according to their priority values.
BCA204A21: DATA STRUCTURES 54 Dr.G.Jasmine Beulah
Priority Queue:
BCA204A21: DATA STRUCTURES 55 Dr.G.Jasmine Beulah
Ascending Priority Queue:
 In this queue, elements can be inserted randomly. The smallest
element of the queue is deleted first.
 If elements with equal priority are present, the FIFO technique
is applied.
BCA204A21: DATA STRUCTURES 56 Dr.G.Jasmine Beulah
Descending Priority Queue:
 In this queue, elements can be inserted randomly.
 The largest element of the queue is deleted first.
 If elements with equal priority are present, the FIFO technique
is applied
BCA204A21: DATA STRUCTURES 57 Dr.G.Jasmine Beulah
Double Ended Queue
A dequeue, also known as a double-ended queue, is an
ordered collection of items similar to the queue.
It has two ends, a front and a rear, and the items remain
positioned in the collection.
What makes a dequeue different is the unrestrictive
nature of adding and removing items.
New items can be added at either the front or the rear.
Likewise, existing items can be removed from either
end.
In a sense, this hybrid linear structure provides all the
capabilities of stacks and queues in a single data
structure.
BCA204A21: DATA STRUCTURES 58 Dr.G.Jasmine Beulah
Double Ended Queue
 A deque is structured, as described above, as an ordered
collection of items where items are added and removed
from either end, either front or rear. The deque
operations are given below.
BCA204A21: DATA STRUCTURES 59 Dr.G.Jasmine Beulah
Double Ended Queue
 deque() creates a new deque that is empty. It needs no parameters
and returns an empty deque.
 add_front(item) adds a new item to the front of the deque. It needs
the item and returns nothing.
 add_rear(item) adds a new item to the rear of the deque. It needs
the item and returns nothing.
 remove_front() removes the front item from the deque. It needs no
parameters and returns the item. The deque is modified.
 remove_rear() removes the rear item from the deque. It needs no
parameters and returns the item. The deque is modified.
 is_empty() tests to see whether the deque is empty. It needs no
parameters and returns a boolean value.
 size() returns the number of items in the deque. It needs no
parameters and returns an integer.
BCA204A21: DATA STRUCTURES 60 Dr.G.Jasmine Beulah
Applications of Queue:
1. One of the classical areas to which queues can be applied is
simulation. (Simulation is a process of forming an abstract model
from a real situation in order to understand the impact of
modifications and the effect of introducing various strategies on
the situation)
2. Queues find their application in CPU scheduling, in printer
spooling, in message queuing in computer network.

More Related Content

Similar to Stacks.pptx

Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptxline24arts
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxKALPANAC20
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxhaaamin01
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxPrakash Zodge
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 

Similar to Stacks.pptx (20)

Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Stack application
Stack applicationStack application
Stack application
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
 
stack & queue
stack & queuestack & queue
stack & queue
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
Stack in C.pptx
Stack in C.pptxStack in C.pptx
Stack in C.pptx
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 
Data structures
Data structures Data structures
Data structures
 
Stack
StackStack
Stack
 
Stack unit-ii
Stack unit-iiStack unit-ii
Stack unit-ii
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 

More from Dr. Jasmine Beulah Gnanadurai

More from Dr. Jasmine Beulah Gnanadurai (20)

Data Warehouse_Architecture.pptx
Data Warehouse_Architecture.pptxData Warehouse_Architecture.pptx
Data Warehouse_Architecture.pptx
 
DMQL(Data Mining Query Language).pptx
DMQL(Data Mining Query Language).pptxDMQL(Data Mining Query Language).pptx
DMQL(Data Mining Query Language).pptx
 
Quick Sort.pptx
Quick Sort.pptxQuick Sort.pptx
Quick Sort.pptx
 
KBS Architecture.pptx
KBS Architecture.pptxKBS Architecture.pptx
KBS Architecture.pptx
 
Knowledge Representation in AI.pptx
Knowledge Representation in AI.pptxKnowledge Representation in AI.pptx
Knowledge Representation in AI.pptx
 
File allocation methods (1)
File allocation methods (1)File allocation methods (1)
File allocation methods (1)
 
Segmentation in operating systems
Segmentation in operating systemsSegmentation in operating systems
Segmentation in operating systems
 
Mem mgt
Mem mgtMem mgt
Mem mgt
 
Decision tree
Decision treeDecision tree
Decision tree
 
Association rules apriori algorithm
Association rules   apriori algorithmAssociation rules   apriori algorithm
Association rules apriori algorithm
 
Big data architecture
Big data architectureBig data architecture
Big data architecture
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
Aritificial intelligence
Aritificial intelligenceAritificial intelligence
Aritificial intelligence
 
Java threads
Java threadsJava threads
Java threads
 
Java Applets
Java AppletsJava Applets
Java Applets
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
JavaScript Operators
JavaScript OperatorsJavaScript Operators
JavaScript Operators
 
Css Text Formatting
Css Text FormattingCss Text Formatting
Css Text Formatting
 
CSS - Cascading Style Sheet
CSS - Cascading Style SheetCSS - Cascading Style Sheet
CSS - Cascading Style Sheet
 
Newsgroup
NewsgroupNewsgroup
Newsgroup
 

Recently uploaded

MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 

Recently uploaded (20)

MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 

Stacks.pptx

  • 1. Data Structures 1 Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES Dr. G. Jasmine Beulah Dept. Computer Science, Kristu Jayanti College(UG) Bengaluru
  • 2. Stack Stack - Definition; Array representation of stack; Operations on stack; Polish notation; Reverse Polish notation; Applications of stack, Conversion of an infix arithmetic expression to postfix, Evaluation of postfix expression 2 Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 3. Today’s discussion… 3 *Stack *Basic principles *Operation of stack *Stack using Array *Applications of stack Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 5. Basic Idea • A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real- world stack, for example – a deck of cards or a pile of plates, etc. 5 Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 6. Stack Representation • Can be implemented by means of Array, Structure, Pointers and Linked List. • Stack can either be a fixed size or dynamic. 6 Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 8. STACK: Last-In-First-Out (LIFO) *An abstract data type (ADT) is an abstraction of a data structure *An ADT specifies: *Data stored *Operations on the data 8 Assumption: stack contains integer elements! Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 9. Objects: * A finite sequence of nodes Operations: *Create *push(): Insert element at top on the stack *peek(): Return top element without removing it *pop(): Remove and return top element *isEmpty(): check if stack is empty *isfull() : test if stack is full *Stack ADT Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES 9
  • 10. Stack using Array 10 Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 11. 11 top top PUSH Push using Stack Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 12. 12 top top POP Pop using Stack Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 13. • In the array implementation, we would: • Declare an array of fixed size (which determines the maximum size of the stack). • Keep a variable which always points to the “top” of the stack. • Contains the array index of the “top” element. 13 Basic Idea Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 14. Declaration & Stack Creation 14 #define MAXSIZE 10 int stack[MAXSIZE]; int top =-1; ARRAY Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 15. Checking for Stack Full 15 int isfull() { if(top == MAXSIZE) return 1; else return 0; } ARRAY Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 16. Checking for stack empty 16 int isempty () { if (top == -1) return 1; else return (0); } ARRAY Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 17. Pushing an element into stack 17 int push(int data) { if(!isfull()) { top = top + 1; stack[top] = data; } else { printf("Could not insert data, Stack is full.n"); } } ARRAY Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 18. Popping an element from stack 18 int pop() { int data; if(!isempty()) { data = stack[top]; top = top - 1; return data; } else { printf("Could not retrieve data, Stack is empty.n"); } } ARRAY Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 19. Example: A Stack using an Array 19 #include <stdio.h> #define MAXSIZE 100 struct lifo { int st[MAXSIZE]; int top; }; typedef struct lifo stack; main() { stack A, B; create(&A); create(&B); push(&A,10); push(&A,20); push(&A,30); push(&B,100); push(&B,5); printf (“%d %d”, pop(&A), pop(&B)); push (&A, pop(&B)); if (isempty(&B)) printf (“n B is empty”); return; } Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 20. Applications of Stacks • Direct applications: • Page-visited history in a Web browser • Undo sequence in a text editor • Saving local variables when one function calls another, and this one calls another 20 Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES
  • 22. Notation *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 − Infix Notation Prefix (Polish) Notation Postfix (Reverse-Polish) Notation BCA204A21: DATA STRUCTURES 22 Dr.G.Jasmine Beulah
  • 23. Infix Notation *In an Infix Notation, operators are used in-between operands. *e.g. a - b + c, where operators are used in-between operands. * Humans can easily read, write, and speak in infix notation but the same does not go well with computing devices. BCA204A21: DATA STRUCTURES 23 Dr.G.Jasmine Beulah
  • 24. Prefix Notation *In the Prefix 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 BCA204A21: DATA STRUCTURES 24 Dr.G.Jasmine Beulah
  • 25. Postfix Notation *Postfix notation is also known as Reversed Polish Notation *In the Postfix notation style, the operator is post-fixed to the operands i.e., the operator is written after the operands. *For example, ab+ *This is equivalent to its infix notation a + b. BCA204A21: DATA STRUCTURES 25 Dr.G.Jasmine Beulah
  • 26. A simple example to show the difference of the three Notation BCA204A21: DATA STRUCTURES 26 Dr.G.Jasmine Beulah
  • 27. Infix and Postfix Notations • Infix: operators placed between operands: A+B*C • Postfix: operands appear before their operators:- ABC*+ • There are no precedence rules to learn in postfix notation, and parentheses are never needed BCA204A21: DATA STRUCTURES 27 Dr.G.Jasmine Beulah
  • 28. Infix Postfix A + B A B + A + B * C A B C * + (A + B) * C A B + C * A + B * C + D A B C * + D + (A + B) * (C + D) A B + C D + * A * B + C * D A B * C D * + A + B * C  (A + (B * C))  (A + (B C *) )  A B C * + A + B * C + D  ((A + (B * C)) + D )  ((A + (B C*) )+ D)  ((A B C *+) + D)  A B C * + D + 28 BCA204A21: DATA STRUCTURES Infix to Postfix Dr.G.Jasmine Beulah
  • 29. Infix to postfix conversion • Use a stack for processing operators (push and pop operations). • Scan the sequence of operators and operands from left to right and perform one of the following: • output the operand, • push an operator of higher precedence, • pop an operator and output, till the stack top contains operator of a lower precedence and push the present operator. BCA204A21: DATA STRUCTURES 29 Dr.G.Jasmine Beulah
  • 30. The Algorithm Steps… 1. Print operands as they arrive. 2. If the stack is empty or contains a left parenthesis on top, push the incoming operator onto the stack. 3. If the incoming symbol is a left parenthesis, push it on the stack. 4. If the incoming symbol is a right parenthesis, pop the stack and print the operators until you see a left parenthesis. Discard the pair of parentheses. 5. If the incoming symbol has higher precedence than the top of the stack, push it on the stack. 6. If the incoming symbol has equal precedence with the top of the stack, use association. If the association is left to right, pop and print the top of the stack and then push the incoming operator. If the association is right to left, push the incoming operator. 7. If the incoming symbol has lower precedence than the symbol on the top of the stack, pop the stack and print the top operator. Then test the incoming operator against the new top of stack. 8. At the end of the expression, pop and print all operators on the stack. (No parentheses should remain.) BCA204A21: DATA STRUCTURES 30 Dr.G.Jasmine Beulah
  • 31. Infix to Postfix Conversion Requires operator precedence information Operands: Add to postfix expression. Close parenthesis: pop stack symbols until an open parenthesis appears. Operators: Pop all stack symbols until a symbol of lower precedence appears. Then push the operator. End of input: Pop all remaining stack symbols and add to the expression. BCA204A21: DATA STRUCTURES 31 Dr.G.Jasmine Beulah
  • 32. Current symbol Operator Stack Postfix string 1 A A 2 * * A 3 ( * ( A 4 B * ( A B 5 + * ( + A B 6 C * ( + A B C 7 * * ( + * A B C 8 D * ( + * A B C D 9 ) * A B C D * + 10 + + A B C D * + * 11 E + A B C D * + * E 12 A B C D * + * E + Expression: A * (B + C * D) + E becomes A B C D * + * E + Postfix notation is also called as Reverse Polish Notation (RPN) 32 BCA204A21: DATA STRUCTURES Infix to Postfix Rules Dr.G.Jasmine Beulah
  • 33. Dr.G.Jasmine Beulah BCA204A21: DATA STRUCTURES 33 Evaluation of Postfix Expression Algorithm: 1. Scan the postfix expression from left to right. 2. If we encounter an operand, push it on to stack. 3. If we encounter an operator, pop two operands from the stack. The first operand popped is an operand 2 and second popped is operand 1. 4. Perform the desired operation on the operands. i.e. operand 1 operator operand 2 5. Push the result on to the stack. 6. Repeat the above procedure till the end of inputs encountered.
  • 35. Queue : BCA204A21: DATA STRUCTURES 35 Dr.G.Jasmine Beulah Queue
  • 36. Basic Idea(FIFO) • Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). BCA204A21: DATA STRUCTURES 36 Dr.G.Jasmine Beulah
  • 37. BCA204A21: DATA STRUCTURES 37 Dr.G.Jasmine Beulah 1. 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. 2. Queue is referred to be as First In First Out list. 3. For example, people waiting in line for a rail ticket form a queue. Queue (FIFO)
  • 38. Queue Representation • As in stacks, a queue can also be implemented using Arrays, Linked- lists, Pointers and Structures. BCA204A21: DATA STRUCTURES 38 Dr.G.Jasmine Beulah
  • 39. BCA204A21: DATA STRUCTURES 39 Dr.G.Jasmine Beulah QUEUE enqueue create dequeue size isempty Operations of Queue
  • 40. BCA204A21: DATA STRUCTURES 40 Dr.G.Jasmine Beulah Array representation of Queue  Represent queue by using Linear Arrays.  There are two variables i.e. front and rear, that are implemented in the case of every queue.  Front and rear variables point to the position from where insertions and deletions are performed in a queue.  Initially, the value of front and queue is -1 which represents an empty queue. Initial: int front =-1 int rear = -1;
  • 41. BCA204A21: DATA STRUCTURES 41 Dr.G.Jasmine Beulah Queue Insertion Points to remember when we add an element in the queue:  Check whether the queue has any space to add an element into it.  If the queue has spaces then add the element into the queue by incrementing the REAR pointer.  Example: Increment the REAR as REAR+1 or REAR++  Queue [Rear] = Item which is used to add an element into the queue  Note: Rear is the position where the item is to be inserted. void qinsert() { int item; if(rear==max-1) printf("Queue is full"); else { printf("Enter the value to insertn"); scanf("%d",&item); rear++; queue[rear]=item; } }
  • 42. BCA204A21: DATA STRUCTURES 42 Dr.G.Jasmine Beulah Queue Overflow  Insertion of an element in a queue is possible only if there is room in the array to fit in elements.  If the maximum size of the array is reached and if an attempt to insert an element into the queue that is full, is tried, the condition that occurs is called ‘queue overflow’.  A function to check whether queue is full or not returns 1 when the queue is full and returns 0 when the queue is not full.  This function is used to check for queue overflow.  A queue is full when REAR reaches the maximum size of the array used for the queue i.e. REAR=max-1. FRONT = 0 and REAR = n-1 indicates this condition. int qfull( ) { if(rear = = max-1) return 1; else return 0; }
  • 43. BCA204A21: DATA STRUCTURES 43 Dr.G.Jasmine Beulah Deletion of element from the queue: Points to remember when we remove an element from the queue:  Check whether the queue has any element to be deleted.  If so, delete the element from the queue by incrementing the FRONT pointer. Example: Item= queue[rear]  Increment the front using front++  Note: Rear is the position where the item is to be inserted. void qdelete() { int item; if(front==rear+1) printf("Queue is empty"); else { item=queue[front]; printf("%d is deletedn",item); front++; } }
  • 44. BCA204A21: DATA STRUCTURES 44 Dr.G.Jasmine Beulah Queue Underflow  Deletion or removal of an element from the queue can be done only if at least one item is there in the queue.  It means that the queue must be non-empty. An attempt to remove an element from an empty queue is called ‘queue underflow’.  A function to check whether queue is empty or not return 1 when the queue is empty and returns 0 when the queue is not empty. This function is used to check for queue underflow. int qempty() { if(front == rear+1) return 1; else return 0; }
  • 45. BCA204A21: DATA STRUCTURES 45 Dr.G.Jasmine Beulah Queue display  After deleting an element, the value of front will increase from -1 to 0.  however, the queue will look something like following. void qdisplay() { int item; int p=front; if(p==rear+1) printf("Queue is empty"); else { printf("nQueue Elements"); while(p<=rear) { printf("%dt",queue[p]); p++; } } }
  • 46. BCA204A21: DATA STRUCTURES 46 Dr.G.Jasmine Beulah Types of Queue
  • 47. BCA204A21: DATA STRUCTURES 47 Dr.G.Jasmine Beulah Disadvantage of Linear Queue:  Using array to implement the queue, will create a problem when the queue is full – memory space are fixed. Problem solution Create a circular queue. Circular queue wraps around to the beginning whenever the Head(Front) or the Tail(Rear) reaches the end of the queue.
  • 48. BCA204A21: DATA STRUCTURES 48 Dr.G.Jasmine Beulah Circular Queue:  The method of arranging the queue elements Q[1], Q[2], …….. Q[n] in a circular fashion with Q[1] following Q[n] is called Circular Queue.  A circular queue is similar to a linear queue as it is also based on the FIFO (First In First Out) principle except that the last position is connected to the first position in a circular queue that forms a circle.  It is also known as a Ring Buffer.
  • 49. BCA204A21: DATA STRUCTURES 49 Dr.G.Jasmine Beulah Circular Queue: Enqueue operation The steps of enqueue operation are given below:  First, we will check whether the Queue is full or not.  Initially the front and rear are set to -1. When we insert the first element in a Queue, front and rear both are set to 0.  When we insert a new element, the rear gets incremented, i.e., rear=rear+1. Let's understand the enqueue and dequeue operation through the diagrammatic representation.
  • 50. BCA204A21: DATA STRUCTURES 50 Dr.G.Jasmine Beulah Circular Queue:
  • 51. BCA204A21: DATA STRUCTURES 51 Dr.G.Jasmine Beulah Circular Queue:
  • 52. BCA204A21: DATA STRUCTURES 52 Dr.G.Jasmine Beulah Circular Queue: void insert(int ele) { if(front==rear+1)||(rear==size-1) { printf(“Cqueue is full”); } elseif(front==-1 && rear==-1) { front=rear=0; cqueue[rear]=ele; rear++; } else if(rear=size-1) { rear=0; cqueue[rear]=ele; } else { rear++; cqueue[rear]=ele; } void delete() { if(front==-1 && rear==-1) { printf(“Cqueue is empty”); } elseif(front==rear) { ele=cqueue[front]; front=rear-1; else { ele=cqueue[front]; front++; } elseif(front=size-1) { ele=cqueue[front]; front=0; } else { ele=cqueue[front];
  • 53. BCA204A21: DATA STRUCTURES 53 Dr.G.Jasmine Beulah Priority Queue:  A Queue in which it is able to insert elements or remove elements from any position on some property (such as priority of the task to be processed) is referred as priority Queue.  An element of higher priority is processed before any element of lower priority.  Two elements with the same priority are processed according to the order in which they were added to the queue.  In priority queue, every element has been assigned with a priority value called priority.  The elements can be inserted or deleted randomly any where in the queue. Priority queue is a data structure in which prioritized insertion and deletion operations on elements can be performed according to their priority values.
  • 54. BCA204A21: DATA STRUCTURES 54 Dr.G.Jasmine Beulah Priority Queue:
  • 55. BCA204A21: DATA STRUCTURES 55 Dr.G.Jasmine Beulah Ascending Priority Queue:  In this queue, elements can be inserted randomly. The smallest element of the queue is deleted first.  If elements with equal priority are present, the FIFO technique is applied.
  • 56. BCA204A21: DATA STRUCTURES 56 Dr.G.Jasmine Beulah Descending Priority Queue:  In this queue, elements can be inserted randomly.  The largest element of the queue is deleted first.  If elements with equal priority are present, the FIFO technique is applied
  • 57. BCA204A21: DATA STRUCTURES 57 Dr.G.Jasmine Beulah Double Ended Queue A dequeue, also known as a double-ended queue, is an ordered collection of items similar to the queue. It has two ends, a front and a rear, and the items remain positioned in the collection. What makes a dequeue different is the unrestrictive nature of adding and removing items. New items can be added at either the front or the rear. Likewise, existing items can be removed from either end. In a sense, this hybrid linear structure provides all the capabilities of stacks and queues in a single data structure.
  • 58. BCA204A21: DATA STRUCTURES 58 Dr.G.Jasmine Beulah Double Ended Queue  A deque is structured, as described above, as an ordered collection of items where items are added and removed from either end, either front or rear. The deque operations are given below.
  • 59. BCA204A21: DATA STRUCTURES 59 Dr.G.Jasmine Beulah Double Ended Queue  deque() creates a new deque that is empty. It needs no parameters and returns an empty deque.  add_front(item) adds a new item to the front of the deque. It needs the item and returns nothing.  add_rear(item) adds a new item to the rear of the deque. It needs the item and returns nothing.  remove_front() removes the front item from the deque. It needs no parameters and returns the item. The deque is modified.  remove_rear() removes the rear item from the deque. It needs no parameters and returns the item. The deque is modified.  is_empty() tests to see whether the deque is empty. It needs no parameters and returns a boolean value.  size() returns the number of items in the deque. It needs no parameters and returns an integer.
  • 60. BCA204A21: DATA STRUCTURES 60 Dr.G.Jasmine Beulah Applications of Queue: 1. One of the classical areas to which queues can be applied is simulation. (Simulation is a process of forming an abstract model from a real situation in order to understand the impact of modifications and the effect of introducing various strategies on the situation) 2. Queues find their application in CPU scheduling, in printer spooling, in message queuing in computer network.