Ds-Unit-Ii 2018 - 2019
Ds-Unit-Ii 2018 - 2019
Q. What is stack? Write its ADT. Explain briefly various representation in Stack
(2018)
Definition:- A STACK is a liner structure in which items may be added or remove only
at one end. An item may be added or removed only from the TOP of the STACK. This
means that the last item to be added to a STACK is the first item to be removed.
Accordingly STACK are also called “Last in First Out” (LIFO) Lists.
E
D top
D Top
C top C
C
B B
A top B top
B
top A
A A A
Initially the stack is empty as the elements A , B, C, D,E are inserted into it. The Stack
grows continuously upward.
D TOP
C C TOP
B B
A A
To implement a stack we need a variable, called top that hold the index of the top element
of the stack and an array to hold the elements of stack.
Memory Representation of Stack
Stack 11 55 44 66 MAXSTK = 8
0 1 2 3 4 5 6 7
Top
class Stack
{
final int MAXSTK = 8;
int Stk[MAXSTK];
top = -1;
}
Stack S = new Stack();
In this case S is a objet of type Stack and S.Stk[top] is the top most element, when S is
initialized S.top equals to -1. After each insertion S.top increased by 1. Therefore to
check whether a Stack is empty or not, one can simply compare S.top with -1. If S.top
= -1 the Stack is empty otherwise the Stack is not Empty. Similarly to check whether a
Stack is full or not , one can simply compare S.top with MAXSTK-1. If S.top =
MAXSTK-1 then the Stack is full otherwise the Stack is not full.
Memory representation of stack using Linked list: Single linked list structure is
sufficient to represent any stack. Here , the data filed is for the item, and link filed is as
usual to point to the next item. Top is a pointer variable which will point to the top of the
stack. To initialize a stack one can simply set its top to NULL.
350
20 1065
850
30 null
1065
Operation on a Stack|: The following operations that are usually performed on a stack
create() : Create an empty stack
isEmpty() : Return true if stack is empty, otherwise return false
isFull() : Return true if stack is full, otherwise return false
peek() : Return the top element of the stack
push(x) : Add an element x to the top of the stack
pop() : Delete the top element from the stack
display() : display the stack elements
void Create(Stack s)
{
S=NULL;
}
IsEmpty( ):
int IsEmpty(Stack s)
{
if(s=NUL)
return(1);
Else
return(0);
}
Infix Notation:- We write expression in infix notation, e.g. a - b + c, where operators are
used in-between operands. It is easy for us humans to read, write, and speak in infix
notation but the same does not go well with computing devices. An algorithm to process
infix notation could be difficult and costly in terms of time and space consumption.
Prefix Notation:- In this 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.
Postfix Notation:- This notation style is known as Reversed Polish Notation. In this
notation style, the operator is postfixed to the operands i.e., the operator is written after
the operands. For example, ab+. This is equivalent to its infix notation a + b.
InfixToPostfix(E)
Step-1 : Push( ‘(‘ ) [Push-left parenthese ‘(‘ on to stack ]
Step-2: Repeat Step-3 to Step-20 while stack is not empty
Step-3: set item = E.scan_ch() [scan the symbol from infix expression]
Step-4: if (item = operand)
Step-5 write(item) [write the symbol into the output expression]
Step-6: if (item = ‘(‘ )
Step-7 push(item) [push symbol ‘(‘ to the stack]
Step-8: if (item = operator)
Step-9 x = pop() when
Step-10: if precedence(x) > precedence(item)
Step-11: repeat Step-12 to step-13 while (precedence(x) > precedence(item))
Step-12: x=pop()
Step-13: Write(x)
Step-14: while( precedence(x)=precedence(item))
Step-15: Push(item)
Step-16: if (item = ‘)’ )
Step-17: x = pop()
Step-18: Repeat Step-19 and Step-20 while (x <> ‘(‘ )
Step-19: write(x)
Step-20: x = pop()
Step-21 : Exit
InfixToPrefix(E)
Step-1: Initially add a left parenthesis ‘(‘ at the beginning of the infix expression E.
Step-2: push(s1, ‘)’ )
Step-3: set item = E.ReverseOrderScan_ch()
[scan the symbol from e in right to left manner]
Step-4: Repeat steps-5 to step-21 while stack s1 is not empty
Step-5: if( item = operand )
Step-6: push(s2,item)
Step-7: if item = ‘)’
Step-8: push(s1,item)
Step-9: if (item = operator)
Step-10: set x = pop(s1) when
Step-11: if (precedence(x) > precedence(item) )
A Queue is a liner list of elements, deletions can takes place only at one end called the
FRONT and insertions can takes place only at the other end called the REAR. Queues are
also called First In First Out (FIFO) lists. Since the first element in a queue will be the
first element out of the queue. In other words the order in which elements entered into a
queue is the order in which they leave.
class Queue
{
final int MAXQ = 10;
int front;
int rear;
int items[MAXQ];
}
Queue Q = new Queue();
In this case “Q” is a variable of type Queue during the initialization of Queue FRONT is
set to 0(zero) and REAR is set to -1 (minus one) an each addition of a Queue is the
variable REAR is incremented by 1 , on other hand in each deletion operation the
variable FRONT is incremented by 1.
FRONT = -1 0 1 2 3 4 5 6 7 8 9 MAXQ = 10
REAR = -1
Q=
Empty Queue
0 1 2 3 4 5 6 7 8 9
Q= A B C
FRONT REAR
A, B and then C inserted
Q= B C
Class link
{
int data=d;
link next=null;
link front=null;
link rear=null;
}
link que= new link();
FORNT REAR
null 3025 35 1026 15 4365 45 235 80 null
Under the list representation of Q a special attention is required when the last element
is removed from a queue. In that case, REAR must also set to NULL, since in an empty
queue both FRONT and REAR must be NULL.
AbstractDataType queue
{
Instances:
ordered list of elements; one end is called the front; the other is the rear;
isEmpty(Queue Q)
{
if( front == -1)
print “Queue is empty”
else
print “Queue is not empty”
}
void delete(Queue Q)
{
if(isEmpty(Q))
{
Print “Queue is Empty”
exit(0);
}
Else
Item = Q[front];
Q.front = Q.front+1;
}
void display(Queue Q)
{
for(i=Q.front;Q.front<Q.rear;i++)
print Q.item[i];
}
We can represent a queue as a linked list. In a queue data is deleted from the front end
and inserted at the rear end. We can perform similar operations on the two ends of a
list. We use two pointers front and rear for our linked queue implementation.
Create( ):
Class link
{
Int data = d;
Link next = null;
Link front = null;
Link frear = null;
}
Link que = new link( );
IsEmpty( ):
Insert( item ):
Delete( ):
Int Delete(queue q)
{
Int x;
If ( IsEmpty(q) )
{
Print “ queue is empty”
Exit(0);
}
Temp = q front;
X = temp data;
q front = temp next;
if (q front == null)
qrear = nu;;;
free(temp);
return(x);
}
Display( ):
void displayQ()
{
temp = front;
printf("\n\n\n\t\t Elements in the Queue are: ");
while(temp != NULL )
{
printf("%5d ", temp -> data);
temp = temp -> next;
}
}
Here we need two variables front and rear to keep track of the elements to be deleted and
inserted and therefore it maintains the unique characteristics of the queue
Front will always be pointing to the first element front = -1 will indicate that the
queue is empty.
Each time a new element is inserted into the queue the rear is incremented by 1 i.
rear = rear + 1.
Each time a element is deleted from the queue the value of front is incremented
by 1 . i.e front = front + 1
If front = rear then queue contains only one element (except front = rear = -1 )
Create(Cqueue q)
{
q.front = -1;
q.rear = -1;
}
int IsEmpty(Cqueue q)
{
if(q.front == -1)
return( true)
else
return(false)
}
int isFull(Cqueue q)
{
if(q.front == (q.rear+1) % MAXQ)
return(true)
else
return(false)
}
Insertion: When an item is inserted into the circular queue we have to write
rear = rear + 1 % MAXQ
void insert(Cqueue q, int x)
{
if(isFull(q))
{
print “queue is full”
return
}
if(q.front == -1)
{
q.front = 0
q.rear=0
}
else
q.rear = (q.rear + 1) % ,MAXQ
q.items[q.rear]=x
}
Deletion: Whenever an element is deleted from the queue, the value of front is increased
by 1 i.e front = (front+1) % MAXQ. If front == -1, then queue is empty.
int Delete(Cqueue q)
{
int x;
if(isEmpty(q))
Q. What is Deque? What are the different techniques used to represent Deque?
Explain (2017)
A deque is a linear list in which elements can be added or removed at either end but not
in the middle. The term deque is a contraction of the name double-ended queue.
Front Rear
Deletion
Insertion 15 25 35 ……. 75 95 85
These are two variations of a deque. These two variations are due to the restrictions put to
perform either the insertions or deletions only at one end. They are
i. Input-restricted deque
ii. Output restricted deque
Input restricted and output restricted deques are intermediate between a deque and a
queue.
Input-restricted deque:
Front Rear
-- insertion
deletion deletion
Specifically, an input restricted deque is a deque which allows insertions at only one
end of the list but allows deletions at both end of the list.
Output-restricted deque:
Front Rear
Insertion insertion
deletion - --
An output-restricted deque is a deque which allows deletion at only one end of the list
but allows insertion at both end of the list.
Operations of Deque:
a deque is implemented using either a circular array or a circular doubly linked list. In
a deque, two pointers are maintained, LEFT and RIGHT, which point to either end of
the deque. The elements in a deque extend from the LEFT end to the RIGHT end and
since it is circular, in a deque of N elements, Nth element of deque is followed by the first
element of the deque.
A priority queue can be represented in many ways. Here, we are discussing multi-
queue implementation of priority queue.
Simulation:- Computer simulation imitates an activity in the real world using a computer
program in order to understand how the real-world process behaves. The advantage of
computer simulation is that you can easily change the program and observe impact of
modifications on the result. Simulation is the use of one system to initiate the behavior of
another system. The main goal of simulation is to help the user or to guess for obtaining
the output of the program after implementing some approaches.
Example: AQ
D
A
DQ BQ
C B
CQ
A Junction of four roads
In order to implement this complex scheduling is to classify the workload according to its
characteristics and to maintain separate process queues. In this we have maintained four
different queues. Each queue has its own scheduling algorithm. Process will be assigned
to their respective queues. CPU will then service the processes as per the priority of the
queues.
Event –Driven
Highest Priority System Process Scheduling
Shortest job
Foreground or
Interactive processes First scheduling
CPU
Round Robin
Background or Batch
processes scheduling
1. When process arrived in higher priority queues is very high, the processes in
lower priority queue may starve for a long time.
2. In multilevel queue scheduling , processes are permanently assigned to a queue
upon entry to the system and process do not move among queues.
The multilevel feedback queue scheduling overcomes both the difficulties. The first
problem could be solved by giving time slices between the queues. Each queue gets a
certain portion of the CPU time. Multilevel feedback queue strategy allows a process to
move between queues. If process uses too much CPU time, it will be moved to next lower
priority queue. A process that waits too long in a lower priority queue may be moved to
a higher priority queue.
Queue-1 8 milliseconds
16 milliseconds
Queue-2
Queue-3 FCFS
A process entering the system is put in queue-1. A process in queue-1 is given a time
quantum of 8 milliseconds. If it does not finish within this time, it is moved to the tail of
queue-2. If queue-1 is empty, the process at the front of queue-2 is given a time quantum
of 16 milliseconds. If it does not complete within this time quantum, it is preempted and
put into queue-3. Processes in queue-3 are served on a first-come-first-serve(FCFS) basis,
only when queue-1 and queue-2 are empty.
P1 20
P2 7
P3 9
P1 P2 P3 P1 P2 P3 P1 P1
0 5 10 15 20 22 26 31 36
The advantages of this kind of scheduling is reducing the average turn around time.
Advantages of Queues:
Circular Queues: Queues can lead to empty spaces in the data structure, since a
bigger array is needed than the total number of pieces of data. However, programmers
can use circular queues to use the empty space.
Speed: Data queues are a fast method of inter-process communication. Data queues
free up jobs from performing some work, which can lead to a better response time
and an overall improvement in system performance.
Multiple Jobs: some jobs have performance restraints and cannot handle all the
entries, so the data entries are spread out across multiple jobs. For example, only one
customer service representative can help a customer at a time, so the queue can
spread customer service requests among the representatives, for quicker processing.
Limitations of Queues:
You cannot use Queue for LIFO operations nor you can store hierarchical data
in it.
Queues are implemented in two ways: Using Arrays and and using Linked-List.
Each has their own pros and cons.
Array implementation has space limitations and hence limited capacity.
Linked-list implementation solves this problem, but is slow.