Wednesday, December 5, 2012

Program covering basics of linked list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<iostream.h>
struct node
 {   int num;
      struct node *link;
 };
node *start , *save , *newptr , *ptr;
node * create_node ( int n )
{
      ptr  = ( node * )malloc(sizeof(struct node));
      ptr -> num = n;
      ptr -> link = NULL;
      return ptr;
}
void insert_node ( node *np , int n )
{
     node *tmp;
     if( start == NULL )
          start = np;
     else
     {    tmp = start;
          while (  tmp != NULL)
          { if ( tmp -> num <= n && ( tmp -> link -> num > n || tmp -> link == NULL))
                { np -> link = tmp -> link;
                   tmp -> link = np;
                   return;
                 }
            tmp = tmp -> link;
           }
      }
}
void del_beg()
{
         if ( start == NULL)
             printf("\nUnderflow");
        else
       {
             ptr = start;
             start = start -> link;
             free(ptr);
       }
}
void del (int inf)
{           ptr = start;
             node *tmp;
             while( ptr != NULL )
            {
                     if( ptr-> num == inf)
                    {        if ( ptr == start )
                              {    start = ptr ->link;
                                    free(ptr);
                                     return;
                              }
                             else
                             {      tmp = ptr;
                                     ptr = ptr->link;
                                     free(tmp);
                                     return;
                             }
                     }
                  else
                        ptr = ptr -> link;
              }
}
  void display ( node *np )
{
                 while (np != NULL )
                {     printf("%d" , np -> num );
                       printf("->>");
                       np = np -> link;
                 }
}
void main()
{
       clrscr();
       start = NULL ;
       int inf , ch1;
       char ch ;
       do
       {     printf("\n1.Insertion");
              printf("\n2.Deletion from begining");
              printf("\n3.Display");
              printf("\n4.Deletion of your choice");
              printf("\nEnter your choice:\t");
              scanf("%d" , &ch1 );
              switch(ch1)
             {    case 1:
                   printf("\nEnter the information of the new node:\t");
                   scanf("%d" , &inf );
                   printf("\nCreating new node!!! press Enter to continue");
                   getch();
                   newptr = create_node( inf ) ;
                   insert_node( newptr , inf );
                   display(start);
                   break;
                   case 2:
                   del_beg();
                    break;
                    case 3:
                    display(start);
                     break;
                    case 4:
                     printf("\nEnter the node to be delted:\t");
                     scanf( "%d" , inf );
                     del(inf);
                     break;
             }
              printf("\nDo you want to continue:\t");
              cin>>ch;
       }while ( ch == 'y' || ch == 'Y' );
       getch();
}

3 comments:

  1. gud one..but u to do in complicated way cant we do it in way we learnt in 12th??
    nd we need to insert anywere nt only at beg..??

    ReplyDelete
  2. anywhere ka hi hai ye program if the list is sorted aur jo 12th meh sikha tha that was only for beg and end so that does not work for sorted list

    ReplyDelete
  3. much better than the one in tbk

    ReplyDelete