What's New-Given Below
technicalsymposium
technicalsymposium
technicalsymposium

Free E-Mail Alerts

College Lab Codings/Project Codings-Download


DataStructure-Program to store sparse matrix as a linked list.

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#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 spmat *, int, int, int ) ;
void show_llist ( struct sparse ) ;
void delsparse ( struct sparse * ) ;
void main( )
{
struct sparse s1, s2 ;
clrscr( ) ;
initsparse ( &s1 ) ;
initsparse ( &s2 ) ;
create_array ( &s1 ) ;
printf ( "\nElements in sparse matrix: " ) ;
display ( s1 ) ;
create_triplet ( &s2, s1 ) ;
create_llist ( &s2 ) ;
printf ( "\n\nInformation stored in linked list : " ) ;
show_llist ( s2 ) ;
delsparse ( &s1 ) ;
delsparse ( &s2 ) ;
getch( ) ;
}
/* initializes structure elements */
void initsparse ( struct sparse *p )
{
int i ;
/* create row headnodes */
for ( i = 0 ; i < MAX1 ; i++ )
p -> rhead[i] = ( struct rheadnode * ) malloc ( sizeof ( struct rheadnode ) ) ;
/* initialize and link row headnodes together */
for ( i = 0 ; i < MAX1 - 1 ; i++ )
{
p -> rhead[i] -> next = p -> rhead[i + 1] ;
p -> rhead[i] -> right = NULL ;
p -> rhead[i] -> rowno = i ;
}
p -> rhead[i] -> right = NULL ;
p -> rhead[i] -> next = NULL ;
/* create col headnodes */
for ( i = 0 ; i < MAX1 ; i++ )
p -> chead[i] = ( struct cheadnode * ) malloc ( sizeof ( struct cheadnode ) ) ;
/* initialize and link col headnodes together */
for ( i = 0 ; i < MAX2 - 1 ; i++ )
{
p -> chead[i] -> next = p -> chead[i + 1] ;
p -> chead[i] -> down = NULL ;
p -> chead[i] -> colno = i ;
}
p -> chead[i] -> down = NULL ;
p -> chead[i] -> next = NULL ;
/* create and initialize special headnode */
p -> smat = ( struct spmat * ) malloc ( sizeof ( struct spmat ) ) ;
p -> smat -> firstcol = p -> chead[0] ;
p -> smat -> firstrow = p -> rhead[0] ;
p -> smat -> noofcols = MAX2 ;
p -> smat -> noofrows = MAX1 ;
}
/* creates, dynamically the matrix of size MAX1 x MAX2 */
void create_array ( struct sparse *p )
{
int n, i ;
p -> sp = ( int * ) malloc ( MAX1 * MAX2 * sizeof ( int ) ) ;
/* get the element and store it */
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
printf ( "Enter element no. %d:", i ) ;
scanf ( "%d", &n ) ;
* ( p -> sp + i ) = n ;
}
}
/* displays the contents of the matrix */
void display ( struct sparse s )
{
int i ;
/* traverses the entire matrix */
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
/* positions the cursor to the new line for every new row */
if ( i % MAX2 == 0 )
printf ( "\n" ) ;
printf ( "%d\t", * ( s.sp + i ) ) ;
}
}
/* counts the number of non-zero elements */
int count ( struct sparse s )
{
int cnt = 0, i ;
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
if ( * ( s.sp + i ) != 0 )
cnt++ ;
}
return cnt ;
}
/* creates an array of triplet containing info. about non-zero elements */
void create_triplet ( struct sparse *p, struct sparse s )
{
int r = 0 , c = -1, l = -1, i ;
p -> row = count ( s ) ;
p -> sp = ( int * ) malloc ( p -> row * 3 * sizeof ( int ) ) ;
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
c++ ;
/* sets the row and column values */
if ( ( ( i % MAX2 ) == 0 ) && ( i != 0 ) )
{
r++ ;
c = 0 ;
}
/* checks for non-zero element. Row, column and non-zero element value is assigned to the matrix */
if ( * ( s.sp + i ) != 0 )
{
l++ ;
* ( p -> sp + l ) = r ;
l++ ;
* ( p -> sp + l ) = c ;
l++ ;
* ( p -> sp + l ) = * ( s.sp + i ) ;
}
}
}
/* stores information of triplet in a linked list form */
void create_llist ( struct sparse *p )
{
int j = 0, i ;
for ( i = 0 ; i < p -> row ; i++, j+= 3 )
insert ( p, p -> smat, * ( p -> sp + j ), * ( p -> sp + j + 1 ), * ( p -> sp + j + 2) ) ; }
/* inserts element to the list */
void insert ( struct sparse *p, struct spmat *smat , int r, int c, int v )
{
struct node *temp1, *temp2 ;
struct rheadnode *rh ;
struct cheadnode *ch ;
int i, j ;
/* allocate and initialize memory for the node */
p -> nd = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
p -> nd -> col = c ;
p -> nd -> row = r ;
p -> nd -> val = v ;
/* get the first row headnode */
rh = smat -> firstrow ;
/* get the proper row headnode */
for ( i = 0 ; i < r ; i++ )
rh = rh -> next ;
temp1 = rh -> right ;
/* if no element added in a row */
if ( temp1 == NULL )
{
rh -> right = p -> nd ;
p -> nd -> right = NULL ;
}
else
{
/* add element at proper position */
while ( ( temp1 != NULL ) && ( temp1 -> col < c ) )
{
temp2 = temp1 ;
temp1 = temp1 -> right ;
}
temp2 -> right = p -> nd ;
p -> nd -> right = NULL ;
}
/* link proper col headnode with the node */
ch = p -> smat -> firstcol ;
for ( j = 0 ; j < c ; j++ )
ch = ch -> next ;
temp1 = ch -> down ;
/* if col not pointing to any node */
if ( temp1 == NULL )
{
ch -> down = p -> nd ;
p -> nd -> down = NULL ;
}
else
{
/* link previous node in column with next node in same column */
while ( ( temp1 != NULL ) && ( temp1 -> row < r ) )
{
temp2 = temp1 ;
temp1 = temp1 -> down ;
}
temp2 -> down = p -> nd ;
p -> nd -> down = NULL ;
}
}
void show_llist ( struct sparse s )
{
struct node *temp ;
/* get the first row headnode */
int r = s.smat -> noofrows ;
int i ;
printf ( "\n" ) ;
for ( i = 0 ; i < r ; i++ )
{
temp = s.rhead[i] -> right ;
if ( temp != NULL )
{ while ( temp -> right != NULL )
{
printf ( "Row: %d Col: %d Val: %d\n", temp -> row, temp -> col, temp -> val ) ;
temp = temp -> right ;
}
if ( temp -> row == i )
printf ( "Row: %d Col: %d Val: %d\n", temp -> row, temp -> col, temp -> val ) ; }
}
}
/* deallocates memory */
void delsparse ( struct sparse *p )
{
int r = p -> smat -> noofrows ;
struct rheadnode *rh ;
struct node *temp1, *temp2 ;
int i, c ;
/* deallocate memeory of nodes by traversing rowwise */
for ( i = r - 1 ; i >= 0 ; i-- )
{
rh = p -> rhead[i] ;
temp1 = rh -> right ;
while ( temp1 != NULL )
{
temp2 = temp1 -> right ;
free ( temp1 ) ;
temp1 = temp2 ;
}
}
/* deallocate memory of row headnodes */
for ( i = r - 1 ; i >= 0 ; i-- )
free ( p -> rhead[i] ) ;
/* deallocate memory of col headnodes */
c = p -> smat -> noofcols ;
for ( i = c - 1 ; i >= 0 ; i-- )
free ( p -> chead[i] ) ;
}

CSE/IT/MCA Lab & Technical Programs with Answers-Given Below

Placement Training Program for Engineering Students
(Aptitude Short-Cut Training Program)
Don’t miss this opportunity & 100% Hands on & Practical Sessions Only.

technical symposium

One Day Placement Training program on “Hands on Aptitude short-cut training program & Winning Techniques of Technical/HR Round".
• BE/B.Tech(All Branches)/ME/M.Tech(All Branches)/Passed Out Students
Training Program Will be held in A/C Hall & Reg.Fee includes Course Materials & Lunch,Refreshments.
Participation Certificate Will be Issued to All Participants
Click here to View Complete Details

technical symposium
Freshers Software/Government Jobs Details
job detailsAll Government Jobs Openings
job detailsAll Software Job Openings
job detailsAll Govt/Software Intern Openings
View all Government/Software Jobs Openings job detailsjob detailsjob details



Engineering College/Universities Conference/Symposium Details
job detailsAll Engineering College/Universities Conferences Details
job detailsAll College Symposium Details
job detailsAll College Workshops/Training Program Details
View all Conference & Symposium Details job detailsjob detailsjob details



Engineering Lecture Notes /Study Materials
job detailsCSE/ECE/CIVIL/MECHANICAL Engineering (All Sem Complete Notes)
job detailsMCA/IT/Automobile Engineering (All Sem Complete Notes)
job detailsMBA/Bio-Technology/Bio-Medical Engineering (All Sem Complete Notes)
View all Departments Lecture Notes job detailsjob detailsjob details
Placement Materials with Answers
job detailsTCS/WIPRO/CTS/INFOSYS Placement Papers with Answers
job detailsAll Software Companies Technical Round Materials
job detailsFree Interview & HR Round Materials
View all Placement Materials job detailsjob detailsjob details



Technical Round Programs/Lab Programs & Manuals
job detailsJAVA/C/C++/OS Technical Round Programs with Answer
job detailsC Aptitude Round Programs with Answers
job detailsData Structure Lab & Technical Round Programs with Answers
View all Techical Round & Lab Programs job detailsjob detailsjob details



GATE Original Question Papers Details
job detailsAll Branches of GATE Papers
job detailsGATE All Branches Syllabuses
job detailsGATE Exam Complete Details
View all GATE Exam Question Papers job detailsjob detailsjob details
Anna University & All Universities Original Question papers Details
job detailsAnna University All Branches Question Papers
job detailsAll Universities All Sem Question Papers
job detailsAll University & Anna University Elective Papers Question Papers
View all Universities Question Papers job detailsjob detailsjob details



Govt Exam Original Question Papers Details
job detailsTNPSC/TET Original Question Papers
job detailsUPSC/SSC Original Question Papers
job detailsIES Question Papers
View all Govt Exam Question Papers job detailsjob detailsjob details



Govt Exam/Entrance Exam Syllabuses Details
job detailsEngineering Services Exam Syllabus-All Department
job detailsSTAFF SELECTION COMMISSION (SSC) SYLLABUS
job detailsUGC-NET/SLET Syllabuses
View all Govt Exam/Entrance Exam Syllabuses job detailsjob detailsjob details
Engineering Colleges in Tamil Nadu-Complete Details
job detailsAll Engineering Colleges Complete Details
job detailsDistrict Wise Engineering Colleges Details
job detailsAll Engineering Colleges Address & Seats Details
View all Engineering Colleges Detailsjob detailsjob detailsjob details



Universities in India-Complete Details
job detailsAll Universities in India Complete Details
job detailsAll Universities-State Wise Complete Details
job detailsUniversities Address & Seats Complete Details
View all Universities Detailsjob detailsjob detailsjob details



Paper Presentation Abstracts -Complete Details
job detailsAll Branches of Paper Abstracts
job detailsLatest Paper Abstracts
job detailsFree Download of All Abstracts
View all Paper Abstractsjob detailsjob detailsjob details

Hosting by Yahoo!

About-Us    Contact-Us    Site-map

©copyright 2009 All rights are reserved to technicalsymposium.com