Final dsa lab work
Final dsa lab work
Program :
#include<stdio.h>
int main(){
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
towerOfHanoi(n, 'A', 'B', 'C');
return 0;
}
Output :
Enter the number of disks: 2
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Practical No – 02
struct Node
{
int value;
int row_position;
int column_postion;
temp = *start;
if (temp == NULL)
temp->value = non_zero_element;
temp->row_position = row_index;
temp->column_postion = column_index;
temp->next = NULL;
*start = temp;
}
else
{
while (temp->next != NULL)
temp = temp->next;
r->value = non_zero_element;
r->row_position = row_index;
r->column_postion = column_index;
r->next = NULL;
temp->next = r;
}
}
temp = start;
s = start;
r = start;
printf("row_position: ");
temp = temp->next;
}
printf("\n");
printf("column_postion: ");
while (r != NULL)
r = r->next;
}
printf("\n");
printf("Value: ");
while (s != NULL)
s = s->next;
}
printf("\n");
}
int main()
{
int n, m, i, j;
printf("enter thr row and column of sparse matrix : ");
scanf("%d\t%d", &n, &m);
int ab[n][m];
printf("enter the element of matrix : ");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
scanf("%d", &ab[i][j]);
}
}
if (ab[i][j] != 0)
PrintList(start);
return 0;
}
Output :
Enter thr row and column of sparse matrix : 2 2
Enter the element of matrix :
10
0
2
0
row_position: 0 1
column_postion: 0 0
Value: 10 2
Practical No – 03
return head;
}
}
void print(struct node *h)
{
if (h == NULL)
{
printf("empty list");
}
else
while (h != NULL)
{
printf("%dx^%d+", h->coff, h->deg);
// printf("address of nextt node=%u\n",h->next);
h = h->next;
}
}
struct node *polyadd(struct node *h1, struct node *h2)
{
struct node *ptr, *headp, *p, *q;
p = h1;
q = h2;
ptr = (struct node *)malloc(sizeof(struct node));
headp = ptr;
while (p && q)
{
if (p->deg > q->deg)
{
ptr->coff = p->coff;
ptr->deg = p->deg;
p = p->next;
}
else
{
if (p->deg < q->deg)
{
ptr->coff = q->coff;
ptr->deg = q->deg;
q = q->next;
}
else
{
ptr->coff = p->coff + q->coff;
ptr->deg = p->deg;
p = p->next;
q = q->next;
}
}
if (p && q)
{
ptr->next = (struct node *)malloc(sizeof(struct node));
ptr = ptr->next;
}
}
if (p || q)
{
while (p != NULL)
{
ptr->next = (struct node *)malloc(sizeof(struct node));
ptr = ptr->next;
ptr->coff = p->coff;
ptr->deg = p->deg;
p = p->next;
}
while (q != NULL)
{
ptr->next = (struct node *)malloc(sizeof(struct node));
ptr = ptr->next;
ptr->coff = q->coff;
ptr->deg = q->deg;
q = q->next;
}
}
return headp;
}
int main()
{
int d, b;
printf("enter the total element of list one ");
scanf("%d", &b);
printf("first list\n");
head1 = create(b);
printf("enter the total element of list second");
scanf("%d", &d);
printf("second list\n");
head2 = create(d);
return 0;
}
Output :
enter the total element of list one 2
first list
coff and degree 2 2
coff and degree of next term4 1
enter the total element of list second2
second list
coff and degree2 2
coff and degree of next term 5 1
nodes of first list
2x^2+4x^1+nodes of secod list
2x^2+5x^1+
--------------
list aftermerge of two list
4x^2+9x^1+
Practical No – 04
void insert(void);
void delete(void);
int main()
{
int ch;
int n = 0;
struct ssl *HEAD = NULL;
// printf("how many nodes\t");
printf("How many nodes :");
scanf("\n %d", &n);
HEAD = createssl(n);
// print(HEAD);
printf("\n1.print");
printf("\n2.insert");
printf("\n3.delete");
printf("\n4.exit\n");
// printf("/n");
scanf("\n%d", &ch);
switch (ch)
{
case 1:
print();
break;
case 2:
insert();
break;
case 3:
delete ();
break;
};
} while (ch != 4);
return 0;
}
return (head);
}
void print()
{
struct ssl *temp = head;
while (temp != NULL)
{
printf("\t%d", temp->data);
temp = temp->next;
}
}
void insert()
{
node1 *t;
node1 *r = head;
int pos, i = 1;
t = (node1 *)malloc(sizeof(node1));
printf("Enter the data : ");
scanf("%d", &t->data);
printf("Enter the position : ");
scanf("%d", &pos);
if (pos == 1)
{
t->next = head;
head = t;
i--;
}
else
{
while (pos != i + 1)
{
r = r->next;
i++;
}
t->next = r->next;
r->next = t;
}
}
void delete()
{
node1 *temp;
node1 *ptr;
node1 *cur = head;
int pos, i = 1;
printf("Enter the position to delete :");
scanf("%d", &pos);
if (pos == 1)
{
ptr = head;
head = head->next;
free(ptr);
}
else
{
while (pos != i + 1)
{
cur = cur->next;
i++;
}
ptr = cur->next;
cur->next = ptr->next;
printf("Element deletedis : %d", ptr->data);
free(ptr);
}
}
Output :
How many nodes :3
1.print
2.insert
3.delete
4.exit
1
10 20 40 30
1.print
2.insert
3.delete
4.exit
3
Enter the position to delete :3
Element deletedis : 40
1.print
2.insert
3.delete
4.exit
1
10 20 30
1.print
2.insert
3.delete
4.exit
Practical No – 05
void print();
void insert(void);
void create(int n);
void delete(void);
void del_end(void);
void del_mid(void);
void in_mid(void);
void in_last(void);
int main()
{
int n, ch;
printf("Enter the total number of nodes to be inserted : ");
scanf("%d", &n);
create(n);
do
{
printf("\nEnter your choice : \n");
printf("\n1.print");
printf("\n2.insert");
printf("\n3.delete");
printf("\n4.exit\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
print();
break;
case 2:
insert();
break;
case 3:
delete ();
break;
}
} while (ch != 4);
return 0;
}
void create(int n)
{
int i;
void print()
{
struct node *ptr = head->next; // Start from the first node
while (ptr != NULL)
{
printf("%d\t", ptr->data);
ptr = ptr->next;
}
}
void insert()
{
int ch;
printf("\n1.Insert at any position except end position");
printf("\n2.Insert at end position\n");
scanf("\t%d", &ch);
switch (ch)
{
case 1:
in_mid();
break;
case 2:
in_last();
break;
}
}
void in_mid()
{
node1 *temp;
node1 *cur = head;
int pos, i = 1;
temp = (node1 *)malloc(sizeof(node1));
printf("\nEnter the position : ");
scanf("%d", &pos);
printf("\nEnter the data : ");
scanf("%d", &temp->data);
while (pos != i)
{
cur = cur->next;
i++;
}
if (pos == i)
{
temp->prev = cur;
temp->next = cur->next;
cur->next = temp;
cur = temp->next;
cur->prev = temp;
}
}
void in_last()
{
node1 *qtr;
qtr = (node1 *)malloc(sizeof(node1));
printf("\nEnter the data : ");
scanf("%d", &qtr->data);
qtr->next = NULL;
newnode->next = qtr;
qtr->prev = newnode;
newnode = qtr;
}
void delete()
{
int ch;
printf("\n1.delete at any position except end position");
printf("\n2.delete at end position\n");
scanf("\t%d", &ch);
switch (ch)
{
case 1:
del_mid();
break;
case 2:
del_end();
break;
}
}
void del_mid()
{
node1 *temp;
node1 *cur = head;
int pos, i = 0;
printf("Enter the position to delete : ");
scanf("%d", &pos);
while (pos != i + 1)
{
cur = cur->next;
i++;
}
if (pos == i + 1)
{
temp = cur->next;
cur->next = temp->next;
cur = temp->next;
temp->next->prev = cur;
free(temp);
}
}
void del_end()
{
node1 *temp = head;
while (temp->next != newnode)
{
temp = temp->next;
}
newnode = temp;
temp = temp->next;
newnode->next = NULL;
printf("Deleted element is =%d", temp->data);
free(temp);
}
Output :
Enter the total number of nodes to be inserted : 3
Enter data of your node : 10
Enter data of your node : 20
Enter data of your node : 30
1.print
2.insert
3.delete
4.exit
2
1.print
2.insert
3.delete
4.exit
3
1.print
2.insert
3.delete
4.exit
1
20 30 40
Enter your choice :
1.print
2.insert
3.delete
4.exit
Practical No – 06
void insert(void);
void delete(void);
int main()
{
int ch;
int n = 0;
struct ssl *HEAD = NULL;
printf("How many nodes : ");
scanf("\n %d", &n);
HEAD = createssl(n);
// print(HEAD);
printf("\n1.print");
printf("\n2.insert");
printf("\n3.delete");
printf("\n4.exit\t");
// printf("/n");
scanf("\n%d", &ch);
switch (ch)
{
case 1:
print();
break;
case 2:
insert();
break;
case 3:
delete ();
break;
};
} while (ch != 4);
return 0;
}
return (head);
}
void print()
{
struct ssl *temp = head;
do
{
printf("\t%d", temp->data);
temp = temp->next;
} while (temp != new);
printf("\t%d", temp->data);
}
void insert()
{
node1 *t;
node1 *r = head;
int pos, i = 1;
t = (node1 *)malloc(sizeof(node1));
printf("Enter the data : ");
scanf("%d", &t->data);
printf("Enter the position : ");
scanf("%d", &pos);
if (pos == 1)
{
t->next = head;
head = t;
i--;
}
else
{
while (pos != i + 1)
{
r = r->next;
i++;
}
t->next = r->next;
r->next = t;
}
}
void delete()
{
node1 *temp;
node1 *ptr;
node1 *cur = head;
int pos, i = 1;
printf("Enter the position to delete : ");
scanf("%d", &pos);
if (pos == 1)
{
ptr = head;
head = head->next;
free(ptr);
}
else
{
while (pos != i + 1)
{
cur = cur->next;
i++;
}
ptr = cur->next;
cur->next = ptr->next;
printf("Element deletedis %d", ptr->data);
free(ptr);
}
}
Output :
How many nodes : 3
1.print
2.insert
3.delete
4.exit 1
10 20 40 30
1.print
2.insert
3.delete
4.exit 3
Enter the position to delete : 1
1.print
2.insert
3.delete
4.exit 1
20 40 30
1.print
2.insert
3.delete
4.exit
Practical No – 07
void print();
void insert(void);
void create(int n);
void delete(void);
void del_end(void);
void del_mid(void);
void in_mid(void);
void in_last(void);
int main()
{
int n, ch;
printf("Enter the total number of nodes to be inserted : ");
scanf("%d", &n);
create(n);
do
{
printf("\n1.print");
printf("\n2.insert");
printf("\n3.delete");
printf("\n4.exit\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
print();
break;
case 2:
insert();
break;
case 3:
delete ();
break;
}
} while (ch != 4);
return 0;
}
void create(int n)
{
int i;
void print()
{
struct node *ptr = head->next; // Start from the first node
while (ptr != newnode)
{
printf(" %d\t", ptr->data);
ptr = ptr->next;
}
printf("%d\t", newnode->data);
}
void insert()
{
int ch;
printf("\n1.Insert at any position except end position");
printf("\n2.Insert at end position\n");
scanf("\t%d", &ch);
switch (ch)
{
case 1:
in_mid();
break;
case 2:
in_last();
break;
}
}
void in_mid()
{
node1 *temp;
node1 *cur = head;
int pos, i = 0;
temp = (node1 *)malloc(sizeof(node1));
printf("\nEnter the position : ");
scanf("%d", &pos);
printf("\nEnter the data : ");
scanf("%d", &temp->data);
while (pos != i + 1)
{
cur = cur->next;
i++;
}
if (pos == i + 1)
{
temp->prev = cur;
temp->next = cur->next;
cur->next = temp;
cur = temp->next;
cur->prev = temp;
}
}
void in_last()
{
node1 *qtr;
qtr = (node1 *)malloc(sizeof(node1));
printf("\nEnter the data : ");
scanf("%d", &qtr->data);
qtr->next = head;
newnode->next = qtr;
qtr->prev = newnode;
newnode = qtr;
head->prev = newnode;
}
void delete()
{
int ch;
printf("\n1.delete at any position except end position");
printf("\n2.delete at end position\n");
scanf("\t%d", &ch);
switch (ch)
{
case 1:
del_mid();
break;
case 2:
del_end();
break;
}
}
void del_mid()
{
node1 *temp;
node1 *cur = head;
int pos, i = 0;
printf("Enter the position to delete : ");
scanf("%d", &pos);
while (pos != i + 1)
{
cur = cur->next;
i++;
}
if (pos == i + 1)
{
temp = cur->next;
cur->next = temp->next;
cur = temp->next;
temp->next->prev = cur;
free(temp);
}
}
void del_end()
{
node1 *temp = head;
while (temp->next != newnode)
{
temp = temp->next;
}
newnode = temp;
temp = temp->next;
newnode->next = NULL;
printf("Deleted element is =%d", temp->data);
free(temp);
}
Output :
Enter the total number of nodes to be inserted : 3
Enter data of your node : 10
Enter data of your node : 20
Enter data of your node : 30
address of head=9514896
and address of next of newnode=9514896
1.print
2.insert
3.delete
4.exit
1
10 20 30 50
1.print
2.insert
3.delete
4.exit
4