C program code for implementing matrix using linear linked list
//matrix using linked list
#include<stdio.h>
#include<conio.h>
typedef struct linked
{
int val;
struct linked *next;
}lnk;
typedef struct linkedaddr
{
lnk *node;
struct linkedaddr *link;
}lnkadr;
lnk *createcol();
lnkadr *createrow();
void disprow(lnkadr *);
void dispcol(lnk *);
void main()
{
lnkadr *h;
clrscr();
h=createrow();
printf("Values in the matrix : \n\n\n");
disprow(h);
getch();
}
lnkadr *createrow()
{
//lnk *h;
lnkadr *head=NULL,*temp,*ptr;
char ch;
while(1)
{
temp=(lnkadr *) malloc(sizeof(lnkadr));
temp->node=createcol();
temp->link=NULL;
if(head==NULL)
{
head=temp;
}
ptr->link=temp;
ptr=temp;
printf("Do you have more row in the matrix ?(y/n) : ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='n' || ch=='N')
return head;
}
}
lnk *createcol()
{
lnk *h=NULL,*ptr,*temp;
int v;
while(1)
{
printf("Enter value for the cell of the matrix (0 to exit) : ");
fflush(stdin);
scanf("%d",&v);
if(v==0)
return h;
temp=(lnk *)malloc(sizeof(lnk));
temp->val=v;
temp->next=NULL;
if(h==NULL)
h=temp;
else
ptr->next=temp;
ptr=temp;
}
}
void disprow(lnkadr *h)
{
lnkadr *ptr;
ptr=h;
while(ptr!=NULL)
{
dispcol(ptr->node);
printf("°\n");
ptr=ptr->link;
}
}
void dispcol(lnk *h)
{
lnk *ptr;
ptr=h;
while(ptr!=NULL)
{
printf("°%3d",ptr->val);
ptr=ptr->next;
}
}
#include<stdio.h>
#include<conio.h>
typedef struct linked
{
int val;
struct linked *next;
}lnk;
typedef struct linkedaddr
{
lnk *node;
struct linkedaddr *link;
}lnkadr;
lnk *createcol();
lnkadr *createrow();
void disprow(lnkadr *);
void dispcol(lnk *);
void main()
{
lnkadr *h;
clrscr();
h=createrow();
printf("Values in the matrix : \n\n\n");
disprow(h);
getch();
}
lnkadr *createrow()
{
//lnk *h;
lnkadr *head=NULL,*temp,*ptr;
char ch;
while(1)
{
temp=(lnkadr *) malloc(sizeof(lnkadr));
temp->node=createcol();
temp->link=NULL;
if(head==NULL)
{
head=temp;
}
ptr->link=temp;
ptr=temp;
printf("Do you have more row in the matrix ?(y/n) : ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='n' || ch=='N')
return head;
}
}
lnk *createcol()
{
lnk *h=NULL,*ptr,*temp;
int v;
while(1)
{
printf("Enter value for the cell of the matrix (0 to exit) : ");
fflush(stdin);
scanf("%d",&v);
if(v==0)
return h;
temp=(lnk *)malloc(sizeof(lnk));
temp->val=v;
temp->next=NULL;
if(h==NULL)
h=temp;
else
ptr->next=temp;
ptr=temp;
}
}
void disprow(lnkadr *h)
{
lnkadr *ptr;
ptr=h;
while(ptr!=NULL)
{
dispcol(ptr->node);
printf("°\n");
ptr=ptr->link;
}
}
void dispcol(lnk *h)
{
lnk *ptr;
ptr=h;
while(ptr!=NULL)
{
printf("°%3d",ptr->val);
ptr=ptr->next;
}
}
Comments
Post a Comment