20130330

How to write cpp program for Circular Queue using Linked List



 /*C++ Program for Circular Queue implementation using linked list
Cpp program to implement Circular Queue using Linked List*/
C++ Program for implementing Circular Queue with Linked List

#include<iostream>
using namespace std;

class queue
{
private:
struct node
{
int data;
node* link;
};
node *front;
node *rear;

public:
void insert(int);
void display();
void del();
queue()
{
front=NULL;
front=NULL;
}
};
void queue::insert(int d)
{
//cout<<"insert called\n";
node *newnode=new node;
newnode->data=d;
newnode->link=NULL;

if(front==NULL)
{
front=rear=newnode;
}
else
{
rear->link=newnode;
rear=newnode;
}
}
void queue::display()
{
if(front==NULL){cout<<"Queue is empty.\n";}
else
{
node *ptr;
ptr=front;
while(ptr)
{
cout<<ptr->data<<" ";
ptr=ptr->link;
}
}
cout<<endl;
}
void queue::del()
{
node* temp;
temp=front;
if(front==NULL){cout<<"Queue is Empty.\n";}
else
{
node* temp;
temp=front;
front=front->link;
//rear->link=front;
delete temp;
}
}

int main()
{
queue q;
q.insert(10);
q.insert(20);
q.insert(100);
q.insert(40);
q.insert(300);
q.display();
q.del();
q.display();
return 0;
}
/*
10 20 100 40 300
20 100 40 300
*/

No comments:

Post a Comment