# Queue Implementation in C++

A stack is LIFO (Last-In First Out) structure. In contrast, a queue is a FIFO (First-In First-Out ) structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. A queue is a linear structure for which items can be only inserted at one end and removed at another end.

A Queue can have following operations:

- Enqueue(X) – Place X at the rear of the queue.
- Dequeue() – Remove the front element and return it.
- Front() – Return front element without removing it.
- IsEmpty() – Return TRUE if queue is empty, FALSE otherwise

## Implementation of Queue

**1. Implementation using Linked List**

Usually Queues are implemented using linked List. Insert works in constant time for either end of a linked list. Remove works in constant time only. Seems best that head of the linked list be the front of the queue so that all removes will be from the front. Inserts will be at the end of the list. Consider the following diagram for implementation of Queue using linked list.

**Implementation in C++ using Linked List
**

int dequeue() { int x = front->get(); Node* p = front; front = front->getNext(); delete p; return x; } void enqueue(int x) { Node* newNode = new Node(); newNode->set(x); newNode->setNext(NULL); rear->setNext(newNode); rear = newNode; } int front() { return front->get(); } int isEmpty() { return ( front == NULL ); }

**Implementation using Array **

**Solution:**allow the queue to “wrap around”. Basic idea is to picture the array as a circular array as show below.

**Implementation in C++ using Arrays**

void enqueue(int x) { rear = (rear+1)%size; array[rear] = x; noElements = noElements+1; } int dequeue() { int x = array[front]; front = (front+1)%size; noElements = noElements-1; return x; } int isFull() { return noElements == size; } int isEmpty() { return noElements == 0; }

## Use of Queues

- Out of the numerous uses of the queues, one of the most useful is simulation.
- A simulation program attempts to model a real-world phenomenon.
- Many popular video games are simulations, e.g., SimCity, FlightSimulator
- Each object and action in the simulation has a counterpart in real world.
- If the simulation is accurate, the result of the program should mirror the results of the real-world event.
- Thus it is possible to understand what occurs in the real-world without actually observing its occurrence.

**Simulation of a Bank**

### Related Posts

- Queues in C++
- Implementing Stack Data Structure in C++
- Data Structures Tutorial using C++
- Insertion Sort: A Graphical Explanation
- Selection Sort: A Graphical Explanation
- Bubble Sort: A Graphical Explanation
- Linked lists in C++
- Implementation of Priority Queue using Heap data structure in C++
- Circular Linked Lists
- Applications of Stack in Data Structures
- Doubly Linked Lists
- Binary Trees and Binary Search Trees

### One Response to *Queue Implementation in C++*

### Leave a Reply Cancel reply

### Popular Posts (last 30 days)

- Applications of Stack in … 1120 view(s)
- Circular Linked Lists 1049 view(s)
- Attendance Management Sys… 917 view(s)
- Simple Currency Converter… 662 view(s)
- Recursive Factorial funct… 582 view(s)
- Implementing Stack Data S… 579 view(s)
- Graph Implementation in C… 535 view(s)
- Finding Minimum, Maximum … 462 view(s)
- GRASP Design Patterns 319 view(s)
- Finding Maximum Number in… 315 view(s)

### Related Links

### Tags

Android C-Sharp C/C++ language Classes Data structures Design Pattern Eclipse Game Development Graphics Design Books HTML iPhone JAVA JAVA GUI MIPS Assembly Mobile Programming Books Object Oriented PDF PHP Programming Programming Books Programming Languages Books Python RaphaelJS REST Source Code Threads Tutorial Web Development Books

i want to ask you,

how about if rear=max, so the count of front, return to front(0)

ex: max = 5. i have queue 1 2 3 4 5. dequeue 1&2. so the result is 3 4 5.

i want to take front(3) return to front(0). front(4) return to front(1). etc.

how the implementation. sorry about my english

thanks