Young India 99

YES-Young Energetic Sincere

Home
Instant Inspiration
Incredible India
Knowledge Transfer(KT)
Placement Puraan
VTU Results
Technical Stuff
Coders Adda
win32 snippets
Windows CE Basics
stock ticker PocketPC
Windows Custom Control
Concatenated SMS
Using Timers in Linux
IPC in Linux
Deadlock Detection Algo
Priority queue spinlocks
Sample Queue C++
Contact Us
Sample Queue implementation in C++
 
 
#include<iostream>

//defining node data type
class node
{
    friend linkedqueue;//allowing linkedqueue class to access the data members of class node 
    private:
    int data;//value field
    node *link;//address field
};

//defining class linkedqueue
class linkedqueue
{
    private:
    node *front;//pointer to first node
    node *rear;//pointer to last node
    public: 
    linkedqueue();//constructor
    {
        front=rear=0;//initialising both front and rear to null
    }

    ~linkedqueue();//destructor
    bool isempty() //function to find out whether queue is empty
    {
        return(front)?false;true);//if front pointer is null, queue is empty
    }
    bool isfull();//checks whether queue is full
    int first();//returns first value stored
    int last();//returns last value stored
    linkedqueue& add(const int& x);//function to insert the new node at the end of the linked list
    linkedqueue& delete(int& x);//function to delete the first node
};

linkedqueue::~linkedqueue()//destructor function definition
{
    node *next;//create a dummy pointer to a node
    while(front)
    {
    next=front-->link;//destroy all the nodes of a queue
    delete front;
    front=next;
    }
}

bool linkedqueue::isfull()//isfull fucntion definition
//returns true if queue is full
{
    node *p;
    try
    {
    p=new node;//create a new dummy node,
    delete p;//delete the dummy node
    return false;
    }
    catch (noMem) {return true;}//if memory allocation was successful return false else return true
}



int linkedqueue::first()//first function definition
//returns the first value
{
    if(isempty())
    {
    cout<< "queue empty";
    return 0;
    }
    return front-->data;//if queue is not empty return first value
}

int linkedqueue::last()//last function definition
{
    if(isempty()
    {
    cout<< "queue empty";
    return 0;
    }
    return rear-->data;//if queue is not empty returns the last value
}

linkedqueue& linkedqueue::add(const int& x)//function to add new node at the end of the queue
//modifies the linked queue 
{
    node *p= new node://creating a new node
    p-->data=x;//assigning the data value
    p-->link=0;//link points to null
    if(front) rear-->link=p;//if queue is not empty new node becomes the rear node of the queue
    else front=p;//if queue is empty ,new node corresponds to both front and rear node
    rear=p;
    return *this;
}

linkedqueue& linkedqueue::delete(int& x)//function to delete the first node of the queue
//modifies the linked queue 
{

    x=front->data;
    node *p=front;
    front=front->link;//front points to next node
    delete p;//first node is deleted
    return *this;
}

int main()
{
linkedqueue queue;
int y
cout<<"enter the first value\n";
cin>>y;
queue.add(y);
cout<<"Enter the second value\n";
cin>>y;
queue.add(y);
cout<<"enter the third value\n";
cin>>y;
queue.add(y);
cout<<"first value:";
y=queue.first();
cout<<y;
cout<<"\nlast value:";
cout<<queue.last();
cout<<"\nafter pop operation";
queue.delete();
cout<<"first value:";
y=queue.first();
cout<<y;
cout<<"\nlast value:";
cout<<queue.last();
}