Queue Data Structure in C++ Programming (using arrays) | All Queue Operations | Part – 2



We will be implementing queue data structure and all its standard operations. We will implement queue using arrays in c++ programming language.
Following are the stack operations –
1) enqueue() –
Elements are added from one end (Rear / Back).
2) dequeue() –
Elements are removed from one end (Head / Front).
3) isEmpty() –
Tells if the queue is empty or not
4) isfull() –
Tells if the queue is full or not.
5) count() –
Get the number of items in the queue.
6) display() –
Display all items in the queue

If you want the full C++ Program code for Queue & Queue operations follow me on Instagram or Facebook (links given below) & send me your email address & I will mail the full code to you ✌

Full DSA playlist –
Full Article & Code on our official website –

C++ Programming Tutorials for Beginners Course –

Simple Snippets Official Website –

Simple Snippets on Facebook –

Simple Snippets on Instagram –

Simple Snippets on Twitter –

Simple Snippets Google Plus Page –

Simple Snippets email ID –
simplesnippetsinfo@gmail.com

For More Technology News, Latest Updates and Blog articles visit our Official Website –

#queue #queueprogram #queuecpp #queuedatastructure #datastructures #queueds #queueoperations

Nguồn: https://anniesnannies-seattle.com/

Xem thêm bài viết khác: https://anniesnannies-seattle.com/cong-nghe/

37 thoughts on “Queue Data Structure in C++ Programming (using arrays) | All Queue Operations | Part – 2

  1. int count()
    {
    if(rear==-1 && front==-1)
    return 0;
    else
    return rear-front+1;
    }
    count should be like this otherwise without element also count will be 1.
    Well thank you sir

  2. Nice code but if someone insert 0 in the queue and display it then the output will be just zeros and it may become confusing at that index whether element is inserted or is it empty position. While displaying 0 should be converted to null.

  3. When we carry dequeue operation, should the elements shieft by one index towards the element which was dequeued?
    Like we have five elements
    10 20 30 40 50.
    And we carried dequeue
    Then 10 gets dequeued
    And the new sequence should be
    20 30 40 50 0
    Right?

  4. Is there an option to replace 0 with null because if someone enqueue 0 then it will be confusing right. When he uses display function then it will show the empty ones which is 0 and his 0. So how to solve that issue.

  5. So, I had a quick question. When is a queue full? I understood all the concepts and your video was AWESOME (as always haha)… but not being able to define if the queue was full or not made me think what a queue being full meant? I'd appreciate if you could let me know. Thanks and like you always say… Peace <3

  6. There is mistake in count function also…if we does not enqueue any value….count must be 0 but this will show 1 because in that case rear and front both are -1 and -1-1+1 is 1…..so put this case also in count function when front and rear are -1

  7. Great Explanation!!
    Minor bug fixes:
    bool isFull()

    {

    if (r-f == size-1)

    {

    return true;

    }

    else

    {

    return false;

    }

    }
    int count()

    {

    if (r == -1 && f == -1)

    {

    return 0;

    }

    else

    {

    return (r – f) + 1;

    }

    }

  8. #include<iostream>
    using namespace std;
    class queue{
    int i,arr[5],rear,front;
    public:
    queue(){
    rear=0;
    front=-1;
    for(i=0;i<5;i++)
    arr[i]=0;
    }
    bool isempty(){
    if(rear=-1&&front==-1)
    return true;
    else
    return false;
    }
    bool isfull(){
    if(rear==4)
    return true;
    else
    return false;
    }
    void enque(int value){
    if(isfull()){
    cout<<"queue is full ,element can't be enque"<<endl;
    }
    else if(isempty()){
    front=0;
    rear=0;
    arr[rear]=value;
    }
    else{rear++;
    arr[rear]=value;
    }}
    void deque(){
    if(isempty())
    {cout<<"queue is empty ,element can't be deque"<<endl;}
    else if(rear==front){
    arr[front]=0;
    front=rear=-1;
    }
    else
    {
    arr[front]=0;
    front++;
    }}
    void display(){
    for(i=0;i<=4;i++)
    cout<<arr[i]<<" ";
    }
    void peek(int position){
    if(rear-front+1>=position)
    cout<<arr[position];
    else
    cout<<"invalid position"<<endl;
    }
    void change(int position,int value){
    if(rear-front+1>=position)
    arr[position]=value;
    else
    cout<<"invalid position";
    }
    int count(){
    return rear-front+1;
    }
    void ree(){
    cout<<rear<<endl;
    }
    };
    int main(){
    queue q1;
    int value,option,position;
    do{
    cout<<endl<<"SELECT THE OPERATION,press '0' for stop"<<endl;
    cout<<"'1'isEmpty"<<endl;
    cout<<"'2'isfull"<<endl;
    cout<<"'3'Enque"<<endl;
    cout<<"'4'Deque"<<endl;
    cout<<"'5'Display"<<endl;
    cout<<"'6'peek"<<endl;
    cout<<"'7'change"<<endl;
    cout<<"'8'Count"<<endl;
    cout<<"'9'ClearScreen"<<endl;
    cin>>option;
    cout<<endl;
    switch(option){
    case 0: exit(0);
    break;
    case 1:{if(q1.isempty())
    cout<<"queue is empty"<<endl;
    else
    cout<<"queue is not empty"<<endl;
    }
    break;
    case 2:{if(q1.isfull())
    cout<<"queue is full"<<endl;
    else
    cout<<"queue is not full"<<endl;
    }
    break;
    case 3:{cout<<"enque is called,value:";
    cin>>value;
    q1.enque(value);
    }
    break;
    case 4: q1.deque();
    break;
    case 5:q1.display();
    break;
    case 6:{cout<<"peek option is called, position:";
    cin>>position;
    q1.peek(position);
    }
    break;
    case 7:{cout<<"change option is callednposition:";
    cin>>position;
    cout<<"value:";
    cin>>value;
    q1.change(position,value);
    }
    break;
    case 8:{cout<<"the total no. of elements isn array:"<<q1.count()<<endl;
    }
    break;
    case 9:{system("cls");
    }
    case 11:{
    cout<<"rear=";
    q1.ree();}
    break;
    default:cout<<"enter the proper option"<<endl;
    }
    }while(option!=0);
    }//bro please help me to correct this code ,it is not storing more then two elements, if you enter third element for enque then it will replace third element with second element,

  9. Best explanation but in the count function it is (rear – front +1 ) but if both the rear and count are at -1 and -1 the count should be zero but it is 1..

  10. in dequeue why we have written return x, i mean we dont need that x right. could the return type of dequeue be void??

  11. bhai, apke is channel ko to 1Milllion subcribers hona chaiye, itne acche aur satik tareeke se koi DS padha v skta h, vishwas nhi ho rha h!!!!!!!!…m apka bhot bhot abhari hu, jo apne itne jatil vishayon ko v itne saral aur graphics se zariye itna accha banaya ki hm ubte tk nhi!!!!!please ap CS k baaki subjects ko v graphics k zariye padhaeyega (but free m, bhai :)), taki humara bhala ho….apko ishwar sada sukhi rakhe……
    -Apka ek subscriber….

  12. Your explanation is mind blowing. Through your videos i get to learn things in a single shot!
    I have one request – can you please make a video on linked list implementation of stacks and queues.

  13. Your channel is like finding an oasis in Sahara Desert….. Thanks for the great explanation and endless effort !!!

  14. If you add 3 element in the quee and then dénuée the first element you will have something like 0 56 34 0 0 and if you call the count function it will execute rear-front +1 which is =-1-(-1)+1=1 ;which is wrong because you still have 2 element not one so to fix this you can initialize int itemsnumber. to 0 and do for (int i=0;I<5;I++){if(arr[i]!=0) itemsnumber ++;}. It will loop through all arrays numbers and if the number is not null it will increment the item number to 1

  15. I think you should check your dequeue "else" block
    it should be
    [x=arr[rear];

    arr[rear]=0;

    rear–;] in order to established FIFO or LILO
    in your code it is currently performing FILO or LIFO(stack)
    Am I right or not bro??

  16. Sir ,
    according to my your function to check whether queue is full or not is wrong
    it should be
    bool isFull()
    {
    if(rear- front == 4)
    return true;
    else
    return false;
    }

    please tell me if i am wrong…….

  17. When we dequeue all the elements how the rear and front go to -1 as there is no change in rear in the else part of the dequeue function. Whenever i dequeue all the elements i can not enter new elements in the queue so the queue becomes useless. Can you help me?

  18. Hey Guys, if you want more such tech educational videos on this channel then please support me by subscribing to this channel & also share it with your friends as it helps me create more content just for you ✌

Leave a Reply

Your email address will not be published. Required fields are marked *