Doubly Linked List C program Data Structure


This Program is Doubly Linked List and performing all operation on the list. This is a part of Mumbai University MCA Colleges Data Structure C program MCA Sem 2

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void create(int num);
void add_at_beg(int num);
void add_after(int num,int c);
void del(int num);
void display();
void count();
void rev();
void search();
int find(int num);
void sort();

struct node
{
          struct node *prev;
          struct node *next;
          int info;
}*start,*q;

int num;
static int i=0;
void main()
{
int  m,n,po,i,choice;
          clrscr();
          start=NULL;
printf("                                        DOUBLY LINKED LIST                           ");
          while(1)
          {
printf("\n\n--------------------------- MAIN MENU -----------------\n\n");
                   printf("1.CREATE.\n\n");
                   printf("2.ADD_AT_BEG.\n\n");
                   printf("3.ADD_AFTER.\n\n");
                   printf("4.DELETE.\n\n");
                   printf("5.DISPLAY.\n\n");
                   printf("6.COUNT.\n\n");
                   printf("7.REVERSE.\n\n");
                   printf("8.SORT.\n\n");
                   printf("9.SEARCH.\n\n");
                   printf("10.EXIT.\n\n");
                   printf("Enter your choice : ");
                   scanf("%d",&choice);
                   switch(choice)
                   {
                             case 1:
printf("\n\n------------------------- CREATION OF LIST ---------------------------\n\n");
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(m);
                                      }
                                      break;

                              case 2:
printf("\n\n--------------- ADD AT BEGINING ----------------\n\n");
                                      printf("\n\nEnter The Elements : ");
                                      scanf("%d",&m);
                                      add_at_beg(m);
                                      break;


                              case 3:
printf("\n\n--------------- ADD AFTER ----------------\n\n");
                                      printf("\n\nEnter The Elements : ");
                                      scanf("%d",&m);
printf("\n\nEnter The Position after which u want to insert :");
                                      scanf("%d",&po);
                                      add_after(m,po);
                                      break;


                              case 4:
printf("\n\n------------------ DELETION ------------------------\n\n");
                                      printf("\n\nEnter The Element to be deleted : ");
                                      scanf("%d",&m);
                                      del(m);
                                      break;

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

                             case 6:
                                      count();
                                      break;

                             case 7:
                                      rev();
                                      break;

                             case 8:
                                      sort();
                                      break;

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

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

void create(int num)
{
          struct node *q,*tmp;
          tmp=malloc(sizeof(struct node));
          tmp->info=num;
          tmp->next=NULL;
          if(start==NULL)
          {
                   tmp->prev=NULL;
                   start->prev=tmp;
                   start=tmp;
          }
          else
          {
                   q=start;
                   while(q->next!=NULL)
                             q=q->next;
                   q->next=tmp;
                   tmp->prev=q;
          }
}

void add_at_beg(int num)
{
          struct node *tmp;
          tmp=malloc(sizeof(struct node));
          tmp->prev=NULL;
          tmp->info=num;
          tmp->next=start;
          start->prev=tmp;
          start=tmp;
printf("\n\n------------------------ ELEMENTS IN THE LIST AFTER ADD_AT_BEG ------------------\n\n");
          display();
}


void add_after(int num,int c)
{
          struct node *tmp,*q;
          int i;
          q=start;
          for(i=0;i<c;i++)
          {
                   q=q->next;
                   if(q==NULL)
                   {
printf("\n\n----------------------------------------------------\n\n");
                             printf("THERE ARE LESS THAN %d ELEMENTS",c);
                             printf("\n\n------------------------------------------------\n\n");
                             return;
                   }
          }
          tmp=malloc(sizeof(struct node));
          tmp->info=num;
          q->next->prev=tmp;
          tmp->next=q->next;
          tmp->prev=q;
          q->next=tmp;
printf("\n\n--------------------- ELEMENTS IN THE LIST AFTER ADD_AFTER -----------------\n\n");
          display();
}

void del(int num)
{
          struct node *tmp,*q;
          if(start->info==num)
          {
                   tmp=start;
                   start=start->next;
                   start->prev=NULL;
                   free(tmp);
printf("\n\n-------------- ELEMENTS IN THE LIST AFTER DELETION OF FIRST NODE ---------------\n\n");
                   display();
                   return;
          }
          q=start;
          while(q->next->next!=NULL)
          {
                   if(q->next->info==num)
                   {
                             tmp=q->next;
                             q->next=tmp->next;
                             tmp->next->prev=q;
                             free(tmp);
printf("\n\n--------------- ELEMENTS IN THE LIST AFTER DELETION OF INTERMEDIATE NODE -------\n\n");
                             display();
                             return;
                   }
                   q=q->next;
          }
          if(q->next->info==num)
          {
                   tmp=q->next;
                   free(tmp);
                   q->next=NULL;
printf("\n\n------------- ELEMENTS IN THE LIST AFTER DELETION OF LAST NODE --------------\n\n");
                   display();
                   return;
          }
          printf("\n\n\---------------------------------------------------------------n\n");
          printf("ELEMENT  %d  NOT FOUND");
printf("\n\n\-----------------------------------------------------------------n\n");
}

void display()
{
          struct node *q;
          if(start==NULL)
          {
printf("\n\n\-----------------------------------------------------------------n\n");
                   printf("LIST IS EMPTY");
printf("\n\n\------------------------------------------------------------------n\n");
                   return;
          }
          q=start;
          while(q!=NULL)
          {
                   printf("%d\t",q->info);
                   q=q->next;
          }
}

void count()
{
          struct node *q=start;
          int cnt=0;
          while(q!=NULL)
          {
                   q=q->next;
                   cnt++;
          }
printf("\n\n------------------- NUMBER OF NODES IN THE LIST ---------------------\n\n");
          printf("%d",cnt);
}

void rev()
{
          struct node *p1,*p2;
          printf("\n\n------------------- ORIGINAL LIST-------------------\n\n");
          display();
          p1=start;
          p2=p1->next;
          p1->next=NULL;
          p1->prev=p2;
          while(p2!=NULL)
          {
                   p2->prev=p2->next;
                   p2->next=p1;
                   p1=p2;
                   p2=p2->prev;
          }
          start=p1;
printf("\n\n---------------------- REVERSED LIST -------------------\n\n");
          display();
}

void search()
{
           int found=0;

           printf("\n\nEnter The number which is to be searched : ");
           scanf("%d",&num);
           q=start;
while((q!=NULL) && (found==0))
                   found=find(num);
           if(found)
printf("\n\n------------------ THE NUMBER %d IS PRESENT IN THE LOCATION %d --------------",num,i);
           else
printf("\n\n----------------- THE NUMBER IS NOT PRESENT IN THE LIST -------------");
           getch();


}

int find(int num)
{
          if (q->info == num )
          {
            return(1);
          }
          else
                   q=q->next;

           i++;
           return 0;
}
void sort()
{
          struct node *q,*q1;
          int c,a1,a2;
printf("\n\n-------------------------- ORIGINAL LIST-----------------\n\n");
          display();
          if(start==NULL)
          {
                    printf("\n\n--------------------------------------------------\n\n");
                    printf("LIST IS EMPTY");
                    printf("\n\n-------------------------------------------------------\n\n");
          }
          else
          {
                   q=start;
                   while(q!=NULL)
                   {
                             q1=q->next;
                             while(q1!=NULL)
                             {
                                      a1=q->info;
                                      a2=q1->info;
                                      if(a1>a2)
                                      {
                                                q1->info=a1;
                                                q->info=a2;
                                      }
                                      q1=q1->next;
                             }
                             q=q->next;
                   }
printf("\n\n---------------------------- SORTED LIST ---------------------------\n\n");
                   display();
          }
}

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