Seprate Chaining Technique

#include < stdio.h >
#include < conio.h >
#define SIZE 5

struct node
{
 int value ;
 struct node *next ;
};

void search_value( int  , struct node * );
void assign_value( int , struct node  * );
void display ( struct node * );
void main()
{
 struct node *arr[SIZE];
 int i = 0;
 int value , choice ;
 
 system("cls");

 for( i = 0 ; i < SIZE ; i++ )
 {
  arr[i] = NULL ;
 }

 while ( 1 )
 {
  printf( " \n\n 1. Insert \n 2. Search \n 3. Display \n 4. Exit \n\n Enter choice : ");
   scanf( "%d" , &choice );
   
   if ( choice == 1 )
   {
    printf( " \n Enter value : " );
    scanf("%d" , &value );

    assign_value( value , arr  );
   }
   else if ( choice == 2 )
   {
    printf( " \n Enter value : " );
    scanf("%d" , &value );

    search_value( value , arr );
   }
   else if ( choice == 3 )
   {
    display ( arr ) ;
   }
   else
   {
    printf( " \n Thank you ! " );
    getch();
    exit(0);
   }
   getch();
 }

 getch();
}

int hash_function( int value )
{
 return value % SIZE ;
}


void assign_value( int value , struct node  *arr[] )
{
 int hash_value = hash_function( value );
 int i = 0 ;
 struct node *current = arr[ hash_value ] ; 
 struct node *new_node;

 new_node = ( struct node * ) malloc ( sizeof( struct node ) ) ;
 new_node->value = value;
 new_node->next = NULL ;

 if( arr[ hash_value ]  == NULL  )
 {
  arr[ hash_value ] = new_node ;
 }
 else
 {
  while ( current->next != NULL )
  {
   current = current->next ;
  }
  current->next = new_node;
 }
 display ( arr ) ;
}

void search_value( int value , struct node *arr[] )
{
 int hash_value = hash_function( value );
 int i = 0 ;
 struct node *current = arr[ hash_value ] ; 
 
 
 if( arr[ hash_value ]  ==  NULL  )
 {
  printf(" value not found " );
 }
 else
 {
  while ( current != NULL && current->value != value )
  {
   current = current->next ;
  }
  
  if( current != NULL )
  {
   printf(" Value found ! " );
  }
  else
  {
   printf(" Value not found ! " );
  }
 }
}

void display ( struct node *arr[] )
{
 int i = 0 ;
 struct node *current;
 
 printf( " \n\n Value stored .. " );
 for ( i = 0 ; i < SIZE ; i++ )
 {
  current = arr [ i ] ;
  printf( "\n\n %d Link List : " , i + 1 );
  while( current != NULL )
  {
   printf( " %d -> " , current->value );
   current = current->next ;
  }
  printf( " NULL \n" );
 }
 getch();
}


***********************************************************************************************************************
------------------------------------------------------------------------

         code
------------------------------------------------------------------------

 Enter choice : 1

 Enter value : 15


 Value stored ..

 1 Link List :  10 ->  15 ->  NULL


 2 Link List :  NULL


 3 Link List :  NULL


 4 Link List :  NULL


 5 Link List :  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 1

 Enter value : 20


 Value stored ..

 1 Link List :  10 ->  15 ->  20 ->  NULL


 2 Link List :  NULL


 3 Link List :  NULL


 4 Link List :  NULL


 5 Link List :  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 1

 Enter value : 30


 Value stored ..

 1 Link List :  10 ->  15 ->  20 ->  30 ->  NULL


 2 Link List :  NULL


 3 Link List :  NULL


 4 Link List :  NULL


 5 Link List :  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 1

 Enter value : 17


 Value stored ..

 1 Link List :  10 ->  15 ->  20 ->  30 ->  NULL


 2 Link List :  NULL


 3 Link List :  17 ->  NULL


 4 Link List :  NULL


 5 Link List :  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice :






1

 Enter value : 16


 Value stored ..

 1 Link List :  10 ->  15 ->  20 ->  30 ->  NULL


 2 Link List :  16 ->  NULL


 3 Link List :  17 ->  NULL


 4 Link List :  NULL


 5 Link List :  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 1

 Enter value : 18


 Value stored ..

 1 Link List :  10 ->  15 ->  20 ->  30 ->  NULL


 2 Link List :  16 ->  NULL


 3 Link List :  17 ->  NULL


 4 Link List :  18 ->  NULL


 5 Link List :  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 1

 Enter value : 19


 Value stored ..

 1 Link List :  10 ->  15 ->  20 ->  30 ->  NULL


 2 Link List :  16 ->  NULL


 3 Link List :  17 ->  NULL


 4 Link List :  18 ->  NULL


 5 Link List :  19 ->  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 1

 Enter value : 26


 Value stored ..

 1 Link List :  10 ->  15 ->  20 ->  30 ->  NULL


 2 Link List :  16 ->  26 ->  NULL


 3 Link List :  17 ->  NULL


 4 Link List :  18 ->  NULL


 5 Link List :  19 ->  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 1

 Enter value : 44


 Value stored ..

 1 Link List :  10 ->  15 ->  20 ->  30 ->  NULL


 2 Link List :  16 ->  26 ->  NULL


 3 Link List :  17 ->  NULL


 4 Link List :  18 ->  NULL


 5 Link List :  19 ->  44 ->  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 2

 Enter value : 25
 Value not found !

 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 2

 Enter value : 26
 Value found !

 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 3


 Value stored ..

 1 Link List :  10 ->  15 ->  20 ->  30 ->  NULL


 2 Link List :  16 ->  26 ->  NULL


 3 Link List :  17 ->  NULL


 4 Link List :  18 ->  NULL


 5 Link List :  19 ->  44 ->  NULL


 1. Insert
 2. Search
 3. Display
 4. Exit

 Enter choice : 4

 Thank you !
SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment