Thursday, March 15, 2012

Circular LinkList & Circular Queue




Circular Link List - A less common convention is to make it point to the first node of the list; in that case the list is said to be circular or circularly linked; otherwise it is said to be open or linear.



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

//Circular LinkList

  struct Queue
      {
           int n;
           struct Queue *next;
      }*front, *rear,*cur,*prt;
  void push()
      {
           prt=(struct Queue *)malloc(sizeof(struct Queue));
           printf("\n\tEnter a no. : ");
           scanf("%d",&prt->n);
           prt->next=NULL;
           if(front==NULL)
              {
                   front=rear=prt;
              }
           else
              {
                   rear->next=prt;
                   rear=prt;
              }
      }

  void pop()
      {
           if(front==NULL)
              {
                   printf("\n\tQueue is Empty!");
                   getch();
                   return;
              }
           else
              {
                   prt=front;
                   front=front->next;
                   printf("\n\tNo. Poped - %d ",prt->n);
                   free(prt);
              }
           getch();
      }

  void display()
      {
           if(front==NULL)
              {
                   printf("\n\tQueue is Empty!");
                   getch();
                   return;
              }
           else
              {
                   printf("\n\tValues in Queue : ");
                   for(cur=front;cur;cur=cur->next)
                        {
                            printf(" %d ",cur->n);
                        }
                   getch();
              }
      }
  void main()
      {
           int c=0;
           char a;
           clrscr();
           front=rear=cur=prt=NULL;
           while(c!=4)
              {
                   clrscr();
                   printf("\n\t1. Push ");
                   printf("\n\t2. Pop");
                   printf("\n\t3. Display ");
                   printf("\n\t4. Exit ");
                   printf("\n\tEnter a Choice : ");
                   scanf("%d",&c);
                   switch(c)
                        {
                            case 1: push();
                                                break;
                            case 2: pop(front);
                                                break;
                            case 3: display();
                                                break;
                            case 4: printf("\n\t\tQuit...");
                                                getch();
                                                exit(0);
                        }
              }
           getch();
      }



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

//Circular Queue using LinkList

  typedef struct CList
      {
           int n;
           struct CList *next;
      }*Cnode;
  Cnode NewNode,temp,last;
  typedef Cnode Cqueue;
  Cqueue CreateQ()
      {
           Cqueue cq;
           cq=(Cqueue)malloc(sizeof(struct CList));
           if(cq==NULL)
              {
                   printf("\n\tNull Value \n");
                   return NULL;
              }
           cq->next=cq;
           return cq;
      }

  void disp(Cqueue cq)
      {
           if(isempty(cq))
              {
                   printf("\n\tList is Empty!\n");
              }
          else
              {
                   printf("\n\tValues in the List : ");
                   temp=cq->next;
                   while(temp!=cq)
                        {
                            printf("%d\t",temp->n);
                            temp=temp->next;
                        }
                   printf("\n");
              }
      }

  int isempty(Cqueue cq)
      {
           return(cq->next==cq);
      }

  Cqueue Push(Cqueue cq,int num)
      {
           Cnode NewNode;
           NewNode=(Cnode)malloc(sizeof(struct CList));
           NewNode->n=num;
           if(isempty(cq))
              {
                   NewNode->next=cq;
                   cq->next=NewNode;
                   last=NewNode;
              }
           else
              {
                   last->next=NewNode;
                   NewNode->next=cq;
                   last=NewNode;
              }
           return cq;
      }

  int Pop(Cqueue cq)
      {
           int num;
           if(isempty(cq))
              {
                   printf("\n\tUnderflow Condition!\n");
                   return -1;
              }
           else
              {
                   num=(cq->next)->n;
                   temp=cq->next;
                   cq->next=(cq->next)->next;
                   free(temp);
                   return num;
              }
      }

  void main()
      {
           int c,num;
           Cqueue cq;
           cq=CreateQ();
           clrscr();
           while(1)
              {
                   printf("\n\t1. Push");
                   printf("\n\t2. Display");
                   printf("\n\t3. Pop");
                   printf("\n\t4. Exit");
                   printf("\n\tEnter your choice: ");
                   scanf("%d",&c);
                   switch(c)
                        {
                            case 1: printf("\n\tEnter the Values: ");
                                                scanf("%d",&num);
                                                Push(cq,num);
                                                break;
                            case 2:
                                                disp(cq);
                                                break;
                            case 3:  num=Pop(cq);
                                                if(num!=-1)
                                                printf("\n\tValue Popped -  %d\n",num);
                                                break;
                            case 4:
                                                printf("\n\tQuit..,.");
                                                getch();
                                                exit(0);

                            default:
                                                printf("\nInvalid choice");
                   }
              }
           getch();
      }



No comments: