Skip to main content

Queue using array in c++ code

 Hi, guys Queue using array in c++ code given below

#include<iostream>
using namespace std;
int queue [5];
int front=-1;
int rear=-1;
void enqueue(int x) {
    if(rear==5-1) {
        cout<<"overflow condition";
    }
    else if(rear==-1 and front==-1)
    {
        front=rear=0;
        queue[front]=x;
    }
    else {
        rear++;
        queue[rear]=x;
    }
}
void dequeue() {
    if(front==-1 and rear==-1)
    {
        cout<<"underflow cond";
    }
    else if(rear==front)
    {
        front=rear=-1;
    }
    else {
        cout<<"dequeued element is"<<queue[front]<<endl;
        front ++;
    }
}
void display () {

    if(front==-1 && rear==-1) {
        cout<<"list is empty";
    }
    else
    {
        cout<<"display"<<endl;
    }
    while(front!=rear+1) {
        cout<<queue[front];
        front++;
    }

}
main() {
    enqueue(1) ;
    enqueue(2) ;
    enqueue(3) ;
    enqueue(4) ;
    enqueue(5) ;
    dequeue() ;
    dequeue() ;
    display () ;
}


Comments

Popular posts from this blog

Print elements in sorted order from a row_column wise sorted matrix

  #include <iostream> using namespace std;   struct Node {     int val;      int r;      int c; };   void minHeapify(Node harr[], int i, int heap_size) {     int l = i * 2 + 1;     int r = i * 2 + 2;      if(l < heap_size&& r<heap_size && harr[l].val < harr[i].val && harr[r].val < harr[i].val){             Node temp=harr[r];             harr[r]=harr[i];             harr[i]=harr[l];             harr[l]=temp;             minHeapify(harr ,l,heap_size);             minHeapify(harr ,r,heap_size);         }           if (l < heap_size && harr[l].val < harr[i].val){             Node temp=harr[i];                         harr[i]=harr[l];             harr[l]=temp;             minHeapify(harr ,l,heap_size);         } }   void sortedform(int mat[][3], int n) {      int arr[n*n]; /*if row size 3 and column size 3 then the array should store 3*3=9 elements*/     Node harr[n];     for (int i = 0; i < n; i++)         harr[i] = { mat[0][i], 0,

Queue using link list c++ code

 Hi, guys code for queue using link list in c++ language #include<iostream> using namespace std; struct node {     int data;     node *next; }; struct node *front=0, *rear=0; void enqueue(int x) {     node *newnode;     newnode=new node;     newnode->next=0;     newnode->data=x;     if(front==0 and rear==0)     {         front=rear=newnode;     }     else     {         rear->next=newnode;         rear=newnode;     } } void display () {     node *temp=front;     while(temp!=0)     {         cout<<temp->data<<endl;         temp=temp->next;     } } void dequeue() {     node *temp=front;     front=front->next;     delete (temp) ; } main() {     enqueue(3) ;     enqueue (8) ;     enqueue(7) ;     dequeue () ;     display () ; }

Create and display Doubly circular link list in c++ code

 Hi, guys code for creating and displaying doubly circular link list using c++ given below #include <iostream> using namespace std; struct node {     int data;     node *next;     node *prev; }; struct node *tail,*head; /*creating doubly circular link list*/ void createdcll() {     node *newnode;     int choice;     head=0;     while (choice!=0)     {         newnode= new node;         cout<< "enter the dcll data" <<endl;         cin>>newnode->data;         if (head==0) {             head=tail=newnode;             head->next=head;             head->prev=head;         }         else {             tail->next=newnode;             newnode->prev=tail;             newnode->next=head;             head->prev=newnode;             tail=newnode;         }         cout<< "u wanna continue" ;         cin>>choice;     } } /*display doubly circular link list*/ void displaydcll() {