Thursday, January 15, 2015

How to implement Circular Queue By Linklist?

//Circular Queue Through Linklists
#include<iostream>
#define SIZE 100
#include<conio.h>
using namespace std;
struct circularque
{
  int data;
  struct circularque *next;
}*f=NULL,*r=NULL,*n,*temp,*temp1;

void insert();
void delet();
void display();

int main()
{
  int ch;
  do
  {
     cout<<"\n\n\tMain Menu";
     cout<<"\n****************************";
     cout<<"\n1. Insert\n2. Delete\n3. Display\n4. Exit\n\nEnter Your Choice: ";
     cin>>ch;
     switch(ch)
     {
    case 1:
      insert();
      display();
      break;
    case 2:
      delet();
      break;
    case 3:
    display();
      break;
      case 4:
    default:
      cout<<"\n\nWrong Choice!!! Try Again.";
     }
  }while(ch!=4);
  return 0;
}

void insert()
{
  n=new circularque[sizeof(circularque)];
  cout<<"\nEnter the Element: ";
  cin>>n->data;
  if(f==NULL)
  {
      f=n;
  }
  else
  {
      r->next=n;
  }
  r=n;
  r->next=f;
}
void delet()
{
  int x;
  temp=f;
  if(f==NULL)
  {
      cout<<"\nCircular Queue Empty!!!";
  }
  else
  {
     if(f==r)
     {
       x=f->data;
       delete(temp);
       f=NULL;
       r=NULL;
     }
     else
     {
    x=temp->data;
    f=f->next;
    r->next=f;
    delete(temp);
     }
     cout<<"\nElement "<<x<<" is Deleted";
     display();
  }
}
void display()
{
  temp=f;
  temp1=NULL;
  if(f==NULL)
  {
    cout<<"\n\nCircular Queue Empty!!!";
  }
  else
  {
    cout<<"\n\nCircular Queue Elements are:\n\n";
    while(temp!=temp1)
    {
       cout<<temp->data<<"  ";
       temp=temp->next;
       temp1=f;
    }
  }
}

No comments:

Post a Comment