Posts

Showing posts from 2009

Program on deque that implements a linked list.

Program on deque that implements a linked list. #include " " #include " " #include " " struct node { int data ; struct node *link ; } ; struct dqueue { struct node *front ; struct node *rear ; } ; void initdqueue ( struct dqueue * ) ; void addqatend ( struct dqueue *, int item ) ; void addqatbeg ( struct dqueue *, int item ) ; int delqatbeg ( struct dqueue * ) ; int delqatend ( struct dqueue * ) ; void display ( struct dqueue ) ; int count ( struct dqueue ) ; void deldqueue ( struct dqueue * ) ; void main( ) { struct dqueue dq ; int i, n ; clrscr( ) ; initdqueue ( &dq ) ; addqatend ( &dq, 11 ) ; addqatbeg ( &dq, 10 ) ; addqatend ( &dq, 12 ) ; addqatbeg ( &dq, 9 ) ; addqatend ( &dq, 13 ) ; addqatbeg ( &dq, 8 ) ; addqatend ( &dq, 14 ) ; addqatbeg ( &dq, 7 ) ; display ( dq ) ; n = count ( dq ) ; printf ( "\nTotal elements: %d", n ) ; ...

Program to find the shortest path

Program to find the shortest path #include " " #include " " #define INF 9999 void main( ) { int arr[4][4] ; int cost[4][4] = { 7, 5, 0, 0, 7, 0, 0, 2, 0, 3, 0, 0, 4, 0, 1, 0 } ; int i, j, k, n = 4 ; clrscr( ) ; for ( i = 0 ; i { for ( j = 0; j { if ( cost[i][j] == 0 ) arr[i][j] = INF ; else arr[i][j] = cost[i][j] ; } } printf ( "Adjacency matrix of cost of edges:\n" ) ; for ( i = 0 ; i { for ( j = 0; j printf ( "%d\t", arr[i][j] ) ; printf ( "\n" ) ; } for ( k = 0 ; k { for ( i = 0 ; i { for ( j = 0 ; j { if ( arr[i][j] > arr[i][k] + arr[k][j] ) arr[i][j] = arr[i][k] + arr[k][j]; ...

minimum cost of a spanning tree.

minimum cost of a spanning tree. #include "stdio.h" #include "conio.h" #include "alloc.h" struct lledge { int v1, v2 ; float cost ; struct lledge *next ; } ; int stree[5] ; int count[5] ; int mincost ; struct lledge * kminstree ( struct lledge *, int ) ; int getrval ( int ) ; void combine ( int, int ) ; void del ( struct lledge * ) ; void main( ) { struct lledge *temp, *root ; int i ; clrscr( ) ; root = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ; root -> v1 = 4 ; root -> v2 = 3 ; root -> cost = 1 ; temp = root -> next = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ; temp -> v1 = 4 ; temp -> v2 = 2 ; temp -> cost = 2 ; temp -> next = ( struct lledge * ) malloc ( sizeof ( struct lledge ) ) ; temp = temp -> next ; temp -> v1 = 3 ; temp -> v2 = 2 ; temp -> cost = 3 ; temp -> next = ( struct lledge * ) malloc ( sizeof ...

Breadth first search algorithm.

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 = NUL...

depth first search algorithm.

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...

Program that creates random numbers in a given file

Program that creates random numbers in a given file. #include "stdio.h" #include "conio.h" #include "stdlib.h" void main( ) { FILE *fp ; char str [ 67 ] ; int i, noofr, j ; clrscr( ) ; printf ( "Enter file name: " ) ; scanf ( "%s", str ) ; printf ( "Enter number of records: " ) ; scanf ( "%d", &noofr ) ; fp = fopen ( str, "wb" ) ; if ( fp == NULL ) { printf ( "Unable to create file." ) ; getch( ) ; exit ( 0 ) ; } randomize( ) ; for ( i = 0 ; i { j = random ( 1000 ) ; fwrite ( &j, sizeof ( int ), 1, fp ) ; printf ( "%d\t", j ) ; } fclose ( fp ) ; printf ( "\nFile is created. \nPress any key to continue." ) ; getch( ) ; }

External Sorting.

External Sorting. #include "stdio.h" #include "conio.h" #include "stdlib.h" void shownums ( char * ) ; void split ( char * ) ; void sort ( char * ) ; void main( ) { char str[67] ; clrscr( ) ; printf ( "Enter file name: " ) ; scanf ( "%s", str ) ; printf ( "Numbers before sorting:\n" ) ; shownums ( str ) ; split ( str ) ; sort ( str ) ; printf ( "\nNumbers after sorting:\n" ) ; shownums ( str ) ; getch( ) ; } /* Displays the contents of file */ void shownums ( char *p ) { FILE *fp ; int i ; fp = fopen ( p, "rb" ) ; if ( fp == NULL ) { printf ( "Unable to open file." ) ; getch( ) ; exit ( 0 ) ; } while ( fread ( &i, sizeof ( int ), 1, fp ) != 0 ) printf ( "%d \t", i ) ; fclose ( fp ) ; } /* Splits the original file into two files */ void split ( char *p ) { FILE *fs, *ft ; ...

Merge Sort.

Merge Sort. #include #include 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 printf ( "%d\t", a[i] ) ; printf ( "\n\nSecond array:\n" ) ; for ( i = 0 ; i printf ( "%d\t", b[i] ) ; for ( i = 0 ; i { for ( j = i + 1 ; 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 { if ( a[j] c[i++] = a[j++] ; else c[i++] = b[k++] ; if ( j == 5 || k == 5 ) brea...

Heap Sort.

Heap Sort. #include "stdio.h" #include "conio.h" void makeheap ( int [ ], int ) ; void heapsort ( int [ ], int ) ; void main( ) { int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ; int i ; clrscr( ) ; printf ( "Heap Sort.\n" ) ; makeheap ( arr, 10 ) ; printf ( "\nBefore Sorting:\n" ) ; for ( i = 0 ; i printf ( "%d\t", arr[i] ) ; heapsort ( arr, 10 ) ; printf ( "\nAfter Sorting:\n" ) ; for ( i = 0 ; i printf ( "%d\t", arr[i] ) ; getch( ); } void makeheap ( int x[ ], int n ) { int i, val, s, f ; for ( i = 1 ; i { val = x[i] ; s = i ; f = ( s - 1 ) / 2 ; while ( s > 0 && x[f] { x[s] = x[f] ; s = f ; f = ( s - 1 ) / 2 ; } x[s] = val ; } } void heapsort ( int x[ ], int n ) { int i, s, f, ivalue ; for ( i = n - 1 ; i > 0 ; i-- ) ...

Binary Tree Sorting.

Binary Tree Sorting. #include "stdio.h" #include "conio.h" #include "alloc.h" struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void inorder ( struct btreenode * ) ; void main( ) { struct btreenode *bt ; int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ; int i ; bt = NULL ; clrscr( ) ; printf ( "Binary tree sort.\n" ) ; printf ( "\nArray:\n" ) ; for ( i = 0 ; i printf ( "%d\t", arr[i] ) ; for ( i = 0 ; i insert ( &bt, arr[i] ) ; printf ( "\nIn-order traversal of binary tree:\n" ) ; inorder ( bt ) ; getch( ) ; } void insert ( struct btreenode **sr, int num ) { if ( *sr == NULL ) { *sr = malloc ( sizeof ( struct btreenode ) ) ; ( *sr ) -> leftchild = NULL ; ( *sr ) -> data = num ; ( *sr ) -> rightchi...

Insertion sort.

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 printf ( "%d\t", arr[i] ) ; for ( i = 1 ; i { for ( j = 0 ; 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 printf ( "%d\t", arr[i] ) ; getch( ) ; }

Quick sort

Quick sort #include "conio.h " #include "stdio.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 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 ) 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 ; }

Selection sort.

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

Bubble sort.

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

Binary search in a sorted array.

Binary search in a sorted array. #include " " #include " " void main( ) { int arr[10] = { 1, 2, 3, 9, 11, 13, 17, 25, 57, 90 } ; int mid, lower = 0 , upper = 9, num, flag = 1 ; clrscr( ) ; printf ( "Enter number to search: " ) ; scanf ( "%d", &num ) ; for ( mid = ( lower + upper ) / 2 ; lower mid = ( lower + upper ) / 2 ) { if ( arr[mid] == num ) { printf ( "The number is at position %d in the array.", mid ) ; flag = 0 ; break ; } if ( arr[mid] > num ) upper = mid - 1 ; else lower = mid + 1 ; } if ( flag ) printf ( "Element is not present in the array." ) ; getch( ) ; }

Linear search in a sorted array.

Linear search in a sorted array. #include " " #include " " void main( ) { int arr[10] = { 1, 2, 3, 9, 11, 13, 17, 25, 57, 90 } ; int i, num ; clrscr( ) ; printf ( "Enter number to search: " ) ; scanf ( "%d", &num ) ; for ( i = 0 ; i { if ( arr[9] = num ) { if ( arr[i] == num ) printf ( "The number is at position %d in the array.", i ) ; else printf ( "Number is not present in the array." ) ; break ; } } getch( ) ; }

Linear search in an unsorted array.

Linear search in an unsorted array. #include #include " " void main( ) { int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ; int i, num ; clrscr( ) ; printf ( "Enter number to search: " ) ; scanf ( "%d", &num ) ; for ( i = 0 ; i { if ( arr[i] == num ) break ; } if ( i == 10 ) printf ( "Number is not present in the array." ) ; else printf ( "The number is at position %d in the array.", i ) ; getch( ) ; }

Program to maintain a heap.

#include #include void restoreup ( int, int * ) ; void restoredown ( int, int *, int ) ; void makeheap ( int *, int ) ; void add ( int, int *, int * ) ; int replace ( int, int *, int ) ; int del ( int *, int * ) ; void main( ) { int arr [20] = { 1000, 7, 10, 25, 17, 23, 27, 16, 19, 37, 42, 4, 33, 1, 5, 11 } ; int i, n = 15 ; clrscr( ) ; makeheap ( arr, n ) ; printf ( "Heap:\n" ) ; for ( i = 1 ; i printf ( "%d\t", arr [i] ) ; i = 24 ; add ( i, arr, &n ) ; printf ( "\n\nElement added %d.\n", i ) ; printf ( "\nHeap after addition of an element:\n" ) ; for ( i = 1 ; i printf ( "%d\t", arr [i] ) ; i = replace ( 2, arr, n ) ; printf ( "\n\nElement replaced %d.\n", i ) ; printf ( "\nHeap after replacement of an element:\n" ) ; for ( i = 1 ; i printf ( "%d\t", arr [i] ) ; i = del ( arr, &n ) ; printf ( "\n\nElement deleted %d.\n", i ) ; printf ( "\nHeap after dele...

Program which maintains a B-tree of order 5.

#include #include #include #include #define MAX 4 #define MIN 2 struct btnode { int count ; int value[MAX + 1] ; struct btnode *child[MAX + 1] ; } ; struct btnode * insert ( int, struct btnode * ) ; int setval ( int, struct btnode *, int *, struct btnode ** ) ; struct btnode * search ( int, struct btnode *, int * ) ; int searchnode ( int, struct btnode *, int * ) ; void fillnode ( int, struct btnode *, struct btnode *, int ) ; void split ( int, struct btnode *, struct btnode *, int, int *, struct btnode ** ) ; struct btnode * delete ( int, struct btnode * ) ; int delhelp ( int, struct btnode * ) ; void clear ( struct btnode *, int ) ; void copysucc ( struct btnode *, int ) ; void restore ( struct btnode *, int ) ; void rightshift ( struct btnode *, int ) ; void leftshift ( struct btnode *, int ) ; void merge ( struct btnode *, int ) ; void display ( struct btnode * ) ; void main( ) { struct node *root ; root = NULL ; clrscr( ) ; root = insert ( 27, root ) ; root = insert ( ...

Program to maintain an AVL tree.

#include #include #include #define FALSE 0 #define TRUE 1 struct AVLNode { int data ; int balfact ; struct AVLNode *left ; struct AVLNode *right ; } ; struct AVLNode * buildtree ( struct AVLNode *, int, int * ) ; struct AVLNode * deldata ( struct AVLNode *, int, int * ) ; struct AVLNode * del ( struct AVLNode *, struct AVLNode *, int * ) ; struct AVLNode * balright ( struct AVLNode *, int * ) ; struct AVLNode * balleft ( struct AVLNode *, int * ) ; void display ( struct AVLNode * ) ; void deltree ( struct AVLNode * ) ; void main( ) { struct AVLNode *avl = NULL ; int h ; clrscr( ) ; avl = buildtree ( avl, 20, &h ) ; avl = buildtree ( avl, 6, &h ) ; avl = buildtree ( avl, 29, &h ) ; avl = buildtree ( avl, 5, &h ) ; avl = buildtree ( avl, 12, &h ) ; avl = buildtree ( avl, 25, &h ) ; avl = buildtree ( avl, 32, &h ) ; avl = buildtree ( avl, 10, &h ) ; avl = buildtree ( avl, 15, &h ) ; avl = buildtree ( avl, 27, &h ) ; avl = buildtree...

Program to maintain a threaded binary tree.

#include #include #include enum boolean { false = 0, true = 1 } ; struct thtree { enum boolean isleft ; struct thtree *left ; int data ; struct thtree *right ; enum boolen isright ; } ; void insert ( struct thtree **, int ) ; void delete ( struct thtree **, int ) ; void search ( struct thtree **, int, struct thtree **, struct thtree **, int * ) ; void inorder ( struct thtree * ) ; void deltree ( struct thtree ** ) ; void main( ) { struct thtree *th_head ; th_head = NULL ; /* empty tree */ insert ( &th_head, 11 ) ; insert ( &th_head, 9 ) ; insert ( &th_head, 13 ) ; insert ( &th_head, 8 ) ; insert ( &th_head, 10 ) ; insert ( &th_head, 12 ) ; insert ( &th_head, 14 ) ; insert ( &th_head, 15 ) ; insert ( &th_head, 7 ) ; clrscr( ) ; printf ( "Threaded binary tree before deletion:\n" ) ; inorder ( th_head ) ; delete ( &th_head, 10 ) ; printf ( "\nThreaded binary tree after deletion:\n" ) ; inorder ( th_he...

Program to insert and delete a node from the binary search tree.

#include #include #include #define TRUE 1 #define FALSE 0 struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void delete ( struct btreenode **, int ) ; void search ( struct btreenode **, int, struct btreenode **, struct btreenode **, int * ) ; void inorder ( struct btreenode * ) ; void main( ) { struct btreenode *bt ; int req, i = 0, num, a[ ] = { 11, 9, 13, 8, 10, 12, 14, 15, 7 } ; bt = NULL ; /* empty tree */ clrscr( ) ; while ( i { insert ( &bt, a[i] ) ; i++ ; } clrscr( ) ; printf ( "Binary tree before deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 10 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 14 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 8 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 13 ) ; ...

Program to implement a binary search tree.

#include #include #include struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void inorder ( struct btreenode * ) ; void preorder ( struct btreenode * ) ; void postorder ( struct btreenode * ) ; void main( ) { struct btreenode *bt ; int req, i = 1, num ; bt = NULL ; /* empty tree */ clrscr( ) ; printf ( "Specify the number of items to be inserted: " ) ; scanf ( "%d", &req ) ; while ( i++ { printf ( "Enter the data: " ) ; scanf ( "%d", &num ) ; insert ( &bt, num ) ; } printf ( "\nIn-order Traversal: " ) ; inorder ( bt ) ; printf ( "\nPre-order Traversal: " ) ; preorder ( bt ) ; printf ( "\nPost-order Traversal: " ) ; postorder ( bt ) ; } /* inserts a new node in a binary search tree */ void insert ( struct btreenode **sr, int num ) { if ( *sr == NULL ) { *sr = malloc ( sizeof ( struct btre...

Program to build a binary search tree from an array.

#include #include #include struct node { struct node *left ; char data ; struct node *right ; } ; struct node * buildtree ( int ) ; void inorder ( struct node * ) ; char a[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' } ; void main( ) { struct node *root ; clrscr( ) ; root = buildtree ( 0 ) ; printf ( "In-order Traversal:\n" ) ; inorder ( root ) ; getch( ) ; } struct node * buildtree ( int n ) { struct node *temp = NULL ; if ( a[n] != '\0' ) { temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; temp -> left = buildtree ( 2 * n + 1 ) ; temp -> data = a[n] ; temp -> right = buildtree ( 2 * n + 2 ) ; } return temp ; } void inorder ( struct node *root ) { if ( root != NULL ) { ...

Program to build a binary search tree from arrays.

#include #include #include struct node { struct node *left ; char data ; struct node *right ; } ; struct node * buildtree ( int ) ; void inorder ( struct node * ) ; char arr[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' } ; int lc[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 } ; int rc[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 } ; void main( ) { struct node *root ; clrscr( ) ; root = buildtree ( 0 ) ; printf ( ā€œIn-order Traversal:\nā€ ) ; inorder ( root ) ; getch( ) ; } struct node * buildtree ( int index ) { struct node *temp = NULL ; if ( index != -1 ) { temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; temp -> left = buildtree ( lc[index] ) ; temp -> data = arr[index] ; temp -> right = buildtree ( rc[index] ) ; } return temp ; } void inorder ( struct node *root ) { if ( root != NULL ) { inorder ( root -> left ) ;...

Program to insert and delete a node from the binary search tree

#include #include #include #define TRUE 1 #define FALSE 0 struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void delete ( struct btreenode **, int ) ; void search ( struct btreenode **, int, struct btreenode **, struct btreenode **, int * ) ; void inorder ( struct btreenode * ) ; void main( ) { struct btreenode *bt ; int req, i = 0, num, a[ ] = { 11, 9, 13, 8, 10, 12, 14, 15, 7 } ; bt = NULL ; /* empty tree */ clrscr( ) ; while ( i { insert ( &bt, a[i] ) ; i++ ; } clrscr( ) ; printf ( "Binary tree before deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 10 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 14 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 8 ) ; printf ( "\nBinary tree after deletion:\n" ) ; inorder ( bt ) ; delete ( &bt, 13 ) ; ...

Program to reconstruct a binary search tree.

#include #include #include #define MAX 101 struct node { struct node *left ; int data ; struct node *right ; } ; void insert ( struct node **, int ) ; void preorder ( struct node * ) ; void postorder ( struct node * ) ; void inorder ( struct node * ) ; struct node * recons ( int *, int *, int ) ; void deltree ( struct node * ) ; int in[MAX], pre[MAX], x ; void main( ) { struct node *t, *p, *q ; int req, i, num ; t = NULL ; /* empty tree */ clrscr( ) ; printf ( "Specify the number of items to be inserted: " ) ; while ( 1 ) { scanf ( "%d", &req ) ; if ( req >= MAX || req printf ( "\nEnter number between 1 to 100.\n" ) ; else break ; } for ( i = 0 ; i { printf ( "Enter the data: " ) ; scanf ( "%d", &num ) ; insert ( &t, num ) ; } printf ( "\nIn-order Traversal:\n" ) ; x = 0 ; inorder ( t ) ; printf ( "\nPre-order Traversal:\n" ) ; x = 0 ; preorder ( t ) ; printf ...

Program that implements a priority queue using an array.

#include #include #define MAX 5 struct data { char job[MAX] ; int prno ; int ord ; } ; struct pque { struct data d[MAX] ; int front ; int rear ; } ; void initpque ( struct pque * ) ; void add ( struct pque *, struct data ) ; struct data delete ( struct pque * ) ; void main( ) { struct pque q ; struct data dt, temp ; int i, j = 0 ; clrscr( ) ; initpque ( &q ) ; printf ( "Enter Job description (max 4 chars) and its priority\n" ) ; printf ( "Lower the priority number, higher the priority\n" ) ; printf ( "Job Priority\n" ) ; for ( i = 0 ; i { scanf ( "%s %d", &dt.job, &dt.prno ) ; dt.ord = j++ ; add ( &q, dt ) ; } printf ( "\n" ) ; printf ( "Process jobs prioritywise\n" ) ; printf ( "Job\tPriority\n" ) ; for ( i = 0 ; i { temp = delete ( &q ) ; printf ( "%s\t%d\n", temp.job, temp.prno ) ; } printf ( "\n" ) ; getch( ) ; } /* initialises data m...

Program that implements deque using an array.

#include #include #define MAX 10 void addqatbeg ( int *, int, int *, int * ) ; void addqatend ( int *, int, int *, int * ) ; int delqatbeg ( int *, int *, int * ) ; int delqatend ( int *, int *, int * ) ; void display ( int * ) ; int count ( int * ) ; void main( ) { int arr[MAX] ; int front, rear, i, n ; clrscr( ) ; /* initialises data members */ front = rear = -1 ; for ( i = 0 ; i arr[i] = 0 ; addqatend ( arr, 17, &front, &rear ) ; addqatbeg ( arr, 10, &front, &rear ) ; addqatend ( arr, 8, &front, &rear ) ; addqatbeg ( arr, -9, &front, &rear ) ; addqatend ( arr, 13, &front, &rear ) ; addqatbeg ( arr, 28, &front, &rear ) ; addqatend ( arr, 14, &front, &rear ) ; addqatbeg ( arr, 5, &front, &rear ) ; addqatend ( arr, 25, &front, &rear ) ; addqatbeg ( arr, 6, &front, &rear ) ; addqatend ( arr, 21, &front, &rear ) ; addqatbeg ( arr, 11, &front, &rear ) ; printf ( "\nEle...

Program that implements circular queue as an array.

#include #include #define MAX 10 void addq ( int *, int, int *, int * ) ; int delq ( int *, int *, int * ) ; void display ( int * ) ; void main( ) { int arr[MAX] ; int i, front, rear ; clrscr( ) ; /* initialise data member */ front = rear = -1 ; for ( i = 0 ; i arr[i] = 0 ; addq ( arr, 14, &front, &rear ) ; addq ( arr, 22, &front, &rear ) ; addq ( arr, 13, &front, &rear ) ; addq ( arr, -6, &front, &rear ) ; addq ( arr, 25, &front, &rear ) ; printf ( "\nElements in the circular queue: " ) ; display ( arr ) ; i = delq ( arr, &front, &rear ) ; printf ( "Item deleted: %d", i ) ; i = delq ( arr, &front, &rear ) ; printf ( "\nItem deleted: %d", i ) ; printf ( "\nElements in the circular queue after deletion: " ) ; display ( arr ) ; addq ( arr, 21, &front, &rear ) ; addq ( arr, 17, &front, &rear ) ; addq ( arr, 18, &front, &rear ) ; addq ( arr, 9, ...

Program that implements queue as a linked list.

#include #include struct node { int data ; struct node *link ; } ; struct queue { struct node *front ; struct node *rear ; } ; void initqueue ( struct queue * ) ; void addq ( struct queue *, int ) ; int delq ( struct queue * ) ; void delqueue ( struct queue * ) ; void main( ) { struct queue a ; int i ; clrscr( ) ; initqueue ( &a ) ; addq ( &a, 11 ) ; addq ( &a, -8 ) ; addq ( &a, 23 ) ; addq ( &a, 19 ) ; addq ( &a, 15 ) ; addq ( &a, 16 ) ; addq ( &a, 28 ) ; i = delq ( &a ) ; printf ( "\nItem extracted: %d", i ) ; i = delq ( &a ) ; printf ( "\nItem extracted: %d", i ) ; i = delq ( &a ) ; printf ( "\nItem extracted: %d", i ) ; delqueue ( &a ) ; getch( ) ; } /* initialises data member */ void initqueue ( struct queue *q ) { q -> front = q -> rear = NULL ; } /* adds an element to the queue */ void addq ( struct queue *q, int item ) { struct node *temp ; temp = ( struct node * ) mall...