Question

Queue data structure: please answer these two question properly Q(1) What are their respective logical and...

Queue data structure: please answer these two question properly

Q(1) What are their respective logical and physical / storage structures .Q(2) At least one application example combining data structure and algorithm should be given.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:
1.)

logical and physical structure for Queue are mentioned below:

  • Array : Queue is mostly uses Array as its underlying logical and physical storage.
  • Linked List : Sometime Linked List is also used as underlying physical storage if space is constraint.

2.)
An example where queue is used is First Come First Serve OS scheduling algorithm.
The sample code using Queue as data structure and output is given below:

// Java program for array
// implementation of queue

// A class to represent a queue
class Queue {
   int front, rear, size;
   int capacity;
   int array[];

   public Queue(int cap)
   {
       this.capacity = cap;
       front = this.size = 0;
       rear = cap - 1;
       array = new int[this.capacity];
   }

   // Queue can be full when size becomes
   // equal to the capacity
   boolean isFull(Queue q)
   {
       return (q.size == q.capacity);
   }

   // Queue is empty when size is 0
   boolean isEmpty(Queue q)
   {
       return (q.size == 0);
   }

   // Method to add an item to the queue.
   // It changes rear and size
   void enqueue(int item)
   {
       if (isFull(this))
           return;
       this.rear = (this.rear + 1)
                   % this.capacity;
       this.array[this.rear] = item;
       this.size = this.size + 1;
       System.out.println(item
                       + " enqueued to process queue");
   }

   // Method to remove an item from queue.
   // It changes front and size
   int dequeue()
   {
       if (isEmpty(this))
           return Integer.MIN_VALUE;

       int item = this.array[this.front];
       this.front = (this.front + 1)
                   % this.capacity;
       this.size = this.size - 1;
       return item;
   }

   // Method to get front of queue
   int front()
   {
       if (isEmpty(this))
           return Integer.MIN_VALUE;

       return this.array[this.front];
   }

   // Method to get rear of queue
   int rear()
   {
       if (isEmpty(this))
           return Integer.MIN_VALUE;

       return this.array[this.rear];
   }
}

/**
* Driver class to execute FCFS in cpu scheduling of process
* */
public class Main {
   public static void main(String[] args)
   {
       Queue processQueue = new Queue(1000);

       processQueue.enqueue(10);
       processQueue.enqueue(20);
       processQueue.enqueue(30);
       processQueue.enqueue(40);

       System.out.println(processQueue.dequeue() + " is first process to be executed from queue\n");

       System.out.println("Front process is " + processQueue.front());

       System.out.println("Rear process is " + processQueue.rear());
   }
}


Run Debug Stop Share Save {} Beautify Language Java 6 } Main.java 1 // Java program for array 2 // implementation of queue 3return; this.rear = (this.rear + 1) % this.capacity; this.array[this.rear] item; this.size = this.size + 1; System.out.printl} * */ 71 if (isEmpty(this)) 72 return Integer.MIN_VALUE; 73 74 return this.array[this.rear]; 75 76 } 77 78. /** 79 * Driver
OUTPUT=>
input 10 enqueued to process queue 20 enqueued to process queue 30 enqueued to process queue 40 enqueued to process queue 10

(plz give me a thums up...if my answer helped you and if any suggestion plz comment, Yr thums up boost me)

Add a comment
Know the answer?
Add Answer to:
Queue data structure: please answer these two question properly Q(1) What are their respective logical and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT