Dsa Lab Assignment 06
Dsa Lab Assignment 06
Output:
Q2. Write a menu driven program to perform the following operations in a circular linked list by using
suitable user defined functions for each case.
1. Create the list
2. Traverse the list
3. Add a node a first node
4. Delete the first node
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int d;
struct node * ne;
} * start = '\0', * end;
void create(int x)
{
struct node * n = (struct node *)malloc(sizeof(struct node));
n->d = x;
n->ne = start;
if(start == '\0')
{
start = n;
end = n;
}
else
{
end->ne = n;
end = n;
}
}
void print()
{
struct node * n = start;
while(n->ne != start)
{
printf("%i\n",n->d);
n = n->ne;
}
printf("%i\n",n->d);
}
void addf(int x)
{
struct node * n = (struct node *)malloc(sizeof(struct node));
n->d = x;
n->ne = start;
end->ne = n;
start = n;
}
void delf()
{
struct node * n = start;
end->ne = n->ne;
start = n->ne;
free(n);
}
void main()
{
int n, s, z = 1;
printf("Enter No. Of Nodes To Add :~ ");
scanf("%i",&n);
printf("Enter Values To List :~ ");
for (int i = 0 ; i < n ; i++)
{
scanf("%i",&s);
create(s);
}
printf("1-> Print List\n");
printf("2-> Add First Node\n");
printf("3-> Delete First Node\n");
printf("4-> Exit Program\n");
while(z)
{
printf("Enter Your Choice :~ ");
scanf("%i",&n);
if (n == 1){print();}
else if (n == 2)
{
printf("Enter Value To Add :~ ");
scanf("%i",&s);
addf(s);
}
else if (n == 3){delf();}
else if (n == 4){z = 0;}
else {printf("Enter Valid Command\n");}
}
}
Output:
Q3. Write a menu driven program to perform the following operations in a header linked list by using
suitable user defined functions for each case. The head node keeps the count of the total number of nodes
in the list. The data node keeps the student information: Name, Roll No, CGPA, Address, City, Branch.
1. Create
2. Display student information
3. Display the total number of nodes (in O(1) time)
4. Display the students’ details belonging to a particular branch
5. Display the students’ details securing > 7.5 CGPA and belonging to a given branch.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char name[16];
int roll;
float cgpa;
char adr[16];
int branch;
struct node * ne;
} * end;
struct head
{
int c;
struct node * ne;
} * start;
void create()
{
struct node * n = (struct node *)malloc(sizeof(struct node));
printf("Enter Name :~ ");
scanf("%s",&n->name);
printf("Enter Roll No. :~ ");
scanf("%i",&n->roll);
printf("Enter C.G.P.A :~ ");
scanf("%f",&n->cgpa);
printf("Enter Address :~ ");
scanf("%s",&n->adr);
printf("Enter Branch No. :~ ");
scanf("%i",&n->branch);
n->ne = '\0';
if (start->ne == '\0')
{
start->ne = n;
end = n;
}
else
{
end->ne = n;
end = n;
}
}
void display()
{
struct node * n = start->ne;
while (n != '\0')
{
printf("Name :~ %s\n",n->name);
printf("Roll No. :~ %i\n",n->roll);
printf("C.G.P.A :~ %0.2f\n",n->cgpa);
printf("Address :~ %s\n",n->adr);
printf("Branch No. :~ %i\n\n",n->branch);
n = n->ne;
}
}
void disno(){printf("No. Of Records Present Are :~ %i\n",start->c);}
void disbac(int x)
{
struct node * n = start->ne;
while (n != '\0')
{
if (n->branch == x)
{
printf("Name :~ %s\n",n->name);
printf("Roll No. :~ %i\n",n->roll);
printf("C.G.P.A :~ %0.2f\n",n->cgpa);
printf("Address :~ %s\n",n->adr);
printf("Branch No. :~ %i\n",n->branch);
}
n = n->ne;
}
}
void dis75(int x)
{
struct node * n = start->ne;
while (n != '\0')
{
if (n->branch == x && n->cgpa >= 7.5)
{
printf("Name :~ %s\n",n->name);
printf("Roll No. :~ %i\n",n->roll);
printf("C.G.P.A :~ %0.2f\n",n->cgpa);
printf("Address :~ %s\n",n->adr);
printf("Branch No. :~ %i\n",n->branch);
}
n = n->ne;
}
}
void main()
{
start = (struct head *)malloc(sizeof(struct head));
start->c = 0;
start->ne = '\0';
int n, s, z = 1;
printf("Enter No. Of Nodes To Add :~ ");
scanf("%i",&n);
for (int i = 0 ; i < n ; i++)
{
create();
start->c++;
}
printf("1-> Print List\n");
printf("2-> Print Count\n");
printf("3-> Display Data Of Department\n");
printf("4-> Display Data Of Department With 7.5+ C.G.P.A\n");
printf("5-> Exit Program\n");
while(z)
{
printf("Enter Your Choice :~ ");
scanf("%i",&n);
if (n == 1){display();}
else if (n == 2){disno();}
else if (n == 3)
{
printf("Enter Department No. :~ ");
scanf("%i",&s);
disbac(s);
}
else if (n == 4)
{
printf("Enter Department No. :~ ");
scanf("%i",&s);
dis75(s);
}
else if (n == 5){z = 0;}
else {printf("Enter Valid Command\n");}
}
}
Output:
Q4. Write a program to represent a sparse matrix in three tuple format using linked list
and write addition function to perform addition.
Code:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int val;
int row;
int column;
struct Node *ne;
} * s = '\0';
void create(struct Node ** start, int nz,int rw, int c )
{
struct Node *t, *r;
t = *start;
if (t == '\0')
{
t = (struct Node *) malloc (sizeof(struct Node));
t->val = nz;
t->row = rw;
t->column = c;
t->ne = '\0';
*start = t;
}
else
{
while (t->ne != '\0')
t = t->ne;
r = (struct Node *) malloc (sizeof(struct Node));
r->val = nz;
r->row = rw;
r->column = c;
r->ne = '\0';
t->ne = r;
}
}
void PrintList(struct Node* start)
{
struct Node *t, *r, *s;
t = r = s = start;
printf("Row_position :~ ");
while(t != '\0')
{
printf("%d ", t->row);
t = t->ne;
}
printf("\n");
printf("Column_postion :~ ");
while(r != '\0')
{
printf("%d ", r->column);
r = r->ne;
}
printf("\n");
printf("Value :~ ");
while(s != '\0')
{
printf("%d ", s->val);
s = s->ne;
}
printf("\n");
}
void add(struct Node * s1, struct Node * s2)
{
struct Node * t1 = s1, * t2 = s2;
while (t1 != '\0' && t2 != '\0')
{
if (t1->row < t2->row)
{
create(&s,t1->val,t1->row,t1->column);
t1 = t1->ne;
}
else if (t1->row == t2->row)
{
if (t1->column < t2->column){create(&s,t1->val,t1->row,t1->column);}
else if (t1->column == t2->column){create(&s,t1->val+t2->val,t1->row,t1-
>column);}
else {create(&s,t2->val,t1->row,t2->column);}
t1 = t1->ne;
t2 = t2->ne;
}
else
{
create(&s,t2->val,t2->row,t2->column);
t2 = t2->ne;
}
}
while (t1 != '\0')
{
create(&s,t1->val,t1->row,t1->column);
t1 = t1->ne;
}
while (t2 != '\0')
{
create(&s,t2->val,t2->row,t2->column);
t2 = t2->ne;
}
}
void main()
{
int m1[4][5] =
{
{0 , 0 , 3 , 0 , 4 },
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 6 , 0 , 0 }
};
int m2[4][5] =
{
{5 , 0 , 3 , 0 , 8 },
{0 , 0 , 5 , 0 , 0 },
{0 , 9 , 0 , 0 , 0 },
{0 , 0 , 6 , 0 , 3 }
};
struct Node * s1 = '\0', * s2 = '\0';
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (m1[i][j] != 0)
create(&s1, m1[i][j], i, j);
printf("First Matrix :~ \n");
PrintList(s1);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (m2[i][j] != 0)
create(&s2, m2[i][j], i, j);
printf("Second Matrix :~ \n");
PrintList(s2);
add(s1,s2);
PrintList(s);
}
Output:
Q5. Write a program to store a polynomial in linked list and write multiplication function to
perform multiplication of two polynomials.
Code:
#include<stdio.h>
#include<stdlib.h>
struct poly
{
int degree;
int coeff;
};
void pro(int n1, int n2, struct poly poly1[10], struct poly poly2[10])
{
int c = -1;
struct poly product[100];
for(int i = 0 ; i < n1 ; i++)
{
for (int j=0;j< n2 ;j++)
{
product[++c].degree=poly1[i].degree+poly2[j].degree;
product[c].coeff=poly1[i].coeff*poly2[j].coeff;
}
}
printf("\nThe Product Of Two Polynomials Is: \n");
for(int i = 0 ; i <= c ; i++)
{
if(product[i].degree==0){printf("%d ",product[i].coeff);}
else if(product[i].degree==1){printf("%dx ",product[i].coeff);}
else{printf("%dx^%d ",product[i].coeff,product[i].degree);}
if(i!=c){printf("+ ");}
}
}
void main()
{
struct poly poly1[10],poly2[10],product[100];
int n1,n2,count=-1;
int i,j;
printf("\nEnter Number Of Terms Of 1st Polynomial: ");
scanf("%d",&n1);
for(i = 0 ; i < n1 ; i++)
{
printf("\nEnter Degree: ");
scanf("%d",&poly1[i].degree);
printf("\nEnter Coefficient: ");
scanf("%d",&poly1[i].coeff);
}
printf("\nEnter Number Of Terms Of 2nd Polynomial: ");
scanf("%d",&n2);
for(i = 0 ; i < n2 ; i++)
{
printf("\nEnter Degree: ");
scanf("%d",&poly2[i].degree);
printf("\nEnter Coefficient: ");
scanf("%d",&poly2[i].coeff);
}
pro(n1,n2,poly1,poly2);
}
Output: