Sei sulla pagina 1di 15

//file #include<stdlib.h> #include<stdio.

h> FILE *fp,*fp1; int no,i,no1,no2; void create() { fp=fopen("data","w"); printf("enter your number"); for(i=0;i<10;i++) { scanf("%d",&no); putw(no,fp); } fclose(fp); } void display() { fp=fopen("data","r"); printf("\n\ncontent of file:-"); while((no=getw(fp))!=EOF) { printf("%d\n",no); } fclose(fp); } void update() { int f; fp=fopen("data","a"); printf("\nenter a new number:-"); scanf("%d",&no2); no=no2; putw(no,fp); printf("\n the file is updated"); fclose(fp); } main() { while(1) { printf("\n\t1.create a file"); printf("\n\t2.display a file"); printf("\n\t3.update a file"); printf("\n\t4.exit"); printf("\n\tenter your choice :-"); scanf("%d",&i); switch (i) { case 1:create(); break;

case 2:display(); break; case 3:update(); break; case 4:exit(1); } } }

/* Merge Sort. */ #include <stdio.h> #include <conio.h> void main( ) { int a[5] = { 11, 2, 9, 13, 57 } ; int b[5] = { 25, 17, 1, 90, 3 } ; int c[10] ; int i, j, k, temp ; clrscr( ) ; printf ( "Merge sort.\n" ) ; printf ( "\nFirst array:\n" ) ; for ( i = 0 ; i <= 4 ; i++ ) printf ( "%d\t", a[i] ) ; printf ( "\n\nSecond array:\n" ) ; for ( i = 0 ; i <= 4 ; i++ ) printf ( "%d\t", b[i] ) ; for ( i = 0 ; i <= 3 ; i++ ) { for ( j = i + 1 ; j <= 4 ; j++ ) { if ( a[i] > a[j] ) { temp = a[i] ; a[i] = a[j] ; a[j] = temp ; } if ( b[i] > b[j] ) { temp = b[i] ; b[i] = b[j] ; b[j] = temp ; } } } for ( i = j = k = 0 ; i <= 9 ; ) { if ( a[j] <= b[k] ) c[i++] = a[j++] ; else c[i++] = b[k++] ; if ( j == 5 || k == 5 ) break ; } for ( ; j <= 4 ; ) c[i++] = a[j++] ; for ( ; k <= 4 ; ) c[i++] = b[k++] ; printf ( "\n\nArray after sorting:\n") ; for ( i = 0 ; i <= 9 ; i++ ) printf ( "%d\t", c[i] ) ; getch( ) ;

/* Quick sort. */ #include <stdio.h> #include <conio.h> int split ( int*, int, int ) ; void main( ) { int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ; int i ; void quicksort ( int *, int, int ) ; clrscr( ) ; printf ( "Quick sort.\n" ) ; printf ( "\nArray before sorting:\n") ; for ( i = 0 ; i <= 9 ; i++ ) printf ( "%d\t", arr[i] ) ; quicksort ( arr, 0, 9 ) ; printf ( "\nArray after sorting:\n") ; for ( i = 0 ; i <= 9 ; i++ ) printf ( "%d\t", arr[i] ) ; getch( ) ; } void quicksort ( int a[ ], int lower, int upper ) { int i ; if ( upper > lower ) { i = split ( a, lower, upper ) ; quicksort ( a, lower, i - 1 ) ; quicksort ( a, i + 1, upper ) ; } } int split ( int a[ ], int lower, int upper ) { int i, p, q, t ; p = lower + 1 ; q = upper ; i = a[lower] ; while ( q >= p ) { while ( a[p] < i ) p++ ; while ( a[q] > i ) q-- ; if ( q > p ) { t = a[p] ; a[p] = a[q] ; a[q] = t ; } } t = a[lower] ; a[lower] = a[q] ;

a[q] = t ; return q ; }

/* Insertion sort. */ #include <stdio.h> #include <conio.h> void main( ) { int arr[5] = { 25, 17, 31, 13, 2 } ; int i, j, k, temp ; clrscr( ) ; printf ( "Insertion sort.\n" ) ; printf ( "\nArray before sorting:\n") ; for ( i = 0 ; i <= 4 ; i++ ) printf ( "%d\t", arr[i] ) ; for ( i = 1 ; i <= 4 ; i++ ) { for ( j = 0 ; j < i ; j++ ) { if ( arr[j] > arr[i] ) { temp = arr[j] ; arr[j] = arr[i] ; for ( k = i ; k > j ; k-- ) arr[k] = arr[k - 1] ; arr[k + 1] = temp ; } } } printf ( "\n\nArray after sorting:\n") ; for ( i = 0 ; i <= 4 ; i++ ) printf ( "%d\t", arr[i] ) ; getch( ) ;}

/* Program that implements breadth first search algorithm. */ #include <stdio.h> #include <conio.h> #include <alloc.h> #define TRUE 1 #define FALSE 0 #define MAX 8 struct node { int data ; struct node *next ; }; int visited[MAX] ; int q[8] ; int front, rear ; void bfs ( int, struct node ** ) ; struct node * getnode_write ( int ) ; void addqueue ( int ) ; int deletequeue( ) ; int isempty( ) ; void del ( struct node * ) ; void main( ) { struct node *arr[MAX] ; struct node *v1, *v2, *v3, *v4 ; int i ; clrscr( ) ; v1 = getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = getnode_write ( 3 ); v2 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = getnode_write ( 4 ); v2 -> next = v3 = getnode_write ( 5 ); v3 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = getnode_write ( 6 ) ; v2 -> next = v3 = getnode_write ( 7 ) ; v3 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[3] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[4] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ;

v1 = getnode_write ( 3 ) ; arr[5] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ; arr[6] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 4 ) ; arr[7] = v1 ; v1 -> next = v2 = getnode_write ( 5 ) ; v2 -> next = v3 = getnode_write ( 6 ) ; v3 -> next = v4 = getnode_write ( 7 ) ; v4 -> next = NULL ; front = rear = -1 ; bfs ( 1, arr ) ; for ( i = 0 ; i < MAX ; i++ ) del ( arr[i] ) ; getch( ) ; } void bfs ( int v, struct node **p ) { struct node *u ; visited[v - 1] = TRUE ; printf ( "%d\t", v ) ; addqueue ( v ) ; while ( isempty( ) == FALSE ) { v = deletequeue( ) ; u=*(p+v-1); while ( u != NULL ) { if ( visited [ u -> data - 1 ] == FALSE ) { addqueue ( u -> data ) ; visited [ u -> data - 1 ] = TRUE ; printf ( "%d\t", u -> data ) ; } u = u -> next ; } } } struct node * getnode_write ( int val ) { struct node *newnode ; newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ; newnode -> data = val ; return newnode ; } void addqueue ( int vertex ) { if ( rear == MAX - 1 )

{ printf ( "\nQueue Overflow." ) ; exit( ) ; } rear++ ; q[rear] = vertex ; if ( front == -1 ) front = 0 ; } int deletequeue( ) { int data ; if ( front == -1 ) { printf ( "\nQueue Underflow." ) ; exit( ) ; } data = q[front] ; if ( front == rear ) front = rear = -1 ; else front++ ; return data ; } int isempty( ) { if ( front == -1 ) return TRUE ; return FALSE ; } void del ( struct node *n ) { struct node *temp ; while ( n != NULL ) {temp = n -> next ; free ( n ) ; n = temp ; } }

/*Program that implements depth first search algorithm. */ #include <stdio.h> #include <conio.h> #include <alloc.h> #define TRUE 1 #define FALSE 0 #define MAX 8 struct node { int data ; struct node *next ; }; int visited[MAX] ; void dfs ( int, struct node ** ) ; struct node * getnode_write ( int ) ; void del ( struct node * ) ; void main( ) { struct node *arr[MAX] ; struct node *v1, *v2, *v3, *v4 ; int i ; clrscr( ) ; v1 = getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = getnode_write ( 3 ) ; v2 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = getnode_write ( 4 ) ; v2 -> next = v3 = getnode_write ( 5 ) ; v3 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = getnode_write ( 6 ) ; v2 -> next = v3 = getnode_write ( 7 ) ; v3 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[3] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[4] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ; arr[5] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ; arr[6] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ;

v1 = getnode_write ( 4 ) ; arr[7] = v1 ; v1 -> next = v2 = getnode_write ( 5 ) ; v2 -> next = v3 = getnode_write ( 6 ) ; v3 -> next = v4 = getnode_write ( 7 ) ; v4 -> next = NULL ; dfs ( 1, arr ) ; for ( i = 0 ; i < MAX ; i++ ) del ( arr[i] ) ; getch( ) ; } void dfs ( int v, struct node **p ) { struct node *q ; visited[v - 1] = TRUE ; printf ( "%d\t", v ) ; q=*(p+v-1); while ( q != NULL ) { if ( visited[q -> data - 1] == FALSE ) dfs ( q -> data, p ) ; else q = q -> next ; } } struct node * getnode_write ( int val ) { struct node *newnode ; newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ; newnode -> data = val ; return newnode ; } void del ( struct node *n ) { struct node *temp ; while ( n != NULL ) { temp = n -> next ; free ( n ) ; n = temp ; }

//polynomial #include <stdio.h> #include <conio.h> #include <alloc.h> struct polynode { float coeff ; int exp ; struct polynode *link ; }; void poly_append ( struct polynode **, float, int ) ; void display_poly ( struct polynode * ) ; void poly_add ( struct polynode *, struct polynode *, struct polynode ** ) ; void main( ) { struct polynode *first, *second, *total ; int i = 0 ; first = second = total = NULL ; /* empty linked lists */ poly_append ( &first, 1.4, 5 ) ; poly_append ( &first, 1.5, 4 ) ; poly_append ( &first, 1.7, 2 ) ; poly_append ( &first, 1.8, 1 ) ; poly_append ( &first, 1.9, 0 ) ; clrscr( ) ; display_poly ( first ) ; poly_append ( &second, 1.5, 6 ) ; poly_append ( &second, 2.5, 5 ) ; poly_append ( &second, -3.5, 4 ) ; poly_append ( &second, 4.5, 3 ) ; poly_append ( &second, 6.5, 1 ) ; printf ( "\n\n" ) ; display_poly ( second ) ; /* draws a dashed horizontal line */ printf ( "\n" ) ; while ( i++ < 79 ) printf ( "-" ) ; printf ( "\n\n" ) ; poly_add ( first, second, &total ) ; display_poly ( total ) ; /* displays the resultant polynomial */ } void poly_append ( struct polynode **q, float x, int y ) { struct polynode *temp ; temp = *q ; if ( *q == NULL ) {

*q = malloc ( sizeof ( struct polynode ) ) ; temp = *q ; } else { while ( temp -> link != NULL ) temp = temp -> link ; temp -> link = malloc ( sizeof ( struct polynode ) ) ; temp = temp -> link ; } temp -> coeff = x ; temp -> exp = y ; temp -> link = NULL ; } void display_poly ( struct polynode *q ) { /* traverse till the end of the linked list */ while ( q != NULL ) { printf ( "%.1f x^%d : ", q -> coeff, q -> exp ) ; q = q -> link ; } printf ( "\b\b\b " ) ; } void poly_add ( struct polynode *x, struct polynode *y, { struct polynode *z ; /* if both linked lists are empty */ if ( x == NULL && y == NULL ) return ; /* traverse till one of the list ends */ while ( x != NULL && y != NULL ) { if ( *s == NULL ) { *s = malloc ( sizeof ( struct polynode ) ) ; z = *s ; } else { z -> link = malloc ( sizeof ( struct polynode ) ) ; z = z -> link ; } if ( x -> exp < y -> exp ) { z -> coeff = y -> coeff ; z -> exp = y -> exp ; y = y -> link ; else struct polynode **s )

{ if ( x -> exp > y -> exp ) { z -> coeff = x -> coeff ; z -> exp = x -> exp ; x = x -> link ; /* go to the next node */ } else { if ( x -> exp == y -> exp ) { z -> coeff = x -> coeff + y -> coeff ; z -> exp = x -> exp ; /* go to the next node */ x = x -> link ; y = y -> link ; } } } } while ( x != NULL ) { if ( *s == NULL ) { *s = malloc ( sizeof ( struct polynode ) ) ; z = *s ; } else { z -> link = malloc ( sizeof ( struct polynode ) ) ; z = z -> link ; } z -> coeff = x -> coeff ; z -> exp = x -> exp ; x = x -> link ; } while ( y != NULL ) { if ( *s == NULL ) { *s = malloc ( sizeof ( struct polynode ) ) ; z = *s ; } else { z -> link = malloc ( sizeof ( struct polynode ) ) ; z = z -> link ; } z -> coeff = y -> coeff ; z -> exp = y -> exp ;

y = y -> link ; } z -> link = NULL ; }

Potrebbero piacerti anche