Singly Linked List

/* A "BIG" PRG of singly linked list */
#include
#include
void list();
void specific();
void allocate();
void menu();
void fun();
void beg();
void end();
void delete1();
void delete2();
void delete3();
struct node
  {
     int info;
     struct node *ptr;
  };

typedef struct node  NODE;
NODE *start=0,*refresh,*pre,*temp,*del;
main()
  {
     char ch;
     clrscr();
     start=0;
     do
       {
      allocate();
      if(start==0)
       {
         start=refresh;
         pre=refresh;
       }
      else
       {
         pre->ptr=refresh;
         pre=refresh;
       }
      printf("do you want to continue(N/n)-> ");        fflush(stdin);
      ch=getche();
    }while(ch!='n'||ch=='N');
     fun();
 }
void list()
 {
   refresh=start;
      printf("\n\t data entered in the list are\n\n");
      while(refresh!=0)
      {
     printf("\n\t\t\t%d",refresh->info);
     refresh=refresh->ptr;
      }
 }
void allocate()
  {
     refresh=(NODE*)malloc(sizeof(NODE));
     printf("\n\nenter data-> ");
     scanf("%d",&refresh->info);
     refresh->ptr=0;
  }
void menu()
  {
     clrscr();
     gotoxy(35,1);  printf("MENU");
     gotoxy(10,2);printf("----------------------------------------------");
     gotoxy(32,3);  printf("1. List Data");
     gotoxy(32,4);  printf("2. Insert at Biginning ");
     gotoxy(32,5);  printf("3. Insert at the End");
     gotoxy(32,6);  printf("4. Insert at the Specified Position");
     gotoxy(32,7);  printf("5. Delete at Biginning");
     gotoxy(32,8);  printf("6. Delete at the End");
     gotoxy(32,9);  printf("7. Delete at the Specified Position");
     gotoxy(32,10);  printf("8. EXIT");
     gotoxy(10,11);printf("----------------------------------------------");
     printf("\nenter your choice-> ");
  }
void fun()
 {
    char c,ch;
    while(1)
      {
      c=getche();
     menu();     fflush(stdin);
     if(c=='2'||c=='3'||c=='4')
        allocate();
     if(c=='1')
          list();
     if(c=='2')
          beg();
     if(c=='3')
          end();
     if(c=='4')
       specific();
     if(c=='5')
       delete1();
     if(c=='6')
       delete2();
     if(c=='7')
       delete3();
     if(c=='8')
       exit(0);
      }
  }
void beg()
  {
     if(start==0)
       start=refresh;
     else
       refresh->ptr=start;
       start=refresh;
  }
void end()
  {
     if(start==0)
       start=refresh;
     else
       {
     for(pre=start;pre->ptr!=0;pre=pre->ptr);
     pre->ptr=refresh;
       }
  }
void specific()
  {
     int a=1,b;
     printf("enter position no. in the list-. ");
     scanf("%d",&b);
     if(start==0)
      {
    printf("there is no elemens");
    return;
      }
     if(b==1)
    {
      beg();
      return;
    }
     for(pre=start;pre->ptr!=NULL&&a<=b-2;pre=pre->ptr,a++);
     if(pre->ptr==0)
       {
        printf("nodes are les than the number you have entered");
        return;
       }
     temp=pre->ptr;
     pre->ptr=refresh;
     refresh->ptr=temp;
}
void delete1()
   {
       temp=start;
       start=start->ptr;
       free(temp);
   }
void delete2()
  {
       temp=start;
       for(;temp->ptr!=0;pre=temp,temp=temp->ptr);
       temp=temp->ptr;
       pre->ptr=0;
       free(temp);
  }
void delete3()
  {
     int a=1,pos;
     printf("enter the position-> ");
     scanf("%d",&pos);
     temp=start;
     if(pos==1)
       {  delete1();
      return;    }
     for(;aptr!=0;a++,temp=temp->ptr);
     if(temp->ptr==0)
      printf("data are less in the list");
     pre=temp->ptr;
     del=temp->ptr;
     temp->ptr=pre->ptr;
     free(del);
  }


#vpsinghrajput

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment