Thursday, April 19, 2012

Circular LinkList



A circular linked list is a Linear Linklist whose last node point back to the head node.


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

  typedef struct Clist
      {
           int n;
           struct Clist *next;
      }node;
      node *head;

  void Insert(int num)
      {
           node *pr,*temp;
           temp= (node *)malloc(sizeof(node));
           temp->n = num;
           if(head == NULL)
              {
                   head = temp;
                   temp->next = head;
              }
           else
              {
                   temp->next = head->next;
                   head->next = temp;
                   head = temp;
              }
      }

  void InsertBeg(int num)
      {
           node *temp;
           temp=(node *)malloc(sizeof(node));
           temp->n=num;
           temp->next=head->next;
           head->next=temp;
      }

  void InsetAft(int num,int pos)
      {
           node *temp,*pr;
           int i=0;
           pr=head->next;
           while(pr!=NULL)
              {
                   pr=pr->next;
                   if(pr==head->next)
                        {
                            printf("\n\tPosition %d is Out of Range!!",pos);
                            return;
                        }
                   i++;
                   if(i==pos)
                        {
                            break;
                        }
              }
           temp =(node *)malloc(sizeof(node) );
           temp->next = pr->next;
           temp->n = num;
           pr->next = temp;
           if(pr==head)
              {
                   head=temp;
              }
      }

  void del(int num)
      {
           node *temp,*pr;
           if( head->next == head && head->n == num)
              {
                   temp = head;
                   head = NULL;
                   free(temp);
                   return;
              }
           pr = head->next;
           if(pr->n == num)
              {
                   temp = pr;
                   head->next = pr->next;
                   free(temp);
                   return;
              }
           while(pr->next != head)
              {
                   if(pr->next->n == num)
                        {
                            temp = pr->next;
                            pr->next = temp->next;
                            free(temp);
                            printf("\n\tThe No. %d is Deleted\n",num);
                            return;
                        }
                   pr = pr->next;
              }
           if(pr->next->n == num)
              {
                   temp = pr->next;
                   pr->next = head->next;
                   free(temp);
                   head = pr;
                   return;
              }
           printf("\n\t %d is Not Present\n",num);
           getch();
      }

  void Disp()
      {
           node *pr;
           if(head == NULL)
              {
                   printf("\n\tList is Undeflow");
                   return;
              }
           pr = head->next;
           printf("\n\tValues in the List : ");
           while(pr != head)
              {
                   printf("%d - ", pr->n);
                   pr = pr->next;
              }
           printf("%d\n",head->n);
           getch();
      }

  void main()
      {
           int ch=0,num,pos;
           head=NULL;
           while(ch!=6)
              {
                   clrscr();
                   printf("\n\t1. Insert");
                   printf("\n\t2. Display");
                   printf("\n\t3. Insert at Begining");
                   printf("\n\t4. Insert After");
                   printf("\n\t5. Delete");
                   printf("\n\t6. Exit");
                   printf("\n\tEnter Choice : ");
                   scanf("%d",&ch);
                   switch(ch)
                        {
                            case 1:
                                printf("\n\tEnter an Element : ");
                                scanf("%d",&num);
                                Insert(num);
                                break;
                            case 2:
                                if(head == NULL)
                                      {
                                                printf("\n\tList is Empty!\n");
                                                getch();
                                       }
                                else
                                      {
                                                Disp();
                                      }
                                                break;
                            case 3:
                                printf("\n\tEnter an Element : ");
                                scanf("%d",&num);
                                InsertBeg(num);
                                break;
                            case 4:
                                printf("\n\tEnter an Element : ");
                                scanf("%d",&num);
                                printf("\n\tEnter the Position of Insertion : ");
                                scanf("%d",&pos);
                                InsetAft(num,pos);
                                break;
                            case 5:
                                if(head == NULL)
                                      {
                                                printf("\n\tUnderflow Condition Occured\n");
                                                getch();
                                      }
                                else
                                      {
                                                printf("\n\tEnter the Number to Delete : ");
                                                scanf("%d",&num);
                                                del(num);
                                      }
                                                break;
                            case 6:
                                printf("\n\tQuit..........");
                                getch();
                                exit(0);
                            default:
                                printf("\n\tThe Choice is Out of Rangen");
                        }
              }
      }

No comments: