Circular Linked List C Program Data Structure

This Program is for Circular Linked List and performs various actions on the list. This is part of Mumbai University MCA Colleges Data Structures C program MCA Sem 2

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
void count();
void sort();
void display();
void create_list(int);
void addatbeg(int);
void addafter(int,int);
void del(int);
void reverse();
void search();

struct node
{
          int info;
          struct node *link;
} *last;
void main()
{
          int ch,n,m,pos,i;
          last=NULL;
          clrscr();
printf("                              CIRCULAR LINKED LIST                                ");
           while(1)
          {
printf("\n\n-------------------------- MAIN MENU ----------------------------\n\n");
                   printf("1.CREATE.\n\n");
                   printf("2.ADD_AT_BEGINING.\n\n");
                   printf("3.ADD_AFTER.\n\n");
                   printf("4.DELETE.\n\n");
                   printf("5.DISPLAY.\n\n");
                   printf("6.REVERSE.\n\n");
                   printf("7.COUNT.\n\n");
                   printf("8.SORT.\n\n");
                   printf("9.SEARCH.\n\n");
                   printf("10.EXIT.\n\n");
                   printf("Enter Your Choice : ");
                   scanf("%d",&ch);

                    switch(ch)
                   {
                             case 1:
printf("\n\nEnter the number of nodes you want : ");
                                      scanf("%d",&n);
                                      printf("\n\nEnter %d elements : ",n);
                                      for(i=0;i<n;i++)
                                      {
                                                scanf("%d",&m);
                                                create_list(m);
                                      }
                             break;

                             case 2:
printf("\n\n------------------- ADD AT BEGINING ---------------------\n\n");
                                       printf("Enter the elements: ");
                                       scanf("%d",&m);
                                       addatbeg(m);
                                       break;

                             case 3:
printf("\n\n-------------- ADD AFTER ---------------------\n\n");
                                       printf("Enter the elements : ");
                                       scanf("%d",&m);
printf("\n\nEnter the position after which the element is inserted : ");
                                       scanf("%d",&pos);
                                       addafter(m,pos);
                                       break;

                             case 4:

                                       if(last==NULL)
                                       {
printf("\n\n-------------------------------------\n\n");
                                                printf("List Underflow");
printf("\n\n-------------------------------------\n\n");
                                                continue;
                                       }
printf("\n\n------------------ DELETION --------------------\n\n");
                                       printf("Enter the number for deletion : ");
                                       scanf("%d",&m);
                                       del(m);
                                       break;

                             case 5: 
printf("\n\n---------------DISPLAYING-------------\n\n");
                                       display();
                                       break;

                             case 6:
                                       reverse();
                                       break;
                             case 7:
                                       count();
                                       break;

                             case 8:
sort();
                                       break;

                             case 9:
search();
                                       break;
                             case 10:
exit(1);

                             default:
printf("\n\nINVALID CHOICE !!!!! TRY AGIAN\n\n");
                              }
                    }
      }

 void create_list(int num)
 {
          struct node *q, *tmp;
          tmp=malloc(sizeof(struct node));
          tmp->info=num;
if(last==NULL)
          {
                    last=tmp;
                    tmp->link=last;
          }
          else
          {
                    tmp->link=last->link;
                    last->link=tmp;
                    last=tmp;
          }
   }

    void addatbeg(int num)
    {
          struct node *tmp;
          tmp=malloc(sizeof(struct node));
          tmp->info=num;
          tmp->link=last->link;
          last->link=tmp;
printf("\n\n------------ELEMENTS IN THE LIST AFTER ADD_AT_BEG--------------\n\n");
          display();
    }

     void addafter(int num,int pos)
     {
                    struct node *tmp, *q;
                    int i;
                    q=last->link;
                    for(i=0;i<pos-1;i++)
                    {
                             q=q->link;
                             if(q==last->link)
                             {
                                       printf("\n\n-------------------------------------\n\n");
                                       printf("There are  less than %d elements\n",pos);
                                       printf("\n\n-------------------------------------\n\n");
                                       return;
                             }
                   }
                    tmp=malloc(sizeof(struct node));
                   tmp->link=q->link;
                   tmp->info=num;
                   q->link=tmp;
                    if(q==last)
                   last=tmp;
printf("\n\n------------ELEMENTS IN THE LIST AFTER   ADD_AFTER--------------\n\n");
                   display();
  }

 void del(int num)
 {
          struct node *tmp, *q;
          if(last->link==last&&last->info==num)
          {
          tmp=last;
          last=NULL;
          free(tmp);
          return;
          }
          q=last->link;
          if(q->info==num)
          {
                   tmp=q;
                   last->link=q->link;
                   free(tmp);
printf("\n\n------------ ELEMENTS IN THE LIST AFTER DELETION OF FIRST NODE --------------\n\n");
                   display();
                   return;
   }
   while(q->link!=last)
   {
          if(q->link->info==num)
          {
                   tmp=q->link;
                   q->link=tmp->link;
                   free(tmp);
printf("\n\n------------ ELEMENTS IN THE LIST AFTER  DELETION OF MIDDLE NODE --------------\n\n");
                   display();
                   return;
          }
          q=q->link;
  }

  if(q->link->info==num)
  {
          tmp=q->link;
          q->link=last->link;
          free(tmp);
          last=q;
printf("\n\n------------ ELEMENTS IN THE LIST AFTER DELETION OF LAST NODE --------------\n\n");
          display();
          return;

  }
printf("\n\n ------------------- ELEMENT %d NOT FOUND ---------------------\n\n",num);
   }

void display()
{
          struct node *q;
           if(last==NULL)
          {
                    printf("\n\n---------------------------------------------\n\n");
                    printf("list is empty\n");
                    printf("\n\n---------------------------------------------\n\n");
                    return;
          }
          q=last->link;
          while(q!=last)
          {
                   printf("%d\t",q->info);
                    q=q->link;
          }
          printf("%d\t",last->info);
}

void reverse()
{
          struct node *p1,*p2,*p3,*start,*q;
          printf("\n\n--------------- ORIGINAL LIST --------------\n\n");
          display();
          start=last;
          p1=last;
          p2=p1->link;
          p3=p2->link;
          p1->link=NULL;
          p2->link=p1;
          while(p3!=start)
          {
                   p1=p2;
                   p2=p3;
                   p3=p3->link;
                   p2->link=p1;
          }
          last=p3;
          p3->link=p2;
          q=last->link;
printf("\n\n-------------------- REVERSED LIST ----------------------\n\n");
          printf("%d\t",last->info);
          while(q!=last)
          {
                printf("%d\t",q->info);
                q=q->link;
          }
}

void count()
{
          struct node *q, *tmp;
          int cnt=1;
          q=last->link;
printf("\n\n------------------ Number of nodes in the list --------------\n\n");
          while(q!=last)
          {
                   q=q->link;
                   cnt=cnt++;

          }
          printf("%d\t",cnt);

}

void sort()
{
          struct node *q,*q1;
          int c,a1,a2,i;
          if(last==NULL)
          {
printf("\n\n--------------------------------------------------------------\n\n");
                   printf("List is empty");
printf("\n\n-----------------------------------------------------------\n\n");
          }
          else
          {
                   printf("\n\n--------------- ORIGINAL LIST --------------\n\n");
                   display();
                   q=last->link;
                   while(q!=last)
                   {
                                      q1=q->link;
                                      while(q1!=last)
                                      {
                                                a1=q->info;
                                                a2=q1->info;
                                                if(a1>a2)
                                                {
                                                          q->info=a2;
                                                          q1->info=a1;

                                                }
                                                q1=q1->link;
                                      }
                                      a1=q->info;
                                      a2=q1->info;
                                      if(a1>a2)
                                      {
                                                q->info=a2;
                                                q1->info=a1;

                                       }
                                       q=q->link;
                             }

printf("\n---------------------- SORTED LIST --------------------\n");
                   display();
                   return;
          }
}

void search()
{

          struct node *q,*tmp;
          int i=1,item;
printf("\n\n------------------------------- ORIGINAL LIST --------------\n\n");
          display();
          printf("\n\nEnter the element to be searched :");
          scanf("%d",&item);
          tmp=last->link;
          while(tmp->link!=last)
          {
                   if(tmp->info==item)
                   {
printf("\n---------------------- Element %d is found at location : %d --------------\n",item,i);
                             return;
                   }
                   tmp=tmp->link;
                   ++i;

          }
          if(tmp->link==last)
                   printf("\n------------------------ Element is not found ---------------------\n");
                   return;
}



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a comment