Posts

Showing posts from May 31, 2009

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

Program that implements queue as an array.

#include #include #define MAX 10 void addq ( int *, int, int *, int * ) ; int delq ( int *, int *, int * ) ; void main( ) { int arr[MAX] ; int front = -1, rear = -1, i ; clrscr( ) ; addq ( arr, 23, &front, &rear ) ; addq ( arr, 9, &front, &rear ) ; addq ( arr, 11, &front, &rear ) ; addq ( arr, -10, &front, &rear ) ; addq ( arr, 25, &front, &rear ) ; addq ( arr, 16, &front, &rear ) ; addq ( arr, 17, &front, &rear ) ; addq ( arr, 22, &front, &rear ) ; addq ( arr, 19, &front, &rear ) ; addq ( arr, 30, &front, &rear ) ; addq ( arr, 32, &front, &rear ) ; i = delq ( arr, &front, &rear ) ; printf ( "\nItem deleted: %d", i ) ; i = delq ( arr, &front, &rear ) ; printf ( "\nItem deleted: %d", i ) ; i = delq ( arr, &front, &rear ) ; printf ( "\nItem deleted: %d", i ) ; getch( ) ; } /* adds an element to the queue */ void addq ( int *arr...

Program to evaluate an epression entered in postfix form

#include #include #include #include #include #define MAX 50 struct postfix { int stack[MAX] ; int top, nn ; char *s ; } ; void initpostfix ( struct postfix * ) ; void setexpr ( struct postfix *, char * ) ; void push ( struct postfix *, int ) ; int pop ( struct postfix * ) ; void calculate ( struct postfix * ) ; void show ( struct postfix ) ; void main( ) { struct postfix q ; char expr[MAX] ; clrscr( ) ; initpostfix ( &q ) ; printf ( "\nEnter postfix expression to be evaluated: " ) ; gets ( expr ) ; setexpr ( &q, expr ) ; calculate ( &q ) ; show ( q ) ; getch( ) ; } /* initializes data members */ void initpostfix ( struct postfix *p ) { p -> top = -1 ; } /* sets s to point to the given expr. */ void setexpr ( struct postfix *p, char *str ) { p -> s = str ; } /* adds digit to the stack */ void push ( struct postfix *p, int item ) { if ( p -> top == MAX - 1 ) printf ( "\nStack is full." ) ; else { p -> top++ ; p -> sta...

Program to convert an expression in postfix form to an infix form

#include #include #include #define MAX 50 struct postfix { char stack[MAX][MAX], target[MAX] ; char temp1[2], temp2[2] ; char str1[MAX], str2[MAX], str3[MAX] ; int i, top ; } ; void initpostfix ( struct postfix * ) ; void setexpr ( struct postfix *, char * ) ; void push ( struct postfix *, char * ) ; void pop ( struct postfix *, char * ) ; void convert ( struct postfix * ) ; void show ( struct postfix ) ; void main( ) { struct postfix q ; char expr[MAX] ; clrscr( ) ; initpostfix ( &q ) ; printf ( "\nEnter an expression in postfix form: " ) ; gets ( expr ) ; setexpr ( &q, expr ) ; convert ( &q ) ; printf ( "\nThe infix expression is: " ) ; show ( q ) ; getch( ) ; } /* initializes data member */ void initpostfix ( struct postfix *p ) { p -> i = 0 ; p -> top = -1 ; strcpy ( p -> target, "" ) ; } /* copies given expression to target string */ void setexpr ( struct postfix *p, char *c ) { strcpy ( p -> target, c ) ; } ...

Program to convert expression in postfix form to prefix form

#include #include #include #define MAX 50 struct postfix { char stack[MAX][MAX], target[MAX] ; char temp1[2], temp2[2] ; char str1[MAX], str2[MAX], str3[MAX] ; int i, top ; } ; void initpostfix ( struct postfix * ) ; void setexpr ( struct postfix *, char * ) ; void push ( struct postfix *, char * ) ; void pop ( struct postfix *, char * ) ; void convert ( struct postfix * ) ; void show ( struct postfix ) ; void main( ) { struct postfix q ; char expr[MAX] ; clrscr( ) ; initpostfix ( &q ) ; printf ( "\nEnter an expression in postfix form: " ) ; gets ( expr ) ; setexpr ( &q, expr ) ; convert ( &q ) ; printf ( "\nThe Prefix expression is: " ) ; show ( q ) ; getch( ) ; } /* initializes the elements of the structure */ void initpostfix ( struct postfix *p ) { p -> i = 0 ; p -> top = -1 ; strcpy ( p -> target, "" ) ; } /* copies given expr. to target string */ void setexpr ( struct postfix *p, char *c ) { strcpy ( p -> ta...

Program to convert an Infix form to Postfix form

#include #include #include #include #define MAX 50 struct infix { char target[MAX] ; char stack[MAX] ; char *s, *t ; int top ; } ; void initinfix ( struct infix * ) ; void setexpr ( struct infix *, char * ) ; void push ( struct infix *, char ) ; char pop ( struct infix * ) ; void convert ( struct infix * ) ; int priority ( char ) ; void show ( struct infix ) ; void main( ) { struct infix p ; char expr[MAX] ; initinfix ( &p ) ; clrscr( ) ; printf ( "\nEnter an expression in infix form: " ) ; gets ( expr ) ; setexpr ( &p, expr ) ; convert ( &p ) ; printf ( "\nThe postfix expression is: " ) ; show ( p ) ; getch( ) ; } /* initializes structure elements */ void initinfix ( struct infix *p ) { p -> top = -1 ; strcpy ( p -> target, "" ) ; strcpy ( p -> stack, "" ) ; p -> t = p -> target ; p -> s = "" ; } /* sets s to point to given expr. */ void setexpr ( struct infix *p, char *...

Program to convert an Infix expression to Prefix form.

#include #include #include #include #define MAX 50 struct infix { char target[MAX] ; char stack[MAX] ; char *s, *t ; int top, l ; } ; void initinfix ( struct infix * ) ; void setexpr ( struct infix *, char * ) ; void push ( struct infix *, char ) ; char pop ( struct infix * ) ; void convert ( struct infix * ) ; int priority ( char c ) ; void show ( struct infix ) ; void main( ) { struct infix q ; char expr[MAX] ; clrscr( ) ; initinfix ( &q ) ; printf ( "\nEnter an expression in infix form: " ) ; gets ( expr ) ; setexpr ( &q, expr ) ; convert ( &q ) ; printf ( "The Prefix expression is: " ) ; show ( q ) ; getch( ) ; } /* initializes elements of structure variable */ void initinfix ( struct infix *pq ) { pq -> top = -1 ; strcpy ( pq -> target, "" ) ; strcpy ( pq -> stack, "" ) ; pq -> l = 0 ; } /* reverses the given expression */ void setexpr ( struct infix *pq, char *str ) { pq -> s = str ; str...

Program implements linked list as a stack.

#include #include #include /* structure containing data part and linkpart */ struct node { int data ; struct node *link ; } ; void push ( struct node **, int ) ; int pop ( struct node ** ) ; void delstack ( struct node ** ) ; void main( ) { struct node *s = NULL ; int i ; clrscr( ) ; push ( &s, 14 ) ; push ( &s, -3 ) ; push ( &s, 18 ) ; push ( &s, 29 ) ; push ( &s, 31 ) ; push ( &s, 16 ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; delstack ( &s ) ; getch( ) ; } /* adds a new node to the stack as linked list */ void push ( struct node **top, int item ) { struct node *temp ; temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; if ( temp == NULL ) printf ( "\nStack is full." ) ; temp -> data = item ; temp -> link = *top ; *top = temp ; } /* pops an eleme...

Program implements array as a stack.

#include #include #define MAX 10 struct stack { int arr[MAX] ; int top ; } ; void initstack ( struct stack * ) ; void push ( struct stack *, int item ) ; int pop ( struct stack * ) ; void main( ) { struct stack s ; int i ; clrscr( ) ; initstack ( &s ) ; push ( &s, 11 ) ; push ( &s, 23 ) ; push ( &s, -8 ) ; push ( &s, 16 ) ; push ( &s, 27 ) ; push ( &s, 14 ) ; push ( &s, 20 ) ; push ( &s, 39 ) ; push ( &s, 2 ) ; push ( &s, 15 ) ; push ( &s, 7 ) ; i = pop ( &s ) ; printf ( "\n\nItem popped: %d", i ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; i = pop ( &s ) ; printf ( "\nItem popped: %d", i ) ; getch( ) ; } /* intializes the stack */ void initstack ( struct stack *s ) { s -> top = -1 ; } /* adds an element to the stack */ voi...

Program to store sparse matrix as a linked list

#include #include #include #define MAX1 3 #define MAX2 3 /* structure for col headnode */ struct cheadnode { int colno ; struct node *down ; struct cheadnode *next ; } ; /* structure for row headnode */ struct rheadnode { int rowno ; struct node * right ; struct rheadnode *next ; } ; /* structure for node to store element */ struct node { int row ; int col ; int val ; struct node *right ; struct node *down ; } ; /* structure for special headnode */ struct spmat { struct rheadnode *firstrow ; struct cheadnode *firstcol ; int noofrows ; int noofcols ; } ; struct sparse { int *sp ; int row ; struct spmat *smat ; struct cheadnode *chead[MAX2] ; struct rheadnode *rhead[MAX1] ; struct node *nd ; } ; void initsparse ( struct sparse * ) ; void create_array ( struct sparse * ) ; void display ( struct sparse ) ; int count ( struct sparse ) ; void create_triplet ( struct sparse *, struct sparse ) ; void create_llist ( struct sparse * ) ; void insert ( struct sparse *, struct...

Program to multiply two sparse matrices

#include #include #include #define MAX1 3 #define MAX2 3 #define MAXSIZE 20 #define TRUE 1 #define FALSE 2 struct sparse { int *sp ; int row ; int *result ; } ; void initsparse ( struct sparse * ) ; void create_array ( struct sparse * ) ; int count ( struct sparse ) ; void display ( struct sparse ) ; void create_tuple ( struct sparse*, struct sparse ) ; void display_tuple ( struct sparse ) ; void prodmat ( struct sparse *, struct sparse, struct sparse ) ; void searchina ( int *sp, int ii, int*p, int*flag ) ; void searchinb ( int *sp, int jj, int colofa, int*p, int*flag ) ; void display_result ( struct sparse ) ; void delsparse ( struct sparse * ) ; void main( ) { struct sparse s[5] ; int i ; clrscr( ) ; for ( i = 0 ; i initsparse ( &s[i] ) ; create_array ( &s[0] ) ; create_tuple ( &s[1], s[0] ) ; display_tuple ( s[1] ) ; create_array ( &s[2] ) ; create_tuple ( &s[3], s[2] ) ; display_tuple ( s[3] ) ; prodmat ( &s[4], s[1], s[3] ) ; p...