Deletion in a Queue in C++

Deletion in a Queue 

 

Queue is a data structure where you can insert elements in a Fast in First out manner, so you can store values in the order in which they are coming. In this basic linear data structure, pushing elements are called enqueue, and popping them out from the front end is called dequeue. Here is an article about how to perform deletions in a queue in C++, i.e. Dequeue.

Deletion in a queue in C++

What is Queue?

 
Queue is a standard linear user defined data structure that allows you to pre-store  elements in the specific order it gets, as get a first in first out or FIFO feature in that.

The Agorithm to Delete from Queue : Dequeue

Here goes the following algorithm that tells us how to dequeue / delete elements from a queue.

  • Let’s say the queue is actually built in a linked list. Here we have used a predefined linkedlist that us available inside C++ Standard template library (List in STL), and made a class where there is a member variable of that type.
  • Now Queue is first in first out. So the popping out or deletion of elements will be dine in the front of the list.  You can obtain that differently in C too.
  • That means, if the queue is empty, we have to show that the queue is empty and you cannot print anything from an empty queue, because it is empty of elements.
  • Otherwise we will simply pop the element from the front of the list and deallocate the memory from there (If it is not a static queue).
Deletion in a Queue in C – 1

The code to implement this:

Run
#include<bits/stdc++.h>
using namespace std;

class Queue
{
public:
  list < int >L;
  void Push (int i)
  {
    cout << "Pushing the element : " << (i) << endl;
    L.push_back (i);
  }
  int pop ()
  {
    if (L.empty ())
      {
	cout << "The queue is empty" << endl;
      }
    int a = L.front ();
    L.pop_front ();
    cout << "Popping the last element : " << a << endl;
    return a;
  }
  void Show ()
  {
    cout << "The present queue is:" << endl;
  for (auto i:L)
      cout << i << " ";
    cout << endl;
  }
};

int
main ()
{
  Queue q;
  q.Push (2);
  q.Push (9);
  q.Push (3);
  q.Show ();
  q.pop ();
  q.Push (5);
  q.pop ();
  q.pop ();
  q.Show ();
}

Output:

Pushing the element : 2
Pushing the element : 9
Pushing the element : 3
The present queue is:
2 9 3
Popping the last element : 2
Pushing the element : 5
Popping the last element : 9
Popping the last element : 3
The present queue is: 5 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java