Skip to main content

Posts

Showing posts with the label Data Structure

Binary Search in C using Recursion

#include<stdio.h> void bins ( int lb , int ub , int item , int a []); void main () { int i , n , item , a [ 20 ]; printf ( "Enter the size of an array: " ); scanf ( "%d" , & n ); printf ( "Enter the elements of the array: " ); for ( i = 0 ; i < n ; i ++ ) { scanf ( "%d" , & a [ i ]); } printf ( "Enter the number to be search: " ); scanf ( "%d" , & item ); bins ( 0 , n - 1 , item , a ); } void bins ( int lb , int ub , int item , int a []) { int mid , flag = 0 ; if ( lb <= ub ) { mid = ( lb + ub ) / 2 ; if ( item == a [ mid ]) { flag = flag + 1 ; } else if ( item < a [ mid ]) { return bins ( lb , mid - 1 , item , a ); } else return bins ( mid + 1 , ub , item , a ); } if (

Write a C Program to find the number of data in a linked list

#include <stdio.h> #define newnode (struct node*)malloc(sizeof(struct node)) typedef struct node { int data ; struct node * next ; } node ; node * create_list (); void main () { node * f ; int len ; f = NULL ; f = create_list (); len = find_len ( f ); printf ( " \n length = %d" , len ); } // main node * create_list () { node * f , * c , * p ; int tdata ; f = NULL ; printf ( " \n Enter data ( use 0 to exit ) : " ); scanf ( "%d" , & tdata ); while ( tdata != 0 ) { c = newnode ; if ( c == NULL ) { printf ( " \n Insuf. mem. " ); exit ( 0 ); } c -> data = tdata ; c -> next = NULL ; if ( f == NULL ) f = c ; else p -> next = c ; p = c ; printf ( " \n Enter data ( use 0 to exit ) : " ); scanf ( "%d" , & tdata ); } //while return ( f ); } // create list int find_len ( node * f ) { int len = 0 ; node * t