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

Unit2 Class 2

A circular queue is an extended version of a regular queue where the last element connects to the first element, forming a circular structure. It uses two pointers, front and rear, to track the first and last elements. Elements can be added by incrementing the rear pointer and removed by incrementing the front pointer.

Uploaded by

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

Unit2 Class 2

A circular queue is an extended version of a regular queue where the last element connects to the first element, forming a circular structure. It uses two pointers, front and rear, to track the first and last elements. Elements can be added by incrementing the rear pointer and removed by incrementing the front pointer.

Uploaded by

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

CIRCULAR QUEUE:

 A circular queue is the extended version of a regular


queue where the last element is connected to the first
element.

 Thus forming a circle-like structure.

The circular queue solves the major limitation of the normal


queue.

 Front variable points one position counterclockwise from


the location of the front element in the queue but rear is
unchanged.
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

 putting items in the queue is called enqueue, and removing


items from the queue is called dequeue.
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


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
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.
 When the array is viewed as a circle, each array position has a
next and previous position

 The position next to position MAX_QUEUE_SIZE-1 is 0 and the


position that precedes 0 is MAX_QUEUE_SIZE-1

 When the queue rear is at MAX_QUEUE_SIZE-1, the next


element is put in position 0
 To work with a circular queue, move the variable front and
rear from the current position to the next position

 This can be done using the code as

If(rear== MAX_QUEUE_SIZE-1) rear=0;


else
rear++;
 Using the modulus operator, which computes remainder, the
code is equivalent to

(rear +1) % MAX_QUEUE_SIZE


EVALUATION OF EXPRESSIONS
EXPRESSIONS
 The representation and evaluation of expression is
of great interest to computer Scientist.

 Programmers write complex expressions such as


((rear+1==front) || ((rear==MAX_QUEUE_SIZE -
1)&& !front)) or

 Complex assignment statement

X=a/b-c+d*e-a*c
 If the user consider the expression and statement it
contains operators (== ,+,-,||,&&,!,/) and
operands(rear , front, MAX_QUEUE_SIZE)

 The first problem with understanding the


expressions or statements is figuring out the order
in which the expressions are performed

 Eg assume for the assignment statement


a=4
b=c=2
d=e=3
 If the user need to find the value of X

 Which one is the correct answer

 Most of them pick the first answer because division


is carried out before subtraction and multiplication
before addition
 With any programming language, there is a precedence
hierarchy that determines the order in which the operator is
evaluated.
 The operators with higher precedence are evaluated first
Evaluating Postfix Expression
 The standard way of writing expression is known as infix
notation because, it is where the binary operator is placed
between its operands

 Infix notation is the most common way of writing


expressions, it is not the one used by compilers to evaluate
expressions

 Compilers uses a parenthesis-free notation referred as postfix


 Before writing a function that translates expressions from infix to
postfix, use easier task of evaluating expression

 This evaluation process is much simpler than the evaluation of


infix expression because there are no parentheses to consider

 To evaluate an expression make a single left-to-right scan of it

 Place the operands on a stack until operator is found

 Then remove , from the stack , the correct number of operands


for the operator , perform the operation and place the result
back to stack

 Continue this task until end of expression is reached

 Remove the answer from top of stack


 When the input is nine character string as 6 2/3-4 2*+
 An algorithm for producing a postfix expression from an infix
one is as follows
 This algorithm works well when done by hand

 It is inefficient on a computer because it requires


two passes

 The first pass reads the expression and parenthesizes

 The second pass removers the operators

 The order of operand is same in infix and postfix

 Hence the postfix equivalent can be obtained by


scanning the infix expression left-to-right
 During scan operands are passed to the output
expression as they are encountered

 The order in which the operators are output depends


on their precedence

 The higher precedence operator are


output(executed) first

 A stack is one way to perform this task.

 But removing operators correctly is problematic


 Consider a simple expression a+b*c, the post fix
notation is abc*+

 The operands are output immediately, but the


operators need to be reversed

 In general operators with higher precedence must


be output before those with lower precedence

 Hence stack operators as long as the precedence of


the operator at the top of the stack is less than the
precedence of the incoming operator.
 In this example unstacking occurs when end of
sting is reached

 At this point two operators are removed

 Since the operator with higher precedence is on the


top of the stack, it is removed first
 Parentheses makes the translation process more
difficult because the equivalent post fix expression
will be parenthesis free

 Consider a*(b+c)*d which yield abc+*d in post


fix

 In this example stack the operators until the right


parenthesis

 Unstack until the corresponding left parenthesis is


reached

 Then delete the left parenthesis from the stack


 The right parenthesis is never put on the stack,
this leaves only *d in infix expression.

 Since two multiplication have equal


precedence, one is output before the d, the
second is placed on the stack and removed
after d is output

 Inexpressions, left parenthesis complicates


because it behaves like a low precedence
operator when it is on stack and high
precedence operator when it is not
 It is placed in the stack whenever it is found in
the expression

 But it is unstacked only when its matching


right parenthesis is found

 There are two type of precedence


1. in-stack precedence(isp)
2. Incoming precedence(icp)
Function to evaluate a Postfix Expression
LINKED LISTS
 A linked-list is a sequence of data structures which

are connected together via links.

 Linked List is a sequence of links which contains

items.

 Each link contains a connection to another link.

 Linked list is the second most used data structure after


array.
Following are important terms to understand the
concepts of Linked List

•Link − Each Link of a linked list can store a data


called an element.

•Next − Each Link of a linked list contain a link to


next link called Next.

•LinkedList − A LinkedList contains the connection


link to the first Link called First.
• Consider the 3 letter English words ending with AT

• (BAT, CAT, EAT, FAT, HAT, JAT, LAT, MAT,


OAT, PAT, RAT, SAT, TAT, VAT, WAT)
• To insert GAT it is required to move elements one
location higher or lower.
• We must move either HAT, JAT….. or BAT, CAT
etc.
• Excessive data movements are required for insertion
and deletion.
• An elegant solution to this problem of data
movement in sequential representation is
achieved using linked representation.
• The elements of the list are stored in a one
dimensional array called “Data”.
• A second array LINK is added to show the array in
any order.
• For any i, DATA[i] and LINK[i] comprise a node.
 Non Sequential representation of list representation

DATA LINK
HAT 15

CAT 4
EAT 9

GAT 1
WAT 0
BAT 3
FAT 6

VAT 7
• It is usual to draw linked list as an ordered sequence
of nodes with links being represented by arrows.
• The arrows indicates that the

➢ Nodes do not actually reside in sequential locations.


➢ Actual locations of nodes are immaterial(meaning
less).
struct Node
{
int data;
struct Node *next;
};
• In a single linked list each node has exactly one
pointer field.
• A chain is a single Linked list that is comprised of
zero or more nodes.
 To insert a node into the linked list we need to do
following steps.
For example the data item D has to be inserted
between C and E
(i)get a node which is currently unused; let its address
be X;
(i)set the DATA field of this node to D;
(ii) set the LINK field of X to point to the node after C which
contains E;
(iv)set the LINK field of the node containing C to X.
 To delete a node from linked list, we need to do
following steps.
1) Find previous node of the node to be deleted.
2)Change the next of previous node.
3)Free memory for the node to be deleted.

 To delete an element from a single linked list.


 To delete C from the list.
 Find the element that immediately precedes C which is B
and set link to the position of D which is after C.
LINKED STACKS AND QUEUES:
 A linked stack is a linear list of elements commonly
implemented as a singly linked list whose start pointer
performs the role of the top pointer of a stack
A linked queue is also a linear list of elements commonly
implemented as a singly linked list but with two pointers
viz., FRONT and REAR.

 The start pointer of the singly linked list plays the role of
FRONT while the pointer to the last node is set to play the
role of REAR.
 When several stacks and queues coexisted, there was no
efficient way to represent them sequentially.
 We can easily add or delete a node from the top of the stack.
 We can easily add or delete a node to the rear of the queue
and add or delete a node at the front
Algorithm: Push item ITEM into a linked stack S with
top pointer TOP procedure PUSH_LINKSTACK (TOP,
ITEM)

/* Insert ITEM into stack */


Call GETNODE(X)
DATA(X) = ITEM /*frame node for ITEM */

LINK(X) = TOP /* insert node X into stack */

TOP = X /* reset TOP pointer */

end PUSH_LINKSTACK.
Algorithm: Pop from a linked stack S and output the
element through ITEM
procedure POP_LINKSTACK(TOP, ITEM)
/
* pop element from stack and set ITEM to the element
*/
if (TOP = 0) then call LINKSTACK_EMPTY
/* check if linked stack is empty */
else { TEMP = TOP ITEM = DATA(TOP) TOP =

LINK(TOP)
}
call RETURN(TEMP) ;
end POP_LINKSTACK.
Algorithm: Enqueue an ITEM into a linked list queue Q
procedure INSERT_LINKQUEUE(FRONT,REAR,ITEM)
Call GETNODE(X);
DATA(X)= ITEM;
LINK(X)= NIL;
/* Node with ITEM is ready to be inserted into Q */
if (Queue = 0) then
Queue = FRONT = REAR = X;
/* If Q is empty then ITEM is the first element in
the queue Q
else {LINK(REAR) = X; REAR = X
}
end INSERT_LINKQUEUE.
Algorithm: Dequeue an element from the
linked queue Q procedure
DELETE_LINKQUEUE (FRONT,ITEM)
if (FRONT = 0) then call LINKQUEUE_EMPTY;
/* Test condition to avoid deletion in an
empty queue */
else {TEMP = FRONT; ITEM
= DATA (TEMP); FRONT =
LINK (TEMP);
}
call RETURN (TEMP); /* return the
node TEMP to the free pool */
end DELETE_LINKQUEUE

You might also like