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

Submitted By: Huzaifa Sarfraz. Registration No: S1F18BSCS0014. Submitted To: Prof. Attia Muslim. Class: (Bscs Iv) - Subject: Data Structure

A queue is a linear data structure that follows the FIFO (first in, first out) principle. Elements are added to the rear of the queue and removed from the front. This document presents an implementation of a queue using two stacks. Elements are pushed to the first stack, then all elements are moved to the second stack and back to make the oldest element end up on top. The queue supports enQueue to add and deQueue to remove elements. Applications of queues include printer servers, call centers, interrupt handling, and asynchronous data transfer between processes.

Uploaded by

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

Submitted By: Huzaifa Sarfraz. Registration No: S1F18BSCS0014. Submitted To: Prof. Attia Muslim. Class: (Bscs Iv) - Subject: Data Structure

A queue is a linear data structure that follows the FIFO (first in, first out) principle. Elements are added to the rear of the queue and removed from the front. This document presents an implementation of a queue using two stacks. Elements are pushed to the first stack, then all elements are moved to the second stack and back to make the oldest element end up on top. The queue supports enQueue to add and deQueue to remove elements. Applications of queues include printer servers, call centers, interrupt handling, and asynchronous data transfer between processes.

Uploaded by

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

Submitted by:

Huzaifa Sarfraz.
Registration no:
S1F18BSCS0014.
Submitted to:
PROF. ATTIA MUSLIM.
Class:
(BSCS IV).
Subject:
Data Structure.
QUEUE
DEFINITION:
A Queue is a linear structure which follows a particular order in
which the operations are performed. The order is First In First
Out (FIFO). A good example of a queue is any queue of
consumers for a resource where the consumer that came first is
served first. The difference between stacks and queues is in
removing. In a stack we remove the item the most recently
added; in a queue, we remove the item the least recently added.

IMPLEMENTATION OF QUEUE USING


STACK:
We are given a stack data structure with push and pop
operations, the task is to implement a queue using
instances of stack data structure and operations on them.
(By making enQueue operation costly) This method makes
sure that oldest entered element is always at the top of stack 1,
so that deQueue operation just pops from stack1. To put the
element at top of stack1, stack2 is used.
CODE:
#include <iostream>
using namespace std;
  
struct Queue {
    stack<int> s1, s2;
  
    void enQueue(int x)
    {
        // Move all elements from s1 to s2
        while (!s1.empty()) {
            s2.push(s1.top());
            s1.pop();
        }
  
        // Push item into s1
        s1.push(x);
  
        // Push everything back to s1
        while (!s2.empty()) {
            s1.push(s2.top());
            s2.pop();
        }
    }
  
    // Dequeue an item from the queue
    int deQueue()
    {
        // if first stack is empty
        if (s1.empty()) {
            cout << "Q is Empty";
            exit(0);
        }
  
        // Return top of s1
        int x = s1.top();
        s1.pop();
        return x;
    }
};
  
// Driver code
int main()
{
    Queue q;
    q.enQueue(1);
    q.enQueue(2);
    q.enQueue(3);
  
    cout << q.deQueue() << '\n';
    cout << q.deQueue() << '\n';
    cout << q.deQueue() << '\n';
  
    return 0;
}

APPLICATIONS OF QUEUE:
Queue, as the name suggests is used whenever we need to
manage any group of objects in an order in which the first one
coming in, also gets out first while the others wait for their turn,
like in the following scenarios:
1. Serving requests on a single shared resource, like a printer,
CPU task scheduling etc.
2. In real life scenario, Call Center phone systems uses
Queues to hold people calling them in an order, until a
service representative is free.
3. Handling of interrupts in real-time systems. The interrupts
are handled in the same order as they arrive i.e First come
first served.
4. When a resource is shared among multiple consumers.
Examples include CPU scheduling, Disk Scheduling.
5. When data is transferred asynchronously (data not
necessarily received at same rate as sent) between two
processes. Examples include IO Buffers, pipes, file IO, etc.

You might also like