0% found this document useful (0 votes)
87 views34 pages

Circular Queue Operations Explained

The document discusses various data structure concepts like queues, algorithms, abstract data types, and stacks. It provides examples of insert and delete operations in a circular queue and describes how to convert infix expressions to postfix notation using stacks. The document also explains how recursion and stacks can be used to compute factorials of a number.

Uploaded by

jaydave422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views34 pages

Circular Queue Operations Explained

The document discusses various data structure concepts like queues, algorithms, abstract data types, and stacks. It provides examples of insert and delete operations in a circular queue and describes how to convert infix expressions to postfix notation using stacks. The document also explains how recursion and stacks can be used to compute factorials of a number.

Uploaded by

jaydave422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Insert operation in circular queue

If (tail+1) % size = head


Then queue is full.
Else
Add item at location tail
Set tail=(tail+1)%size
End if
Delete operation in circular queue

If head==tail then
Queue is full
Else
remove item at location head
set head = (head +1)%size
End if
What is Data ?
• Data in particular problem consist of a set
of elementary items or atoms.

• An atom usually consist of single elements


such as integers, bit, character

• The choice of data is necessary and key


step in defining and solving problem.
What is Data Structure ?

• The possible ways in which data items


are logically related define different
data structure.

• By choosing a particular structure for


data item certain item becomes
immediate neighbor, while other items
are related in weaker sense.
Type of Data Structure
• Linear Data Structure
– Data Structure is said to be linear
if its elements forms List

• Non Linear Data Structure


Data Structure is said be non
linear if data is not organized as list
physically
What is Algorithm ?
• An algorithm is ``a...step-by-step
procedure for accomplishing some
end.
• we can run the program and observe
its behavior.
• This is not likely to be very useful or
informative in the general case.
• If we run a particular program on a
particular computer with a particular
set of inputs, then all know is the
behavior of the program in a single
instance.
Parameter for analysis of algorithm
• determine the running time of a
program as a function of its inputs;
• determine the total or maximum
memory space needed for program
data;
• determine the total size of the
program code;
• determine whether the program
correctly computes the desired result
Parameter continue…
• determine the complexity of the
program--e.g., how easy is it to read,
understand, and modify; and,
• determine the robustness of the
program--e.g., how well does it deal
with unexpected or erroneous inputs?
What is Abstract Data Types ?
• A useful tool for specifying the logical
properties of data types if know as
abstract data type.
• Data type is collection of values and
set of operation performed on that.
• The term ADT refers to basic
mathematical concept that define data
types.
Array as ADT
• We assume the function type(arg),
which return types of its arg.
• We are not much concerned with
implementation but more with
specification
• Let ARRTYPE (ub,eltype) denote the
ADT correspondence to c array type
eltype array(ub)
• Here ud and eltype is parameter
Array as ADT Continue…
abstract typedef <eltype,ub> ARRTYPE(ub,eletype)
condition type(ub) = int;
abstract eltype extract(a,i)
ARRTYPE(ub,eltype) a;
int I;
precondition 0<=i>=b;
postcondition extract ==a;
Abstract store(a,i,elt)
ARRTYPE(ub,eltype) a;
int i;
Array as ADT continue…
eltype elt;
precondition 0<=i>=b;
postcondition extract ==a;
Evolution of Postfix expression
1. [initialize stack]
top – 1
2. [Process all data in expression]
Repeat through step while next!=‘\0’
3. [Determine type]
if next is char then
call push(s,a,top)
Else
right= pop(s,top)
left= pop(s,top)
result= left Next right
Evolution continue …
Call push(s,result,top)
4. [Get next character ]
next=nextchar()
5. [Get data from top which is result]
pop(s,top)
Infix to postfix (with out parenthesis)
• Given input string infix represent an
infix expression whose single
character symbol have precedence
values given below. S represent a
stack and string function next returns
next character from string. This
converts infix expression to polish or
postfix.
Precedence and rank table
Symbol Precedence Rank
+ - 1 -1
*, / 2 -1
a,b,c etc 3 1
# 0 -
Steps
1. [initialize the stack ]
top=1
2. [initialize output string and rank]
polish=‘ ’
rank=0
3. [get first input symbol]
Next=nextchar(infix)
4. [translate the infix expression]
5. [Remove symbol from stack whose
precedence is >= stack precedence]
Steps continue …
Repeat while (f(next) <= f(s[top])
temp=pop(s,top)
polish=polish+temp
rank=rank+r(temp)
if rank < 1 then
write ‘Invalid’
exit
6. [Push current input symbol to stack]
push(s,top,next)
next=nextchar(infix)
Steps continue …
7 [Remove remaining elements from
stack]
repeat while s[top] != ‘#’
temp=pop(s,top)
polish=polish+temp
rank=rank+r(temp)
if rank < 1 then
write ‘Invalid’
exit
8. [ Is expression valid ?]
if rank=1 then write ‘valid’
Example
Character Stack Polish Rank
#
a #a
+ #+ a 1
b #+b a 1
* #+* ab 2
c #+*c ab 2
- #- abc*+ 1
d #-d abc*+ 1
/ #-/ abc*+d 2
Example
e #-/e abc*+d 2
* #-* abc*+de/ 2
h #-*h abc*+de/ 2
# abc*+de/h*- 1
Infix to postfix (with parenthesis)
1 [initialize stack]
Top=1
2 [initialize output string and rank count]
polish= ‘ ‘
rank=1
3 [get first input symbol]
next=nextchar(infix)
4. [translate the infix expression]
repeat through step 7 while next != ‘ ’
Procedure continue …
5 [Remove symbol with greater precedence
from stack ]
if top<1 then
write ‘Invalid’
exit
else
repeat while f(next) < g(S[top])
temp=pop(s,top)
polish=polish+temp
rank=rank+r(temp)
if rank < 1 then
write ‘Invalid’
exit
Procedure continue …
6. [ Are they matching parenthesis ? ]
If f(next) != g(s[top])
call push(s,top,next)
Else
pop(s,top)
7. [Get next input symbol]
next=nextchar(infix)
8. [Is expression valid ?]
if top!=0 or rank != 1 then
write ‘Invalid’
else
write ‘valid’
Precedence table
Symbol Input Stack Rank
+, - 1 2 -1
*, / 3 4 -1
^ 6 5 -1
Variable 7 8 1
( 9 0 -
) 0 - -
Stack application example
Given integer n this procedure computes n
!. Stack a is used to store activation
record associated with each recursive
call. Each record contains the current
value of N and the current return
address. Temp_Rec is placed on stack
A, copies Pram and Address are push to
A and assign to N and Ret_Addr
Procedure
1 [Save N and return address]
call push (a, top, temp_rec)
2 [Is the base criterion found ?]
if n=0 then
factorial = 1
go to step 4
else
parm=n-1
address = step 3
3. [calculate N ! ]
Procedure continue …

Factorial = n * factorial
4. [Restore previous N and return
address]
temp_rec=pop(a,top)
go to address
Using Example
Level description Stack
Push (a,0, 2
Enter level (2,mainaddress) Main
1 N !=0 Address
Parm=1 addr= step 3
Using Example
Level description Stack
Push (a,1,(1,step 3) 2 1
Enter level N !=0 Main Step
2 Parm=0 addr= step 3 Address 3
Using Example
Level description Stack
Push (a,2,(0,step 3) 2 1 0
Enter level N =0 Main Step Step
3 Factorial =1 Address 3
3
Using Example
Level description Stack
Pop (a,3) 2 1
Step Go to step 3 Main Step
4 Address 3
Using Example
Level description Stack
Factorial – 1 *1 2
Return to Pop (a,2) Main
Level 2 Go to step 3 Address
Using Example
Level description Stack
Factorial – 2 *1
Return to Pop (a,1)
Level 1 Go to main address

You might also like