How to Write a Queue Class in C++

Written by alexander gokhfeld
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Write a Queue Class in C++
The messaging programs frequently use C++ queue class to represent user data. (Ablestock.com/AbleStock.com/Getty Images)

A queue is a first-in, first-out set of the elements added and removed from both ends sequentially. Queues receive and process information in the order it was received. The C++ queue class implements a queue in the form of a circular array. The elements stored in a queue are added at one end and removed from the other. Programmers need to use the default C++ queue function to write a queue class in C++ programming language.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Click the "Start" button in Windows and select the "Visual Studio" application from the "All Programs" menu.

  2. 2

    Click "File," "Open" and locate the C++ program for which you want to write a queue class.

  3. 3

    Add the following code after the line starting with "public:":

    class queue

    {

    private:

    int t[MAX];

    int il; // Insert-End

    int rl; // Remove-End

    public:

    queue()

    {

    rl=-1;

    il=-1;

    }

    void del()

    {

    int tmp;

    if(rl==-1)

    {

    cout<<"empty queue";

    }

    else

    {

    for(int j=0;j<=il;j++)

    {

    if((j+1)<=il)

    {

    tmp=t[j+1];

    t[j]=tmp;

    }

    else

    {

    il--;

    if(il==-1)

    rl=-1;

    else

    rl=0;

    }

    }

    }

    }

    void add(int item)

    {

    if(rl==-1 && il==-1)

    {

    rl++;

    il++;

    }

    else

    {

    il++;

    if(il==MAX)

    {

    cout<<"full queue\n";

    il--;

    return;

    }

    }

    t[il]=item;

    }

    void display()

    {

    if(rl!=-1)

    {

    for(int iter=0 ; iter<=il ; iter++)

    cout<<t[iter]<<" ";

    }

    else

    cout<<"empty";

    }

    };

    in order to overwrite the default queue class in your C++ program.

  4. 4

    Click "File" and select the "Save" option. Click "OK" to add a queue class in your C++ program.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.