0% found this document useful (0 votes)
32 views

Data Structure Unit III - Stack and Queue - SPJ

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Data Structure Unit III - Stack and Queue - SPJ

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

UNIT III- STACK AND

QUEUE
Classification of Data Structures
Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT, Stack
Applications: Reversing data, Arithmetic expressions
conversion and evaluation.

■ Queue: Concept, Queue operations, Array


representation of queue, Queue as ADT, Circular
queue, Priority Queue, Applications of queue:
Categorizing data, Simulation of queue.
Stack
■ Stack is an abstract data type with a bounded(predefined) capacity. It is a
simple data structure that allows adding and removing elements in a
particular order. Every time an element is added, it goes on the top of the
stack and the only element that can be removed is the element that is at
the top of the stack, just like a pile of objects.
■ It is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO(Last In First Out) or FILO(First
In Last Out).

https://www.youtube.com/watch?v=1S
Wr7q121gc&t=180s
Features of Stack
1.Stack is an ordered list of similar data type.
2.Stack is a LIFO(Last in First out) structure or we can
say FILO(First in Last out).
3.push() function is used to insert new elements into the Stack
and pop() function is used to remove an element from the
stack. Both insertion and removal are allowed at only one end
of Stack called Top.
4.Stack is said to be in Overflow state when it is completely
full and is said to be in Underflow state if it is completely
empty.
Basic operations of Stack

• Push: Adds an item in the stack. If the stack is full, then it is said to be an
Overflow condition.
• Pop: Removes an item from the stack. The items are popped in the reversed
order in which they are pushed. If the stack is empty, then it is said to be an
Underflow condition.
• Peek or Top: Returns top element of stack.
• isEmpty: Returns true if stack is empty, else false.
Push and Pop
■ Primary operations: Push and Pop
■ Push
– Add an element to the top of the stack
■ Pop
– Remove the element at the top of the stack

empty stack push an element push another pop

top
B
top top
A A A
top
7
Applications of stack
•To reverse a word - Put all the letters in a stack and pop them out. Because
of the LIFO order of stack, you will get the letters in reverse order.
•In compilers - Compilers use the stack to calculate the value of expressions
like 2 + 4 / 5 * (7 - 9) by converting the expression to prefix or postfix
form.
•In browsers - The back button in a browser saves all the URLs you have
visited previously in a stack. Each time you visit a new page, it is added on
top of the stack. When you press the back button, the current URL is
removed from the stack and the previous URL is accessed.
• https://www.youtube.com/watch?v=d_XvFOkQz5k&list=PLhb7SOmGNU
c5AZurO-im4t_RDr-ymjz0d
Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT,
■ Stack Applications: Reversing data, Arithmetic
expressions conversion and evaluation.
Implementation:

There are two ways to implement a stack:


• Using array
• Using linked list
Stack Operations using Array

■ A stack data structure can be implemented using a one-


dimensional array. But stack implemented using array stores
only a fixed number of data values(Static). This
implementation is very simple. Just define a one dimensional
array of specific size and insert or delete the values into that
array by using LIFO principle with the help of a variable
called 'top'. Initially, the top is set to -1. Whenever we want to
insert a value into the stack, increment the top value by one
and then insert. Whenever we want to delete a value from the
stack, then delete the top value and decrement the top value
by one.
Stack Operations using Array

• 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 the functions used in stack implementation.
• Step 3 - Create a one dimensional array with fixed size (int stack[SIZE])
• Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top = -1)
• Step 5 - In main method, display menu with list of operations and make
suitable function calls to perform operation selected by the user on the
stack.
Stack Operations using Array

■ push(value) - Inserting value into the stack


■ In a stack, push() is a function used to insert an element into the stack. In a
stack, the new element is always inserted at top position. Push function
takes one integer value as parameter and inserts that value into the stack.
We can use the following steps to push an element on to the stack...
• Step 1 - Check whether stack is FULL. (top == SIZE-1)
• Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not
possible!!!" and terminate the function.
• Step 3 - If it is NOT FULL, then increment top value by one (top++) and set
stack[top] to value (stack[top] = value).
Stack Operations using Array

■ pop() - Delete a value from the Stack


■ In a stack, pop() is a function used to delete an element from the stack. In a
stack, the element is always deleted from top position. Pop function does
not take any value as parameter. We can use the following steps to pop an
element from the stack...
• Step 1 - Check whether stack is EMPTY. (top == -1)
• Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
• Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value
by one (top--).
Stack Operations using Array

■ display() - Displays the elements of a Stack


■ We can use the following steps to display the elements of a stack...
• Step 1 - Check whether stack is EMPTY. (top == -1)
• Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the
function.
• Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top.
Display stack[i] value and decrement i value by one (i--).
• Step 3 - Repeat above step until i value becomes '0'.
// C program for array implementation of stack
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

// A structure to represent a stack


struct Stack {
int top;
unsigned capacity;
int* array;
};

// function to create a stack of given capacity. It initializes size of


// stack as 0
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack->capacity * sizeof(int));
return stack;
}

// Stack is full when top is equal to the last index


int isFull(struct Stack* stack)
{
return stack->top == stack->capacity - 1;
}

// Stack is empty when top is equal to -1


int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}
// Function to add an item to stack. It increases top by 1
void push(struct Stack* stack, int item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}

// Function to remove an item from stack. It decreases top by 1


int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
}

// Function to return the top from stack without removing it


int peek(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}

// Driver program to test above functions


int main()
{
struct Stack* stack = createStack(100);

push(stack, 10);
push(stack, 20);
push(stack, 30);

printf("%d popped from stack\n", pop(stack));


return 0;
}
Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT,
■ Stack Applications: Reversing data, Arithmetic
expressions conversion and evaluation.
Abstract Data Types(ADT)
■ Abstract Data type (ADT) is a type (or class) for objects whose behaviour is
defined by a set of value and a set of operations.
■ The definition of ADT only mentions what operations are to be performed but
not how these operations will be implemented. It does not specify how data will
be organized in memory and what algorithms will be used for implementing the
operations. It is called “abstract” because it gives an implementation-
independent view. The process of providing only the essentials and hiding the
details is known as abstraction.

Stack ADT

• In Stack ADT Implementation instead of data being stored in each node, the
pointer to data is stored.
• The program allocates memory for the data and address is passed to the stack
ADT.

Stack ADT

• push() – Insert an element at one end of the stack called top.


• pop() – Remove and return the element at the top of the stack, if it is not empty.
• peek() – Return the element at the top of the stack without removing it, if the
stack is not empty.
• size() – Return the number of elements in the stack.
• isEmpty() – Return true if the stack is empty, otherwise return false.
• isFull() – Return true if the stack is full, otherwise return false.
Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT
■ Stack Applications: Reversing data, Arithmetic
expressions conversion and evaluation.
Reversing a String
Step II
Step I
Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT
■ Stack Applications: Reversing data, Arithmetic
expressions conversion and evaluation.
Arithmetic Expressions

■ What is Polish Notation?


ABC+*
• Conventionally, we use the operator symbol between its two
operands in an arithmetic expression.
■ A+B C–D*E A*(B+C)
–We can use parentheses to change the precedence of the
operators.
–Operator precedence is pre-defined.
• This notation is called INFIX notation.
–Parentheses can change the precedence of evaluation.
–Multiple passes required for evaluation.
–Polish POSTFIX notation
• Refers to the notation in which the operator symbol is placed
after its two operands.
■ AB+CD* AB*CD+/=(A*B)/(C+D)
Polish notation
–Polish PREFIX notation
• Refers to the notation in which the operator symbol is placed
before its two operands.
■ +AB*CD /*AB-CD
■ How to convert an infix expression to Polish form?
• Write down the expression in fully parenthesized form. Then convert
stepwise.
• Example:
■ A+(B*C)/D-(E*F)-G= (((A+((B*C)/D))-(E*F))-G)
■ 653 21 1 2 3 4 45 6
■ Post Fix= (A+(BC*D/)-E*F))-G=
■ (ABC*D\+) -(EF*)=(ABC*D\+EF*-G-

■ (((A+((B*C)/D))-(E*F))-G)
• Polish Postfix form:
■ A B C * D / + E F * - G -
• Polish Prefix form:= A+ /*BCD= --+A\*BDC*EFG
■ – Try it out ….
Conversion Infix to Postfix and Prefix

■ (A + (B * C))
■ Infix to Postfix= ABC*+

■ Infix to Prefix= +A*BC


Polish notation

• Advantages:
–No concept of operator priority.
•Simplifies the expression evaluation rule.
–No need of any parenthesis.
•Hence no ambiguity in the order of evaluation.
–Evaluation can be carried out using a single scan over
the expression string.
•Using stack.
Evaluation of a Polish Expression

• Can be done very conveniently using a stack.


–We would use the Polish postfix notation as illustration.
• Requires a single pass through the expression string from left to
right.
• Polish prefix evaluation would be similar, but the string needs to
be scanned from right to left.
Basic Idea
• Let Q denote an infix expression.
–May contain left and right parentheses.
–Operators are:
• Highest priority: ^ (exponentiation)
• Then: * (multiplication), / (division)
• Then: + (addition), – (subtraction)
–Operators at the same level are evaluated from left to right.
• In the algorithm to be presented:
–We begin by pushing a ‘(’ in the stack.
–Also add a ‘)’ at the end of Q.
Example
Infix notation: (2+4) * (4+6)
Post-fix notation: 2 4 + 4 6 + *
Result: 60
(A + (B * C – (D / E ^ F) * G) * H )
Q STACK Output Postfix String P
A ( A
+ ( + A
( ( + ( A Rules
B ( + ( A B 1.Pop the stack for
* ( + ( * A B two operator of
C ( + ( * A B C same priority.
- ( + ( - A B C * 2.Pop the stack if
( ( + ( - ( A B C * you find a symbol
D ( + ( - ( A B C * D of lower priority
/ ( + ( - ( / A B C * D number than the
E ( + ( - ( / A B C * D E
current one
^ ( + ( - ( / ^ A B C * D E
3.Pop the stack for
F ( + ( - ( / ^ A B C * D E F
closed brackets
) ( + ( - A B C * D E F ^ /
* ( + ( - * A B C * D E F ^/
G ( + ( - * A B C * D E F ^ / G [“^"] = 4
) ( + A B C * D E F ^ / G * - ["*"] = 3
* ( + * A B C * D E F ^ / G * - ["/"] = 3
H ( + * A B C * D E F ^ / G * - H ["+"] = 2
) A B C * D E F ^ / G * - H * + ["-"] = 2
["("] = 1
Conversion of Infix to Prefix Expression

■ a*b+c:
■ Manually:+*abc
Conversion of Infix Expression to Prefix Expression :
Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT, Stack
Applications: Reversing data, Arithmetic expressions
conversion and evaluation.
■ Queue: Concept, Queue operations, Array
representation of queue, Queue as ADT, Circular
queue, Priority Queue, Applications of queue:
Categorizing data, Simulation of queue.
Queue

■ 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). Queue follows First-
In-First-Out methodology, i.e., the data item stored first will be accessed first.

Queue Representation

■ The following diagram given below tries to explain queue representation as data
structure

■ As in stacks, a queue can also be implemented using Arrays, Linked-lists,


Pointers and Structures. For the sake of simplicity, we shall implement queues
using one-dimensional array.e −
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.
■ In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing
(or storing) data in the queue we take help of rear pointer.
Enqueue Operation
■ Queues maintain two data pointers, front and rear. Therefore, its operations are
comparatively difficult to implement than that of stacks.
■ The following steps should be taken to enqueue (insert) data into a queue −
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the next empty
space.
• Step 4 − Add data element to the queue location, where the rear is pointing.
• Step 5 − return success.

Enqueue Operation

int enqueue(int data)


if(isfull())
return 0;
rear = rear + 1;
queue[rear] = data;
return 1;
end procedure
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.
Dequeue Operation

int dequeue() {
if(isempty()) return 0;
int data =
queue[front]; front =
front + 1; return data;
}
Applications of Queue Data Structure

• CPU scheduling, Disk Scheduling


• When data is transferred asynchronously between two
processes.The queue is used for synchronization. eg: IO
Buffers, pipes, file IO, etc
• Handling of interrupts in real-time systems.
• Call Center phone systems use Queues to hold people calling
them in an order
Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT, Stack
Applications: Reversing data, Arithmetic expressions
conversion and evaluation.
■ Queue: Concept, Queue operations, Array
representation of queue, Queue as ADT, Circular
queue, Priority Queue, Applications of queue:
Categorizing data, Simulation of queue.
Implementation of Queue

■ Queue data structure can be implemented in two ways. They are as


follows...
1. Using Array
2. Using Linked List
■ When a queue is implemented using an array, that queue can organize an
only limited number of elements. When a queue is implemented using a
linked list, that queue can organize an unlimited number of elements.
Array implementation of queue
■ In queue, insertion and deletion happen at the opposite ends, so implementation is
not as simple as stack.
To implement a queue using array, create an array arr of size n and take two
variables front and rear both of which will be initialized to 0 which means the queue
is currently empty. Element rear is the index upto which the elements are stored in
the array and front is the index of the first element of the array. Now, some of the
implementation of queue operations are as follows:

1. Enqueue: Addition of an element to the queue. Adding an element will be performed


after checking whether the queue is full or not. If rear < n which indicates that the
array is not full then store the element at arr[rear] and increment rear by 1 but if rear
== n then it is said to be an Overflow condition as the array is full.
2. Dequeue: Removal of an element from the queue. An element can only be deleted
when there is at least an element to delete i.e. rear > 0. Now, element
at arr[front] can be deleted but all the remaining elements have to shifted to the left
by one position in order for the dequeue operation to delete the second element
from the left on another dequeue operation.
3. Front: Get the front element from the queue i.e. arr[front] if queue is not empty.
4. Display: Print all element of the queue. If the queue is non-empty, traverse and print
all the elements from index front to rear.
#include<stdio.h>
#include<conio.h>
#define SIZE 10
void enQueue(int);
void deQueue();
void display();
int queue[SIZE], front = -1, rear = -1;
void main() {
int value, choice;
clrscr();
while(1)
{ printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{ case 1:
printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!"); } } }
void enQueue(int value)
{ if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{ if(front == -1)
front = 0; rear++; queue[rear] = value;
printf("\nInsertion success!!!"); } }

void deQueue()
{ if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{ printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1; } }

void display()
{ if(rear == -1)
printf("\nQueue is Empty!!!");
else{ int i; printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]); } }
Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT, Stack
Applications: Reversing data, Arithmetic expressions
conversion and evaluation.
■ Queue: Concept, Queue operations, Array
representation of queue, Queue as ADT, Circular
queue, Priority Queue, Applications of queue:
Categorizing data, Simulation of queue.
Limitation of Queue
■ As you can see in the image below, after a bit of enqueuing and dequeuing, the size of
the queue has been reduced.

The indexes 0 and 1 can only be used after the queue is reset when all the elements have been dequeued.
After REAR reaches the last index, if we can store extra elements in the empty spaces (0 and 1), we can make
use of the empty spaces. This is implemented by a modified queue called the circular queue.
Types of Queue

■ Simple Queue
■ Circular Queue
■ Priority Queue
■ Deque (Double Ended Queue)
Simple Queue

■ In a simple queue, insertion takes place at the rear and removal occurs at the front. It
strictly follows FIFO rule.
Circular Queue

■ In a circular queue, the last element points to the first element making a circular link.

The main advantage of a circular queue over a simple queue is better memory utilization. If the
last position is full and the first position is empty then, an element can be inserted in the first
position. This action is not possible in a simple queue.
How Circular Queue Works

■ Circular Queue works by the process of circular increment i.e. when we try to
increment the pointer and we reach the end of the queue, we start from the beginning
of the queue.
■ The circular increment is performed by modulo division with the queue size
■ if REAR + 1 == 5 (overflow!),
■ REAR = (REAR + 1)%5 = 0 (start of queue)
Circular Queue Operations
■ The circular queue work as follows:
• two pointers FRONT and REAR
• FRONT track the first element of the queue
• REAR track the last elements of the queue
• initially, set value of FRONT and REAR to -1
■ 1. Enqueue Operation
• check if the queue is full
• for the first element, set value of FRONT to 0
• circularly increase the REAR index by 1 (i.e. if the rear reaches the end, next it would
be at the start of the queue)
• add the new element in the position pointed to by REAR
Circular Queue Operations
2. Dequeue Operation
•check if the queue is empty
•return the value pointed by FRONT
•circularly increase the FRONT index by 1
•for the last element, reset the values of FRONT and REAR to -1
However, the check for full queue has a new additional case:
•Case 1: FRONT = 0 && REAR == SIZE - 1
•Case 2: FRONT = REAR + 1
The second case happens when REAR starts from 0 due to circular increment and when its value is just 1
less than FRONT, the queue is full.
Applications of Circular Queue

• CPU scheduling
• Memory management
• Traffic Management
Priority Queue

■ A priority queue is a special type of queue in which each element is associated with a
priority and is served according to its priority. If elements with the same priority occur,
they are served according to their order in the queue.
Priority Queue
■ Generally, the value of the element itself is considered for assigning the
priority.
■ For example, The element with the highest value is considered as the
highest priority element. However, in other cases, we can assume the
element with the lowest value as the highest priority element. In other
cases, we can set priorities according to our needs.
Priority Queue
Priority Queue

■ A typical priority queue supports following operations.


insert(item, priority): Inserts an item with given priority.
getHighestPriority(): Returns the highest priority item.
deleteHighestPriority(): Removes the highest priority item.
■ How to implement priority queue?
Using Array: A simple implementation is to use array of following structure.
■ getHighestPriority() operation can be implemented by linearly searching the
highest priority item in array. This operation takes O(n) time.
■ deleteHighestPriority() operation can be implemented by first linearly searching
an item, then removing the item by moving all subsequent items one position
back.
Difference between Priority Queue and
Normal Queue
■ In a queue, the first-in-first-out rule is implemented whereas, in a priority queue, the
values are removed on the basis of priority. The element with the highest priority is
removed first.
■ Applications of Priority Queue:
1) CPU Scheduling
2) Graph algorithms like Dijkstra’s shortest path algorithm, Prim’s Minimum
Spanning Tree, etc
3) All queue applications where priority is involved.
Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT, Stack
Applications: Reversing data, Arithmetic expressions
conversion and evaluation.
■ Queue: Concept, Queue operations, Array
representation of queue, Queue as ADT, Circular
queue, Priority Queue, Applications of queue:
Categorizing data, Simulation of queue.
Categorizing data using Queue

■ Queues are used in categorization. Queues categorize data into different groups
without losing the original ordering of data.
■ Example:-Library sections
■ In this, we have to perform different addition or removal operations by
maintaining their basic orders
■ This is an example of multiple Queue operations
■ Multiple queue operation can be implemented using single array.
Categorizing data using Queue
■ Multiple queue operation can be implemented using single array.
■ When queue is implemented using array, the size of array should be known in
advance.
■ Frequent overflow conditions will be encountered if queue is allocated a less
space
■ To overcome this problem, evey time modification in code to allocate more
space for the array is needed
■ If large space is allocated to queue, it may result into wastage of memory.
■ f(1)
So a better
f(1)solution
f(2) is f(2)
to have
f(3) multiple
f(3) queues
f(4) or
f(4)tof(5)
have more
f(5)than one queue
in the same array of sufficient size.

■ Queue 1 Queue 2 Queue 3 Queue 4 Queue 5


Unit -III
■ Stack: Concept, Basic Stack operations, Array
representation of stack, Stack as ADT, Stack
Applications: Reversing data, Arithmetic expressions
conversion and evaluation.
■ Queue: Concept, Queue operations, Array
representation of queue, Queue as ADT, Circular
queue, Priority Queue, Applications of queue:
Categorizing data, Simulation of queue.
Simulation of queue

■ The programming application that uses queue is a simulation , Where real life
activity is modelled by objects
■ Example:-Airport simulation
■ How queues may be applied in real life applications by simulating an airport traffic
control program.
Simulation of queue
Problem Specification:
Write a program to simulate airport traffic control for a small but busy airport assuming
the following specifications.
q There is only one runway for both landing and take off.

q In each unit time, only one plane can land or take off but not both.

q Planes arrive and take off at random time, so that at any given unit, the runway may

be idle or a plane may be landing or taking off.


q Number of planes waiting to take off or land should not exceed a certain fixed limit.

q Landing planes should have high priority than Taking-off plane.

q After running for some period, the program should print statistics such as: number of

planes processed, landed, and took off, refused; average landing and take-off waiting
times, and average run-way idle time.
Simulation of queue
Algorithm
Create two queues, landing and takeoff
Initialize statistics counters to zero
Loop for time unit from 1 to some fixed number
Loop from 1 to some random number of landing planes
If landing queue is not full, add plane to the queue else
refuse landing and increment number of refused.
Loop from 1 to some random number of taking off planes
If takeoff queue is not full, add plane to the queue else
refuse landing and increment number of refused.
If landing queue is not empty process next landing plane and
increment count of planes landed
Else if takeoff queue is not empty, process next plane and
increment count of take off planes
Else increment count of run-way idle time.
Print the statistics
Thank You!!
Important points

■ Infix A+B
■ Postfix AB+
■ Prefix +AB
Important points

■ Infix to postfix Conversion(Operators are after Operands)


■ For any given Equation , put the bracket and do the marking of brackets
■ (A+B)=AB+
■ A/(B^C)+(D*E)-(A*C)
■ Put brackets
■ (((A/(B^C))+(D*E))-(A*C))
■ Do marking
Important points
■ Infix to prefix Conversion (((A/(B^C))+(D*E))-(A*C))
6 4 2 1 1 2 3 3 4 5 5 6
Important points

■ Postfix to Infix Conversion


■ AB+C*DE-FG++$ $=Exponent
Important points

■ Postfix to prefix Conversion

AB+C*DE-FG++$ $=Exponent

You might also like