C++ Source Code For Queue Free Download



Implementation of Queue As Data Structure
                                                           This is a simple C++ code which implements the most important Data Structure Queue .The code contains three files header file,cpp file and the main file which contains main function.You can download all the files by clicking the download button below or you can also copy paste the code.



This is header file.


 #ifndef FILE_H_INCLUDED
#define FILE_H_INCLUDED

//file.h

class Queue
{
  private:
  int front;
  int rear;
  int size;
  int max_size;
  int *queue_array;

  public:
  Queue(int );//constructor
  ~Queue();//destructor
  bool add(int );
  bool remove(int & );
  void print();
  bool is_empty();
  bool is_full();


};


#endif // FILE_H_INCLUDED

This is cpp file


//file.cpp
#include<iostream>
#include"file.h"

using namespace std;

//constructor function
Queue::Queue(int array_size)
{//start of function
    size=0;
    max_size=array_size;
    front=0;//points to first element
    rear=-1;//points to last element

    queue_array=new int[max_size];

}//end of function


//add function
bool Queue::add(int item)
{//start of funcion

    if(!is_full())
    {
        rear++;
        queue_array[rear]=item;
        size++;
        return true;
    }

    else
    return false;

}//end of function


//remove function
bool Queue::remove(int &item)
{//start of function

    if (!is_empty())
    {
        item=queue_array[front];
        front++;
        size--;

        return true;
    }

    else
    return false;

}//end of function


//print function
void Queue::print()
{//start of function
    if (!is_empty())
    {//start if
        for (int i=front;i<=rear;i++)
        {//start for
            cout<<queue_array[i]<<endl;
        }//end for
    }//end if
}//end of function


//is_empty function
bool Queue::is_empty()
{//start of function

    if (size==0)
    return true;

    else
    return false;
}//end of function


//is_full function
bool Queue::is_full()
{//start of function
    if (size==max_size)
    return true;
    else
    return false;
}//end of function



//destructor function
Queue::~Queue()
{//start of function

    delete queue_array;
}//end of function


This is main file


//implementation of queue
//coded by Syed Azeem Abbas

#include<iostream>
#include<windows.h>
#include"file.h"
using namespace std;


int main()
{//start of main


    int array_size;
    cout<<"Enter the maximum size of the Queue:";
    cin>>array_size;

    Queue obj(array_size);
    bool condition=true;

    while (condition==true)
    {//start of while
        system("cls");

        int a;
        cout<<"Enter 1 to add an item:"<<endl;
        cout<<"Enter 2 to remove an item:"<<endl;
        cout<<"Enter 3 to print the Queue:"<<endl;
        cout<<"Enter 4 to exit:"<<endl;

        cin>>a;

        switch (a)
        {
            case 1:
            {

                if (obj.is_full()==false)
                {
                    int item;
                    cout<<"Enter the item you want to add:";
                    cin>>item;

                    obj.add(item);
                    cout<<"Item has been added:"<<endl;
                }

                else
                {
                    cout<<"The Queue has been filled:"<<endl;

                }

                system("pause");
                break;
            }

            case 2:
            {
                int item;
                if (obj.remove(item)==true)
                cout<<item<<" has been removed:"<<endl;
                else
                cout<<"The Queue is empty:"<<endl;
                system("pause");

                break;
            }

            case 3:
            {
                if (obj.is_empty()==false)
                obj.print();
                else
                cout<<"The Queue is empty:"<<endl;
                system("pause");
                break;
            }

            case 4:
            {
                condition=false;
                break;
            }

            default :
            {
                cout<<"Enter a wrong input enter again:"<<endl;
            }
        }
    }//end of while

    system("pause");
    return 0;
}//end of main



                Download the whole project






No comments:

Post a Comment