Using array to execute the queue structure, Data Structure & Algorithms

Q. Using array to execute the queue structure, write down an algorithm/program to

(i) Insert an element in the queue.

(ii) Delete an element from the queue.                                                        

 

Ans.

(i) Algorithm to Insert an element in the given queue.

QINSERT( QUEUE, N, FRONT, REAR, ITEM)

This method inserts an elements ITEM into a queue.

1. [QUEUE already filled ?]

if FRONT =1 and REAR =N or if FRONT = REAR +1 then

write:OVERFLOW, and Return.

2. [Find new value of REAR]

If FRONT : = NULL, then : [QUEUE initially empty]

Set FRONT : = 1 and REAR : = 1, Else if REAR = N then;

Set REAR : = 1

Else:

Set REAR : = REAR + 1 [End of if structure]

3.Set QUEUE [REAR]: = ITEM [This inserts new elements]

4. Return

(ii) Algorithm to erase an element from the Queue

QDELETE (QUEUE, N, FRONT, REAR, ITEM)

This method deletes an element from a Queue and assign it to the variable ITEM.

1.  [Queue already Empty?]

If  FRONT:= NULL, then write UNDERFLOW and Return

2.  Set ITEM:=QUEUE[FRONT]

3.  [Find new value of FRONT]

If FRONT=REAR, THEN[Queue has only one element to start.]

Set FRONT:=NULL and REAR:=NULL Else  if      FRONT=N then:

Set FRONT:=1

Else:

Set FRONT:=FRONT+1 [End of If structure]

4.  Return.

Posted Date: 7/13/2012 1:24:12 AM | Location : United States







Related Discussions:- Using array to execute the queue structure, Assignment Help, Ask Question on Using array to execute the queue structure, Get Answer, Expert's Help, Using array to execute the queue structure Discussions

Write discussion on Using array to execute the queue structure
Your posts are moderated
Related Questions
What is bubble sort? Bubble Sort: The basic idea in bubble sort is to scan the array to be sorted sequentially various times. Every pass puts the largest element in its corr

write an algorithm for stack using array performing the operations as insertion ,deletion , display,isempty,isfull.

1. For the ER diagram you created in assignment, the artefact of the conceptual database design, map the ER model into the relational model according to how it was designed in the

Example: Insertion of a key 33 into a B-Tree (w/split) Step 1: Search first node for key closet to 33. Key 30 was determined. Step 2: Node pointed through key 30, is se

The Linked list is a chain of structures wherein each structure contains data in addition to pointer, which stores the address (link) of the next logical structure in the list.

Consider the file " search_2013 ". This is a text file containingsearch key values; each entry is a particular ID (in the schema given above). You are tosimulate searching over a h

Q. The degree of a node is defined as the number of children it has. Shear show that in any binary tree, the total number of leaves is one more than the number of nodes of degree 2

Write a C++ program with header and source les to store street addresses using the Doubly Linked List ADT. Modify the Node class from Lab Assignment 3 so that it becomes a node in

Hubs - In reality a multiport repeater - Connects stations in a physical star topology - As well may create multiple levels of hierarchy to remove length limitation of 10

Algorithm for insertion of any element into the circular queue: Step-1: If "rear" of the queue is pointing at the last position then go to step-2 or else Step-3 Step-2: make