An algorithm to insert a new element in the queue and also show Enqueue & Dequeue operation.

###An algorithm to insert a new element in the queue and also show Enqueue & Dequeue operation.
Flowchart given below: 

Enqueue Operation.
Dequeue Operation.









Now we will implement this algorithm into a program.
Source Code in C++:
#include <iostream>
using namespace std;
#define size 5
void enqueue(int);
void dequeue();
void display();
int a[size],i,j,front=-1,rear=-1;
int main(int argc, char const *argv[]) {
  dequeue();
  enqueue(3);
  enqueue(11);
  enqueue(111);
  enqueue(212);
  enqueue(112);
  enqueue(10);
  display();
  dequeue();
  display();
  return 0;
}

void enqueue(int value){
  if (rear==size-1) {
    std::cout << "Queue is full." << '\n';
  }else{
    if (front==-1)
      front=0;
      rear++;
      a[rear]=value;
      std::cout << "Enqued element: " <<value<<endl;

  }
}
void dequeue(){
  if(front==-1){
    std::cout << "Dequeue is empty." << '\n';
  }else{
    std::cout << "Dequed element:"<<a[front]<< '\n';
    front=front+1;
    if(front>rear){
      front=rear=-1;
    }
  }
}
void display(){
  if(rear==-1){
    std::cout << "Queue is empty." << '\n';
  }else{
    std::cout << "Elements in the queue: " << '\n';
    for(int i=front;i<=rear;i++){
      std::cout << a[i] << '\n';
    }
  }
}


Output:
Dequeue is empty.
Enqued element: 3
Enqued element: 11
Enqued element: 111
Enqued element: 212
Enqued element: 112
Queue is full.
Elements in the queue:
3
11
111
212
112
Dequed element:3
Elements in the queue:
11
111
212
112

Press any key to continue . . .



Explanation:
What is a queue?
•It is an ordered group of homogeneous items of elements.
•Queues have two ends:
–Elements are added at one end called rear.
–Elements are removed from the other end called front.
•The element added first is also removed first (FIFO: First In, First Out).

Queue Specification
•Definitions: (provided by the user)
–MAX_ITEMS: Max number of items that might be on the queue
–ItemType: Data type of the items on the queue
•Operations
–Enqueue (ItemType newItem)
–Dequeue ()

Queue Operation 
Enqueue (ItemType newItem)
•Function: Adds newItem to the rear of the queue.
•Preconditions: Queue has been initialized and is not full.
•Postconditions: newItem is at rear of queue.

Dequeue (ItemType& item)
•Function: Removes front item from queue and returns it in item.
•Preconditions: Queue has been initialized and is not empty.
•Postconditions: Front element has been removed from queue and item is a copy of removed element.

Queue Operation



Implementation issues
•Implement the queue as a circular structure.
•How do we know if a queue is full or empty?
•Initialization of front and rear.
•Testing for a full or empty queue.




Figure to understand Queue:






Read out the description carefully and then go through this figure.



                                                    *****Happy Coding😊😊*****


























































No comments

Powered by Blogger.