Advertisements
Advertisements
प्रश्न
A Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out).
Define a class Queue with the following details:
| Class name | Queue |
| Data member/instance variable: | |
| dat[ ] | array to hold the integer elements |
| cap | stores the maximum capacity of the queue |
| front | to point the index of the front |
| rear | to point the index of the rear |
| Member functions/methods: | |
| Queue(int max) | constructor to initialize the data member cap = max, front = rear = 0 and create the integer array |
| void add_dat(int v) | to add integers from the rear index if possible else display the message(“Queue full”) |
| int pop_dat( ) | to remove and return elements from front, if any, else returns -999 |
| void display() | to display elements of the queue |
Specify the class Queue giving the details of void add_dat(int) and int pop_dat( ). Assume that the other functions have been defined.
The main( ) function and algorithm need NOT be written.
Advertisements
उत्तर
import java.util.*;
class Queue
{
int dat[];
int cap, front, rear;
public Queue( int max)
{
cap=max;
dat = new int[ cap];
front=rear=0;
}
public void add_dat(int v)
{
if(rear=cap-1)
{
System.out.println("Queue full");
return;
}
dat[rear++]=v;
}
public int pop_dat()
{
if(front=rear)
{
System.out.println("Queue empty");
retum -999;
}
else
return dat[front++];
}
public void display()
{
for(int i=front; i<rear; i++)
{
System.out.print(dat[i]+" ");
}
}
}APPEARS IN
संबंधित प्रश्न
---data structure does not require contiguous memory allocation
(i) Array
(ii) String
(iii) Pointer Array
(iv) Linked List
What is Data Structure?
With suitable example explain how tree can be represented in Memory?
What is a Record ? How it is represented in Memory ?
Explain with a flowchart the following control structure:
Sequence Logic
Explain with a flowchart the following control structure:
Iteration Logic
Define the following term with reference to Tree:
Root
Define the following term with reference to Tree:
Leaf
Define the following terms with reference to Tree : Sibling
What is Array?
Write an algorithm for Traversing Linear Array.
Define Array and Pointer Array in the data structure.
Explain Linear data Structure and Non-linear Data Structure.
Define Group Item
Define Entity.
___________ is very useful in situation when data is to be stored and retrieved in reverse order.
Explain pointer Array with example.
State the principle by which the stack data structure works.
Differentiate between a stack and a queue.
