Queue


 

Queue:

Introduction:

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e.

Basic Operations:

Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues.

Ø enqueue()        add (store) an item to the queue.

Ø dequeue()         remove (access) an item from the queue

Few more functions are required to make the above-mentioned queue operation efficient.

 These are

Ø  peek()              Gets the element at the front of the queue without removing it.

Ø  isfull()              Checks if the queue is full.

Ø  isempty()        Checks if the queue is empty.

Queue using Linklist:

Code:

#include <iostream>

using namespace std;

 

struct node {

    node* link;

    int data;

};

 

class queue {

private:

    node* front;

    node* rear;

 

public:

    queue() {

        front = NULL;

        rear = NULL;

    }

 

    void push(int);

    void pop();

    void show();

};

 

 

void queue::push(int x) {

    node* p = new node;

    p->data = x;

 

    if (rear==NULL) {

        front = p;

        rear = p;

    } else {

        rear->link = p;

        p->link = front;

        rear = p;

    }

}

 

void queue::pop() {

    if (rear==NULL) {

        cout << "Queue is Empty" << endl;

    } else {

        node* p = front;

        int temp = front->data;

 

        if (front == rear) {

            front = NULL;

            rear = NULL;

        } else {

            front = front->link;

        }

 

        cout << "Popped value is: " << temp << endl;

        delete p;

    }

}

 

void queue::show() {

    if (rear==NULL) {

        cout << "Queue is Empty" << endl;

        return;

    }

 

    node* p = front;

 

    while(p!=rear){

            cout<<p->data<<" ";

            p=p->link;

            }

 

    cout << p->data<<endl;

}

 

int main() {

    queue q1;

    int choice, x;

 

    do {

        cout << "1. Push an element" << endl;

        cout << "2. Pop an element" << endl;

        cout << "3. Show elements" << endl;

        cout << "4. Exit" << endl;

        cout << "Enter the choice: ";

        cin >> choice;

 

        switch (choice) {

            case 1:

                cout << "Enter the element you want to enter: ";

                cin >> x;

                q1.push(x);

                break;

            case 2:

                q1.pop();

                break;

            case 3:

                q1.show();

                break;

            case 4:

                return 0;

        }

    } while (true);

 

    return 0;

}

Share this:

ABOUT THE AUTHOR

Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible

0 comments:

Post a Comment