Professional Documents
Culture Documents
Chapter 3 Dsu
Chapter 3 Dsu
Show the memory representation of stack using array with the help of a diagram.
Consider stack contains five integer elements represented with an array A in which each
element occupy 2 bytes memory. Array starts with base address of 2000.
3.In stack only one pointer is used called as 3.In Queue two pointers are used called as
stack top front and rear
5.Application: 5. Application:
Recursion In computer system for
Polish notation organizing processes.
In mobile device for sending
receiving messages.
F NA F
+ + F
) +) F
G +) GF
/ +)/ GF
C +)/ CGF
( + / CGF
* +* / CGF
) +*) / CGF
B +*) B/ CGF
+ +*)+ B/ CGF
( +* +AB/ CGF
NA +*+AB/ CGF
Convert the following infix expression to its prefix form using stack
A + B – C * D/E + F
F NA F
+ + F
E + EF
/ +/ EF
D +/ DEF
* +* /DEF
C +* C/DEF
- +- * C/DEF
B - B+* C/DEF
+ + - B+* C/DEF
A NA A
+ + A
B + AB
↑ +↑ AB
C +↑ ABC
* +* ABC↑
( +*( ABC↑
D +*( ABC↑D
/ +*(/ ABC↑D
E +*(/ ABC↑DE
) +* ABC↑DE/
- - ABC↑DE/*+
F - ABC↑DE/*+F
/ -/ ABC↑DE/*+F
G -/ ABC↑DE/*+FG
NA ABC↑DE/*+FG/-
Describe queue full and queue empty operation conditions on linear queue with
suitable diagrams.
Queue full:-A queue is full when its rear pointer points to max -1 position. Max is
maximum number of elements in a queue. If rear pointer is not equal to max-1 then a new
element can be added to a queue. If queue is full then new element cannot be added to a
queue.
Example:-
Consider max=4. First element is stored at 0th position and last element is stored at 3 rd
position in queue. In the diagram given below rear pointer is pointing to max-1 (3) position
so queue is full.
Queue empty: A queue is empty when its front pointer points to -1 position. When front
pointer is -1 then one cannot delete any data from a queue.
Example:-In the diagram given below front pointer points to -1 value i.e. it points no
location inside queue so queue is empty.
If queue is full then new element cannot be added in a queue. For deletion, front pointer
position is checked and queue empty condition is checked. If front pointer is pointing to -1
then queue is empty and deletion operation cannot be performed. If queue contains any
element then front pointer is incremented by one to remove an element.
If front pointer is pointing to max-1 and element is present at 0th position then front
pointer is initialize to 0th position to continue cycle.
Circular queue has advantage of utilization of space. Circular queue is full only when there
is no empty position in a queue. Before inserting an element in circular queue front and
rear both the pointers are checked.
So if it indicates any empty space anywhere in a queue then insertion takes place.
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}
5
6
5
6
2
11 6+5
2
22 2*11
12
22
4
12
22
3 12/4
22
19 22-3
2
5
3
2
5
4
3
2
5
7 4+3
2
10 | P a g e Mr.KAZI A S M-9765645688
Chapter 3 Introduction to stack and queue
14 7*2
5
9 14-5
Show the effect of PUSH and POP operation on to the stack of size 10. The stack
contains 40, 30, 52, 86, 39, 45, 50 with 50 being at top of the stack. Show
diagrammatically the effect of:
(i) PUSH 59 (ii) PUSH 85
(iii) POP (iv) POP
(v) PUSH 59 (vi) POP
Sketch the final structure of stack after performing the above said operations.
7
5
12 5+7
12
6
12
6 6-2
2
12
4
48 12*4
Linear queue
1. A linear queue can be found in a time-sharing computer system where many users
share the system simultaneously.
2. The first element, which is added into the queue will be the first one to be removed.
Thus queues are also called First-in First-Out lists (FIFO) or Last-In-Last-Out lists
(LILO).
12 | P a g e Mr.KAZI A S M-9765645688
Chapter 3 Introduction to stack and queue
The “max size “represent the maximum capacity of the linear queue.
The following is the condition to test the queue is full or not. (Rear== max size-1)
To insert the first element both pointers should be altered to ‘0’ Front =0 rear=0.
Whenever an item is added to the queue, the value of REAR is incremented by 1.
REAR =REAR+1;
The following is the condition to test the queue is empty or not. Front==-1
Whenever an “Item” is deleted from the queue, the value if front is incremented by 1
Front=Front+1;
To delete the last element, both pointers should be altered.
Front=-1; Rear=-1;
Applications of Queue:
Queue is used when things don’t have to be processed immediatly, but have to be
processed in First InFirst Out order
1) When a resource is shared among multiple consumers. Examples include CPU
scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily received at same rate as
sent) between two processes. Examples include IO Buffers, pipes, file IO, etc.
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic
operations associated with queues −
enqueue() − add (store) an item to the queue.
dequeue() − remove (access) an item from the queue.
Few more functions are required to make the above-mentioned queue operation efficient.
These are −
peek() − Gets the element at the front of the queue without removing it.
isfull() − Checks if the queue is full.
isempty() − Checks if the queue is empty.
Enqueue Operation
13 | P a g e Mr.KAZI A S M-9765645688
Chapter 3 Introduction to stack and queue
Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data where front is
pointing and remove the data after access. The following steps are taken to
perform dequeue operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and exit.
Step 3 − If the queue is not empty, access the data where front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
Step 5 − Return success.
14 | P a g e Mr.KAZI A S M-9765645688
Chapter 3 Introduction to stack and queue
15 | P a g e Mr.KAZI A S M-9765645688
Chapter 3 Introduction to stack and queue
To implement a circular queue data structure using an array, we first perform the following
steps before we implement actual operations.
Step 1 - Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
Step 2 - Declare all user defined functions used in circular queue implementation.
Step 3 - Create a one dimensional array with above defined SIZE (int cQueue[SIZE])
Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '-1'.
(int front = -1, rear = -1)
Step 5 - Implement main method by displaying menu of operations list and make
suitable function calls to perform operation selected by the user on circular queue.
BASIS FOR
LINEAR QUEUE CIRCULAR QUEUE
COMPARISON
Basic Organizes the data elements and Arranges the data in the circular
instructions in a sequential order pattern where the last element is
one after the other. connected to the first element.
Order of task Tasks are executed in order they Order of executing a task may
execution were placed before (FIFO). change.
Insertion and The new element is added from Insertion and deletion can be done
deletion the rear end and removed from at any position.
the front.
Priority queue
16 | P a g e Mr.KAZI A S M-9765645688
Chapter 3 Introduction to stack and queue
A priority queue is a collection in which items can be added at any time, but the only item
that can be removed is the one with the highest priority.
Operations
add(x): add item x
remove: remove the highest priority item
peek: return the highest priority item (without removing it)
size: return the number of items in the priority queue
isEmpty: return whether the priority queue has no items
Examples
Patients in an emergency room
Operating system scheduler
Routing
A* search
Simulation
Many more examples
17 | P a g e Mr.KAZI A S M-9765645688