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
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
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.
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++;
}
}
}
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.
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.
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.