Monday, October 31, 2011

Writing Queue Data Structure In C++

on the same line of our previous post, here is how to write Queue in c++:

[cpp]
<pre>
#include <iostream.h>

struct node
{
int data;
node *link;
};

class queue
{
private:
node *front, *rear;

public:

queue()
{
front = rear = NULL;
}

void addq(int item)
{
node *temp;

temp = new node;

if(temp == NULL)
cout << endl << "Queue is full";

temp->data = item;
temp->link = NULL;

if(front == NULL)
{
rear = front = temp;
return;
}

rear->link = temp;
rear = rear->link;
}

int delq()
{
if(front == NULL)
{
cout << endl << "Queue is empty";
return;
}

node *temp;
int item;

item = front->data;
temp = front;
front = front->link;
delete temp;
return item;
}

~queue()
{
if(front == NULL)
return;
node *temp;
while(front != NULL)
{
temp = front;
front = front->link;
delete temp;
}
}
};</pre>
[/cpp]

3 comments:

  1. What's up, its good article regarding media print, we all know
    media iis a fantastic source off facts.

    ReplyDelete
  2. What's up, yes this post is truly fastidious and I havce learned lot
    of things from iit on the topic of blogging. thanks.

    ReplyDelete
  3. You're so cool! I do not beliesve I've truly read through anything like that
    before. So wonderful to discover someone with unique thoughts on this issue.

    Seriously.. thanks for starting this up.
    This web site is something that is required on the web, someone with a bit
    of originality!

    ReplyDelete