Wednesday, 25 September 2019

Implement Doubly Linked List in C

Implement Doubly Linked List


#include <stdio.h>
#include <malloc.h>
#include <process.h>
void create();
void insert_at_beg();
void insert_at_end();
void insert_after_pos();
void del();
void display_forward();
void display_backward();
struct node  {
int info;
struct node* next;
struct node* prev;
}*start=NULL;
int data,item,n1,pos,i,m;
int main()
{
int n;
setbuf(stdout, NULL);
printf("\n****Doubly Linked List*****\n");
printf("\n1.Create\n2.Insert at Beginning\n3.Insert at End\n4.Insert After Position\n5.Delete\n6.Display Forward \n7.Display Backward\n8.Exit\n");
while(1)
{
printf("\nEnter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Display forward 7. Display backward 8.Exit)\n");
scanf("%d",&n);
switch(n)
{
case 1:
create();
break;
case 2:
insert_at_beg();
break;
case 3:
insert_at_end();
break;
case 4:
insert_after_pos();
break;
case 5:
del();
break;
case 6:
display_forward();
break;
case 7:
display_backward();
break;
case 8:
exit(0);
default:
printf("\nWrong Choice !!\n");
}
}
return 0;
}
void create()
{
int data;
struct node *tmp;
printf("\nEnter the first element to be inserted :\n");
scanf("%d",&data);
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->prev=NULL;
tmp->next=NULL;
if(start == NULL)
start = tmp;
display_forward();
}
void insert_at_beg()
{
int data;
struct node *tmp;
printf("\nEnter the element to be inserted :\n");
scanf("%d",&data);
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->prev=NULL;
tmp->next=NULL;
if(start == NULL)
start = tmp;
else
{
start->prev = tmp;
tmp->next = start;
start = tmp;
}
display_forward();
}
void insert_at_end()
{
int data;
struct node *q,*tmp;
printf("\nEnter the element to be inserted :\n");
scanf("%d",&data);
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->prev=NULL;
tmp->next=NULL;
if(start == NULL)
start = tmp;
else
{
q=start;
while(q->next != NULL)
q = q->next; // Go To last Node
q->next = tmp;
tmp->prev = q;
}
display_forward();
}
void insert_after_pos()
{
int data;
struct node *q,*tmp;
tmp=malloc(sizeof(struct node));
printf("\nEnter the element to be inserted :\n");
scanf("%d",&data);
tmp->info=data;
tmp->prev=NULL;
tmp->next=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
printf("Enter index after which element to be inserted :\n");
scanf("%d",&pos);
q=start;
for(i=0;i<pos;i++)
{
q = q->next;
}
tmp->next = q->next;
if(q->next!=NULL)
{
q->next->prev=tmp;
}
q->next = tmp;
tmp->prev=q;
display_forward();
}
}
void del()
{
struct node *tmp,*q,*prev;
printf("Enter the element to be deleted :\n");
scanf("%d",&data);
if(start->info==data)  //deletion of first node
{
tmp=start;
if(tmp->next!=NULL)
{
start->next->prev=NULL;
}
start=start->next;
free(tmp);
display_forward();
return;
}
q=start;
while(q->next->next!=NULL)  //deletion of middle node
{
if(q->next->info==data)
{
prev=q->next->prev;
tmp=q->next;
q->next=tmp->next;
q->next->prev=prev;
free(tmp);
display_forward();
return;
}
q=q->next;
}
if(q->next->info==data)  //deletion at end
{
tmp=q->next;
q->next=NULL;
free(tmp);
display_forward();
return;
}
printf("\nElement not found \n");
}
void  display_forward()
{
struct node *q;
if(start==NULL)
printf("List is empty!!\n");
else
{
printf("**** Elements in Doubly Linked List ****\n");
q=start;
while(q!=NULL)
{
printf("%d\t",q->info);
q=q->next;
}
}
}
void display_backward()
{
struct node *q = start;
while(q->next!=NULL)
{
q=q->next;
}
while(q!=start)
{
printf("%d\t",q->info);
q=q->prev;
}

printf("%d\t",q->info);
}


Output:

****Doubly Linked List*****

1.Create
2.Insert at Beginning
3.Insert at End
4.Insert After Position
5.Delete
6.Display Forward
7.Display Backward
8.Exit

Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Display forward 7. Display backward 8.Exit)
1

Enter the first element to be inserted :
10
**** Elements in Doubly Linked List ****
10
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Display forward 7. Display backward 8.Exit)
2

Enter the element to be inserted :
20
**** Elements in Doubly Linked List ****
20 10
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Display forward 7. Display backward 8.Exit)
3

Enter the element to be inserted :
30
**** Elements in Doubly Linked List ****
20 10 30
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Display forward 7. Display backward 8.Exit)
7
30 10 20
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Display forward 7. Display backward 8.Exit)
6
**** Elements in Doubly Linked List ****
20 10 30
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Display forward 7. Display backward 8.Exit)

Implement a Singly Linked List in C

Implement a Singly Linked List in C


#include <stdio.h>
#include <malloc.h>
#include <process.h>
void create();
void insert_at_beg();
void insert_at_end();
void insert_after_pos();
void del();
void search();
void display();
void displayrev(start);
void reversedlist();

struct node
{
int info;
struct node *link;
}*start=NULL;

int data,item,n1,pos,i,m;
int main()
{
int n;
setbuf(stdout, NULL);
printf("\n****Linked List*****\n");
printf("\n1.Create\n2.Insert at Beginning\n3.Insert at End\n4.Insert After Position\n5.Delete\n6.Search\n7.Display\n8.Display in Reverse\n9.Reversed List\n10.Exit\n");
while(1)
{
printf("\nEnter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)\n");
scanf("%d",&n);
switch(n)
{
case 1:
create();
break;
case 2:
insert_at_beg();
break;
case 3:
insert_at_end();
break;
case 4:
insert_after_pos();
break;
case 5:
del();
break;
case 6:
search();
break;
case 7:
display();
break;
case 8:
printf("**** Reversed Linked List ****\n");
displayrev(start);
break;
case 9:
reversedlist();
break;
case 10:
exit(0);
default:
printf("\nWrong Choice !!\n");
}
}
return 0;
}
void create()
{
struct node *q, *tmp;
printf("Enter element :\n");
scanf("%d",&data);
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{       q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
void insert_at_beg()
{
struct node *tmp;
printf("\nEnter the element to be inserted :\n");
scanf("%d",&data);
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
display();
}
void insert_at_end()
{
struct node *tmp,*q;
printf("\nEnter the element to be inserted :\n");
scanf("%d",&data);
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=NULL;

if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
display();
}
void insert_after_pos()
{
display();
struct node *q,*tmp;
int index;
tmp=malloc(sizeof(struct node));
printf("\nEnter the element to be inserted :\n");
scanf("%d",&data);
tmp->info=data;
tmp->link=NULL;

if(start==NULL)
{
start=tmp;
}
else
{
printf("Enter index after which element to be inserted :\n");
scanf("%d",&index);
q=start;
for(i=0;i<index;i++)
{
q = q->link;
if(q==NULL)
{
printf("There are  less  elements\n");
return;
}
}
tmp->link = q->link;
q->link = tmp;
}
display();
}
void del()
{
struct node *q,*tmp;
printf("Enter the element to be deleted :\n");
scanf("%d",&data);
if(start->info==data)  //deletion of first node
{
tmp=start;
start=start->link;
free(tmp);
display();
return;
}
q=start;
while(q->link->link!=NULL)     //deletion middle node
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
display();
return;
}
q=q->link;
}
if(q->link->info==data) //deletion of last node
{
tmp=q->link;
q->link=NULL;
free(tmp);
display();
return;
}
printf("\nElement not found \n");
}
void search()
{
struct node *tmp;
int i=0;
printf("\nEnter the element to be searched :");
scanf("%d",&item);
tmp=start;

while(tmp!=NULL)
{
if(tmp->info==item)
{
printf("Element found at index: %d\n",i);
return;
}
tmp=tmp->link;
i++;
}
if(tmp->link==NULL)
printf("Element not found \n");
}
void display()
{
struct node *q;
if(start==NULL)
printf("List is empty!!\n");
else
{
printf("**** Elements in Linked List ****\n");
q=start;
while(q!=NULL)
{
printf("%d\t",q->info);
q=q->link;
}
}
}

void displayrev(struct node* start)
{
struct node *q= start;
if(q!=NULL)
{
displayrev(q->link);
printf("%d ",q->info);
}
}
void reversedlist()
{
    struct node *prevNode, *curNode;

    if(start != NULL)
    {
        prevNode = start;
        curNode = start->link;
        start = start->link;

        prevNode->link = NULL; // Make first node as last node

        while(start != NULL)
        {
        start = start->link;
            curNode->link = prevNode;
            prevNode = curNode;
            curNode = start;
        }

        start = prevNode; // Make last node as head

        printf("SUCCESSFULLY REVERSED LIST\n");
        display(start);
    }
}

Output:

****Linked List*****

1.Create
2.Insert at Beginning
3.Insert at End
4.Insert After Position
5.Delete
6.Search
7.Display
8.Display in Reverse
9.Reversed List
10.Exit

Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)
1
Enter element :
10

Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)
2

Enter the element to be inserted :
5
**** Elements in Linked List ****
5 10
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)
3

Enter the element to be inserted :
20
**** Elements in Linked List ****
5 10 20
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)
4
**** Elements in Linked List ****
5 10 20
Enter the element to be inserted :
15
Enter index after which element to be inserted :
1
**** Elements in Linked List ****
5 10 15 20
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)
5
Enter the element to be deleted :
20
**** Elements in Linked List ****
5 10 15
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)
6

Enter the element to be searched :10
Element found at index: 1

Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)
8
**** Reversed Linked List ****
15 10 5 
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)
9
SUCCESSFULLY REVERSED LIST
**** Elements in Linked List ****
15 10 5
Enter Your Choice :(1.Create 2.Insert at Beg. 3.Insert at End 4.Insert after Pos. 5.Delete 6.Search  7.Display 8.Display in Reverse 9.Reversed List 10.Exit)

Implement Sequential File in C

Implement Sequential File in C


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 10
struct stud
{
char name[20];
int rollno;
int per;
};

void addrecord()
{
struct stud temp;
FILE *fptr;
fptr = fopen("myFile.txt","ab");
if(fptr==NULL)
{
printf("file not opened");
return ;
}

fflush(stdin);
printf("\nEnter the Name :\n");
scanf("%s",&temp.name);
printf("Enter the Roll no :\n");
scanf("%d",&temp.rollno);
printf("Enter the Percentage:\n");
scanf("%d",&temp.per);

fwrite(&temp, sizeof(struct stud), 1, fptr);
fclose(fptr);
printf("\n \nRecord has been added.");
}

void disp()
{
struct  stud temp;
FILE *fptr;
fptr=fopen("myFile.txt","rb");
if(fptr==NULL)
{
printf("file not opened");
return ;
}
printf("\nName\tRollno\tPecentage \n");
printf("\n==========================");
while(fread(&temp, sizeof(struct stud), 1, fptr)==1)
{
printf("\n%s\t%d\t%d", temp.name, temp.rollno,temp.per);
}
fclose(fptr);
}

void search(char *n)
{
struct   stud temp;
FILE *fptr;
fptr=fopen("myFile.txt","rb");
if(fptr==NULL)
{
printf("file not opened");
return;
}
int j=0;
while(fread(&temp, sizeof(struct stud), 1, fptr)==1)
{
if(strcmp(n,temp.name)==0)
{
j=1;
printf("\nRecord found!!!");
return;
}
}
if(j==0)
printf("\nRecord not found!!!");

}
void modify(char *n)
{
struct   stud temp[SIZE];
FILE *fptr;
fptr=fopen("myFile.txt","rb");
if(fptr==NULL)
{
printf("file not opened");
return ;
}
int i=0,j,k=0;
while(fread(&temp[i], sizeof(struct stud), 1, fptr)==1)
{
++i;
}
fclose(fptr);

fptr = fopen("myFile.txt","wb");
if(fptr==NULL)
{
printf("file not opened");
return ;
}

for(j=0;j<i;j++)
{
if(strcmp(n,temp[j].name)==0)
{
k=1;
printf("\nEnter the new marks :\n");
scanf("%d",&temp[j].per);
printf("\nRecord modified");
}
fwrite(&temp[j],sizeof(struct stud),1,fptr);
}
fclose(fptr);
if(k==0)
{
printf("\nRecord not found.!!   :(\n");
}
}

void delete(char *n)
{
struct  stud temp[SIZE];
FILE *fptr;
fptr=fopen("myFile.txt","rb");
if(fptr==NULL)
{
printf("file not opened");
return;
}
int i=0,j,k=0;
while(fread(&temp[i], sizeof(struct stud), 1, fptr)==1)
{
i++;
}
fclose(fptr);

fptr = fopen("myFile.txt","wb");
if(fptr==NULL)
{
printf("file not opened");
return ;
}

for(j=0;j<i;j++)
{
if(strcmp(n,temp[j].name)==0)
{k=1;
printf("\nRecord found!!!  :)");
printf("\n Record deleted   \n");
continue;
}
fwrite(&temp[j],sizeof(struct stud),1,fptr);

}
fclose(fptr);
if(k==0)
{
printf("\n record  not found");
return;
}
}

int main()
{
int c,g;
char t[20];
setbuf(stdout,NULL);
do
{
printf("\n-------MENU-------");
printf("\n1.ADD RECORD");
printf("\n2.DISPLAY");
printf("\n3.SEARCH");
printf("\n4.MODIFY");
printf("\n5.DELETE");
printf("\n6.EXIT\n");
printf("Please enter your choice ");
scanf("%d",&c);
switch(c)
{
case 1:
addrecord();
break;
case 2:
disp();
break;
case 3:
printf("\nEnter the name to search :\n");
scanf("%s",t);
search(t);
break;
case 4:
printf("\nEnter the name to modify :\n");
scanf("%s",t);
modify(t);
break;
case 5:
printf("\nEnter the name to delete :\n");
scanf("%s",t);
delete(t);
break;
case 6: exit(0);
}
printf("\nDo you want to continue?Enter 1 for Yes or enter 0 for No : ");
scanf("%d",&g);
}while(g==1);
return 0;
}


Output:
-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 1

Enter the Name :
aa
Enter the Roll no :
11
Enter the Percentage:
11

Record has been added.
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1

-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 2

Name Rollno Pecentage 

==========================
aa 11 11
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1

-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 3

Enter the name to search :
bb

Record not found!!!
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1

-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 4

Enter the name to modify :
aa

Enter the new marks :
12

Record modified
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1

-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 2

Name Rollno Pecentage 

==========================
aa 11 12
Do you want to continue?Enter 1 for Yes or enter 0 for No : 1

-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 5

Enter the name to delete :
aa

Record found!!!  :)
 Record deleted   

Do you want to continue?Enter 1 for Yes or enter 0 for No : 1

-------MENU-------
1.ADD RECORD
2.DISPLAY
3.SEARCH
4.MODIFY
5.DELETE
6.EXIT
Please enter your choice 2

Name Rollno Pecentage 

==========================
Do you want to continue?Enter 1 for Yes or enter 0 for No : 

Tuesday, 24 September 2019

Addition and Transpose Operations on Sparse Matrix

Operations on Sparse Matrix


#include<stdio.h>
#include<stdlib.h>
#define MAX 20
void printsparse(int b[MAX][3]);
void readsparse(int b[MAX][3]);
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3]);
void transpose(int[MAX][3],int[MAX][3]);
int main()
{
setbuf(stdout,NULL);
int b1[MAX][3],b2[MAX][3],b3[MAX][3],b4[MAX][4];
readsparse(b1);
readsparse(b2);
printf("\n**Addition of Sparse Matrix**\n");
addsparse(b1,b2,b3);
printsparse(b3);
printf("\n**Transpose of Sparse Matrix 1**\n");
        transpose(b1,b4);
        printsparse(b4);
return 0;
}
void readsparse(int b[MAX][3])
{
int i,t,m,n;
printf("\nEnter no. of rows and columns:");
scanf("%d%d",&m,&n);
printf("No. of non-zero triples:");
scanf("%d",&t);
b[0][0]=m;
b[0][1]=n;
b[0][2]=t;
for(i=1;i<=t;i++)
{
printf("Enter the triples(row,column,value):");
scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
}
}
void addsparse(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3])
{
int t1,t2,i,j,k;
if(b1[0][0]!=b2[0][0]||b1[0][1]!=b2[0][1])
{

printf("\nYou have entered invalid matrix!!Size must be equal");
exit(0);
}
t1=b1[0][2];
t2=b2[0][2];
i=j=k=1;
b3[0][0]=b1[0][0];
b3[0][1]=b1[0][1];
while(i<=t1&&j<=t2)
{
if(b1[i][0]<b2[j][0])    //row numbers are not equal
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
k++;
i++;
}
else if(b1[i][0]>b2[j][0])    //row numbers are not equal
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
k++;
j++;
}
else if(b1[i][1]<b2[j][1])      //row numbers are equal, compare column
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
k++;
i++;
}
else if(b1[i][1]>b2[j][1])    //row numbers are equal, compare column
{
b3[k][0]=b2[j][0];
b3[k][1]=b2[j][1];
b3[k][2]=b2[j][2];
k++;
j++;
}
else
{
b3[k][0]=b1[i][0];      //row and column numbers are equal
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2]+b2[j][2];
k++;
i++;
j++;
}
}
while(i<=t1)        //copy remaining terms from b1
{
b3[k][0]=b1[i][0];
b3[k][1]=b1[i][1];
b3[k][2]=b1[i][2];
i++;
k++;
}
while(j<=t2)        //copy remaining terms from b2
{
b3[k][0]=b2[j][0];
b3[k][1]=b1[j][1];
b3[k][2]=b1[j][2];
j++;
k++;
}
b3[0][2]=k-1;       //set numbe of terms in b3
}
void printsparse(int b[MAX][3])
{
int i,t;
t=b[0][2];
printf("\nrow \tcolumn \tvalue");
for(i=0;i<=t;i++)
{
printf("\n%d\t%d\t%d",b[i][0],b[i][1],b[i][2]);
}
}
void transpose(int b1[][3],int b2[][3])
{
int i,j,k,n;
b2[0][0]=b1[0][1];
b2[0][1]=b1[0][0];
b2[0][2]=b1[0][2];

k=1;
n=b1[0][2];

for(i=0;i<b1[0][1];i++)
for(j=1;j<=n;j++)
//if a column number of current triple==i then insert the current triple in b2
if(i==b1[j][1])
{
b2[k][0]=i;
b2[k][1]=b1[j][0];
b2[k][2]=b1[j][2];
k++;
}
}


Output:
Enter no. of rows and columns:5 5
No. of non-zero triples:5
Enter the triples(row,column,value):1 2 10
Enter the triples(row,column,value):1 4 12
Enter the triples(row,column,value):3 3 5
Enter the triples(row,column,value):4 1 15
Enter the triples(row,column,value):4 2 12

Enter no. of rows and columns:5 5
No. of non-zero triples:5
Enter the triples(row,column,value):1 3 8
Enter the triples(row,column,value):2 4 23
Enter the triples(row,column,value):3 3 9
Enter the triples(row,column,value):4 1 20
Enter the triples(row,column,value):4 2 25

**Addition of Sparse Matrix**

row column value
5 5 7
1 2 10
1 3 8
1 4 12
2 4 23
3 3 14
4 1 35
4 2 37
**Transpose of Sparse Matrix 1**

row column value
5 5 5
1 4 15
2 1 10
2 4 12
3 3 5
4 1 12

Program to Convert Matrix to Sparse Matrix

Program to Convert  Conventional Matrix to Sparse Matrix




#include <stdio.h>
#define MAX 20

void read_matrix(int a[10][10], int row, int column);
void print_sparse(int b[MAX][3]);
void create_sparse(int a[10][10], int row, int column, int b[MAX][3]);

int main()
{
    int a[10][10], b[MAX][3], row, column;
    printf("\nEnter the size of matrix (rows, columns): ");
    scanf("%d%d", &row, &column);

    read_matrix(a, row, column);
    create_sparse(a, row, column, b);
    print_sparse(b);
    return 0;
}

void read_matrix(int a[10][10], int row, int column)
{
    int i, j;
    printf("\nEnter elements of matrix\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            printf("[%d][%d]: ", i, j);
            scanf("%d", &a[i][j]);
        }
    }
}

void create_sparse(int a[10][10], int row, int column, int b[MAX][3])
{
    int i, j, k;
    k = 1;
    b[0][0] = row;
    b[0][1] = column;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            if (a[i][j] != 0)
            {
                b[k][0] = i;
                b[k][1] = j;
                b[k][2] = a[i][j];
                k++;
            }
        }
        b[0][2] = k - 1;
    }
}

void print_sparse(int b[MAX][3])
{
    int i, column;
    column = b[0][2];
    printf("\nSparse form - list of 3 triples\n\n");
    for (i = 0; i <= column; i++)
    {
        printf("%d\t%d\t%d\n", b[i][0], b[i][1], b[i][2]);
    }
}

Output:


Enter the size of matrix (rows, columns): 5
4

Enter elements of matrix
[0][0]: 0
[0][1]: 1
[0][2]: 0
[0][3]: 0
[1][0]: 0
[1][1]: 0
[1][2]: 2
[1][3]: 0
[2][0]: 0
[2][1]: 3
[2][2]: 0
[2][3]: 0
[3][0]: 0
[3][1]: 0
[3][2]: 5
[3][3]: 0
[4][0]: 0
[4][1]: 0
[4][2]: 0
[4][3]: 4

Sparse form - list of 3 triples

5      4      5
0      1      1
1      2      2
2      1      3
3      2      5
4      3      4


Stack and Queue as ADT in C++

Stack as ADT The functions associated with stack are: empty()  – Returns whether the stack is empty size()  – Returns the size o...