Friday, August 23, 2013

Represent circular queue using link list & write a program to perform the opeations like:Insert,Delete,finding rear element,finding front element


/*--------------------------------------------------------------------------------
Program    : Represent circular queue using link list & write a program to
                      perform the opeations like:
1.      Insert
2.      Delete
3.      finding rear element
4.      finding front element

Created By : JABIR DAUD PATHAN
Branch     : COMPUTER ENGINEERING
-----------------------------------------------------------------------------------------*/

/*--------------------<<  Header File  >>---------------------*/

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

/*-----<< Global Declaration of Structure and Functions  >>-------*/

typedef struct node
{
 int data;
 struct node*next;
}node;

void ini(node**R);
void enqueue(node**R,int x);
int dequeue(node**R);
int empty(node*r);
void display(node*r);
int fr_element(node*r);
int re_element(node*r);

/*-------<< function defination for initialise queue >>------*/

void ini(node**R)
 {
   *R=NULL;
 }

/*-------<< function defination for insert element >>------*/

void enqueue(node**R,int x)
 {
  node*p;
  p=(node*)malloc(sizeof(node));
  p->data=x;
  if(empty(*R))
   {
     p->next=p;
     *R=p;
   }
  else
   {
     p->next=(*R)->next;
     (*R)->next=p;
     (*R)=p;
   }
 }

/*-------<< function defination for delete element >>------*/

int dequeue(node**R)
 {
  int x;
  node*p;
  p=(*R)->next;
  x=p->data;
  if(p->next==p)
   {
     *R=NULL;
     free(p);
     return(x);
   }
   (*R)->next=p->next;
   free(p);
   return(x);
 }

/*-------<< function defination for display >>------*/

void display(node*r)
 {
  node*p;
  if(!empty(r))
   {
     p=r->next;
   }
  else
   {
     printf("\n\nQueue is empty.");
     return;
   }
   printf("\nElements==>");
   do
   {
    printf(" %d",p->data);
    p=p->next;
   }while(p!=r->next);
 }

/*-------<< function defination for check queue empty condition >>------*/

int empty(node*p)
 {
  if(p->next==NULL)
  return(1);
  return(0);
 }

/*-------<< function defination for finding front element >>------*/

int fr_element(node*r)
 {
  return(r->next->data);
 }

/*-------<< function defination for finding rear element >>------*/

int re_element(node*r)
 {
  return(r->data);
 }

/*------------<<  Main Function Start  >>---------------*/

void main()
 {
  int x,ch,i;
  char j;
  node*r;
  ini(&r);
  do
  {
   clrscr();
   printf("\n\t\t------<< MENU >>------");
   printf("\n\n\t\t1.Insert");
   printf("\n\t\t2.Delete");
   printf("\n\t\t3.Front element");
   printf("\n\t\t4.Rear element");
   printf("\n\t\t5.Exit");
   printf("\n\n\t\t----------------------");
   printf("\n\t\tEnter your choice==>");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1:
                   printf("\nEnter the queue data==>");
                   scanf("%d",&x);
                   enqueue(&r,x);
                   display(r);
                   break;
    case 2:
                   if(!empty(r))
                    {
                     x=dequeue(&r);
                     printf("\nDeleted elements==>%d\n",x);
                    }
                   else
                     printf("\nUnder flow......!");
                   display(r);
                   break;
    case 3:
                   if(!empty(r))
                     printf("\nFront element==>%d",fr_element(r));
                   else
                     printf("\nQueue is empty.");
                   break;
    case 4:
                   if(!empty(r))
                     printf("\nRear element==>%d",re_element(r));
                   else
                     printf("\nQueue is empty.");
                   break;
    case 5:
                   exit(0);
                   break;

   default:
                   printf("\n\n\tinvalid choice");
                   break;

   }
    printf("\n\n\tDo you want to continue(Y/N)?==>");
    j=getch();
  }while(j=='Y' || j=='y');
  getch();
 }

/*------------<<  End Of Main Function >>---------------*/








/*-----------------<< OUTPUT SCREEN  >>-------------------*/

                ------<< MENU >>------
                1.Insert
                2.Delete
                3.Front element
                4.Rear element
                5.Exit
                ----------------------
                Enter your choice==>1

Enter the queue data==>1

Elements==> 1

        Do you want to continue(Y/N)?==>Y

                ------<< MENU >>------
                1.Insert
                2.Delete
                3.Front element
                4.Rear element
                5.Exit
                ----------------------
                Enter your choice==>1

Enter the queue data==>2

Elements==> 1 2

        Do you want to continue(Y/N)?==>Y

                ------<< MENU >>------
                1.Insert
                2.Delete
                3.Front element
                4.Rear element
                5.Exit
                ----------------------
                Enter your choice==>1

Enter the queue data==>3

Elements==> 1 2 3

        Do you want to continue(Y/N)?==>Y

                ------<< MENU >>------
                1.Insert
                2.Delete
                3.Front element
                4.Rear element
                5.Exit
                ----------------------
                Enter your choice==>3

Front element==>1

        Do you want to continue(Y/N)?==>Y

                ------<< MENU >>------
                1.Insert
                2.Delete
                3.Front element
                4.Rear element
                5.Exit
                ----------------------
                Enter your choice==>4

Rear element==>3

        Do you want to continue(Y/N)?==>Y

                ------<< MENU >>------
                1.Insert
                2.Delete
                3.Front element
                4.Rear element
                5.Exit
                ----------------------
                Enter your choice==>2

Deleted elements==>1

Elements==> 2 3

        Do you want to continue(Y/N)?==>Y

                ------<< MENU >>------
                1.Insert
                2.Delete
                3.Front element
                4.Rear element
                5.Exit
                ----------------------
                Enter your choice==>2

Deleted elements==>2

Elements==> 3

        Do you want to continue(Y/N)?==>Y

                ------<< MENU >>------
                1.Insert
                2.Delete
                3.Front element
                4.Rear element
                5.Exit
                ----------------------
                Enter your choice==>2

Deleted elements==>3

Queue is empty.

        Do you want to continue(Y/N)?==>Y

                ------<< MENU >>------
                1.Insert
                2.Delete
                3.Front element
                4.Rear element
                5.Exit
                ----------------------
                Enter your choice==>5








No comments:

Post a Comment