Posts

Program to add two sparse matrices

#include #include #include #define MAX1 3 #define MAX2 3 #define MAXSIZE 9 #define BIGNUM 100 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 addmat ( struct sparse *, struct sparse, struct sparse ) ; 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] ) ; addmat ( &s[4], s[1], s[3] ) ; printf ( "\nResult of addition of two matrices: " ) ; display_result ( s[4] ) ; for ( i = 0 ; i delsparse ( &s[...

Program to transpose a sparse matrix

#include #include #include #define MAX1 3 #define MAX2 3 struct sparse { int *sp ; int row ; } ; void initsparse ( struct sparse * ) ; void create_array ( struct sparse * ) ; void display ( struct sparse ) ; int count ( struct sparse ) ; void create_tuple ( struct sparse *, struct sparse ) ; void display_tuple ( struct sparse ) ; void transpose ( struct sparse *, struct sparse ) ; void display_transpose ( struct sparse ) ; void delsparse ( struct sparse * ) ; void main( ) { struct sparse s[3] ; int c, i ; for ( i = 0 ; i initsparse ( &s[i] ) ; clrscr( ) ; create_array ( &s[0] ) ; printf ( "\nElements in Sparse Matrix: " ) ; display ( s[0] ) ; c = count ( s[0] ) ; printf ( "\n\nNumber of non-zero elements: %d", c ) ; create_tuple ( &s[1], s[0] ) ; printf ( "\n\nArray of non-zero elements: " ) ; display_tuple ( s[1] ) ; transpose ( &s[2], s[1] ) ; printf ( "\n\nTranspose of array: " ) ; display_transpo...

Program to create a 3-tuple from a given matrix

#include #include #include #define MAX1 3 #define MAX2 3 struct sparse { int *sp ; int row ; } ; void initsparse ( struct sparse * ) ; void create_array ( struct sparse * ) ; void display ( struct sparse ) ; int count ( struct sparse ) ; void create_tuple ( struct sparse *, struct sparse ) ; void display_tuple ( struct sparse ) ; void delsparse ( struct sparse * ) ; void main( ) { struct sparse s1, s2 ; int c ; clrscr( ); initsparse ( &s1 ) ; initsparse ( &s2 ) ; create_array ( &s1 ) ; printf ( "\nElements in Sparse Matrix: " ) ; display ( s1 ) ; c = count ( s1 ) ; printf ( "\n\nNumber of non-zero elements: %d", c ) ; create_tuple ( &s2, s1 ) ; printf ( "\n\nArray of non-zero elements: " ) ; display_tuple ( s2 ) ; delsparse ( &s1 ) ; delsparse ( &s2 ) ; getch( ) ; } /* initialises element of structure */ void initsparse ( struct sparse *p ) { p -> sp = NULL ; } /* dynamically creates ...

Program to maintain a linked list

#include #include #include /* structure containing a data part and link part */ struct node { int data ; struct node * link ; } ; void append ( struct node **, int ) ; void addatbeg ( struct node **, int ) ; void addafter ( struct node *, int, int ) ; void display ( struct node * ) ; int count ( struct node * ) ; void delete ( struct node **, int ) ; void main( ) { struct node *p ; p = NULL ; /* empty linked list */ printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ; append ( &p, 14 ) ; append ( &p, 30 ) ; append ( &p, 25 ) ; append ( &p, 42 ) ; append ( &p, 17 ) ; display ( p ) ; addatbeg ( &p, 999 ) ; addatbeg ( &p, 888 ) ; addatbeg ( &p, 777 ) ; display ( p ) ; addafter ( p, 7, 0 ) ; addafter ( p, 2, 1 ) ; addafter ( p, 5, 99 ) ; display ( p ) ; printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ; delete ( &p, 99 ) ; delete ( &p, 1 ) ; delete ( &p, 10 ) ; displ...

Program to Multiply two polynomials maintained as linked lists.

#include #include #include /* structure representing a node of a linked list. The node can store a term of a polynomial */ struct polynode { float coeff ; int exp ; struct polynode *link ; } ; void poly_append ( struct polynode **, float, int ) ; void display_poly ( struct polynode * ) ; void poly_multiply ( struct polynode *, struct polynode *, struct polynode ** ) ; void padd ( float, int, struct polynode ** ) ; void main( ) { struct polynode *first, *second, *mult ; int i = 1 ; first = second = mult = NULL ; /* empty linked lists */ poly_append ( &first, 3, 5 ) ; poly_append ( &first, 2, 4 ) ; poly_append ( &first, 1, 2 ) ; clrscr( ) ; display_poly ( first ) ; poly_append ( &second, 1, 6 ) ; poly_append ( &second, 2, 5 ) ; poly_append ( &second, 3, 4 ) ; printf ( "\n\n" ) ; display_poly ( second ) ; printf ( "\n" ); while ( i++ printf ( "-" ) ; poly_multiply ( first, second, &mult ) ; printf ( "\n\...

Program to add two polynomials maintained as linked lists.

#include #include #include /* structure representing a node of a linked list. The node can store term of a polynomial */ 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 h...

Program to maintain a doubly linked list

#include #include #include /* structure representing a node of the doubly linked list */ struct dnode { struct dnode *prev ; int data ; struct dnode * next ; } ; void d_append ( struct dnode **, int ) ; void d_addatbeg ( struct dnode **, int ) ; void d_addafter ( struct dnode *, int , int ) ; void d_display ( struct dnode * ) ; int d_count ( struct dnode * ) ; void d_delete ( struct dnode **, int ) ; void main( ) { struct dnode *p ; p = NULL ; /* empty doubly linked list */ d_append ( &p , 11 ) ; d_append ( &p , 2 ) ; d_append ( &p , 14 ) ; d_append ( &p , 17 ) ; d_append ( &p , 99 ) ; clrscr( ) ; d_display ( p ) ; printf ( "\nNo. of elements in the DLL = %d\n", d_count ( p ) ) ; d_addatbeg ( &p, 33 ) ; d_addatbeg ( &p, 55 ) ; d_display ( p ) ; printf ( "\nNo. of elements in the DLL = %d\n", d_count ( p ) ) ; d_addafter ( p, 4, 66 ) ; d_addafter ( p, 2, 96 ) ; d_display ( p ) ; printf ( "\nNo. of elements in th...