0% found this document useful (0 votes)
0 views

Final dsa lab work

The document contains multiple practical programming exercises in C, including implementations for the Tower of Hanoi, Sparse Matrix, Polynomial Addition, Singly Linked List, and Doubly Linked List. Each section provides the objective, source code, and sample output for the respective program. The exercises focus on data structures and algorithms, demonstrating various operations and functionalities.

Uploaded by

pa9889630188
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Final dsa lab work

The document contains multiple practical programming exercises in C, including implementations for the Tower of Hanoi, Sparse Matrix, Polynomial Addition, Singly Linked List, and Doubly Linked List. Each section provides the objective, source code, and sample output for the respective program. The exercises focus on data structures and algorithms, demonstrating various operations and functionalities.

Uploaded by

pa9889630188
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Practical No – 01

Object : Write a program to create Tower of Hanoi .

Program :
#include<stdio.h>

void towerOfHanoi(int n, char source, char auxilary, char target)


{
if (n==1)
{
printf("Move disk 1 from %c to %c\n", source, target);
// here we use %c for char data type
return;
}
towerOfHanoi(n - 1, source, target, auxilary);
printf("Move disk %d from %c to %c\n", n, source, target);
towerOfHanoi(n - 1, auxilary, source, target);
}

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

Object : Write a program to create Sparse Matrix.


Program :
#include <stdio.h>
#include <stdlib.h>

struct Node
{

int value;

int row_position;

int column_postion;

struct Node *next;


};

// Function to create new node

void create_new_node(struct Node **start, int non_zero_element,

int row_index, int column_index)


{

struct Node *temp, *r;

temp = *start;

if (temp == NULL)

// Create new node dynamically

temp = (struct Node *)malloc(sizeof(struct Node));

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;

// Create new node dynamically

r = (struct Node *)malloc(sizeof(struct Node));

r->value = non_zero_element;

r->row_position = row_index;

r->column_postion = column_index;

r->next = NULL;

temp->next = r;
}
}

void PrintList(struct Node *start)


{

struct Node *temp, *r, *s;

temp = start;
s = start;
r = start;
printf("row_position: ");

while (temp != NULL)

printf("%d ", temp->row_position);

temp = temp->next;
}

printf("\n");

printf("column_postion: ");

while (r != NULL)

printf("%d ", r->column_postion);

r = r->next;
}

printf("\n");

printf("Value: ");
while (s != NULL)

printf("%d ", s->value);

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]);
}
}

struct Node *start = NULL;

for (int i = 0; i < n; i++)

for (int j = 0; j < m; j++)

if (ab[i][j] != 0)

create_new_node(&start, ab[i][j], i, j);

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

Object : Write a program to perform Polynomials Addition.


Program :
#include <stdio.h>
#include <stdlib.h>
struct node
{
int coff, deg;
struct node *next;
};
struct node *head1, *head2, *headm;
struct node *create(int n)
{
int i;
struct node *p, *q, *head;
if (n != 0)
{
p = (struct node *)malloc(sizeof(struct node));
printf("coff and degree");
scanf("%d%d", &p->coff, &p->deg);
p->next = NULL;
head = p;
for (i = 2; i <= n; i++)
{
q = (struct node *)malloc(sizeof(struct node));
printf("coff and degree of next term");
scanf("%d%d", &q->coff, &q->deg);
q->next = NULL;
p->next = q;
p = q;
}

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);

printf("nodes of first list\n");


print(head1);

printf("nodes of secod list\n");


print(head2);

headm = polyadd(head1, head2);


printf("--------------\n");
printf("list aftermerge of two list\n");
print(headm);

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

Object : Write a program to perform all operations on Singly


Linked List.
Program :
// normal singly
#include <stdio.h>
#include <stdlib.h>

typedef struct ssl


{
int data;
struct ssl *next;
} node1;
struct ssl *temp = NULL;
struct ssl *head = NULL;
struct ssl *createssl(int n);
void print();

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("\n Enter your choice :");

do // 1 means condition always true


{

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;
}

struct ssl *createssl(int n)


{
int i = 0;

struct ssl *new = NULL;

new = (struct ssl *)malloc(sizeof(struct ssl));


printf("\nEnter your first node :\t");
scanf("\n%d", &new->data);
new->next = NULL;
head = new;
temp = head;

for (i = 1; i < n; i++)


{
new = (struct ssl *)malloc(sizeof(struct ssl));
printf("Enter data of next node : ");
scanf("\n%d", &new->data);
new->next = NULL;
// head->next =new;
temp->next = new;
temp = temp->next;
}
temp->next = NULL;

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

Enter your first node : 10


Enter data of next node : 20
Enter data of next node : 30

Enter your choice :


1.print
2.insert
3.delete
4.exit
2
Enter the data : 40
Enter the position : 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

Object : Write a program to perform all operations on Doubly


Linked List.
Program :
// doubly
#include <stdio.h>
#include <stdlib.h>

typedef struct node


{
int data;
struct node *next, *prev;
} node1;

struct node *newnode;


struct node *head = NULL;
struct node *temp = NULL;

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;

head = (struct node *)malloc(sizeof(struct node));


head->next = NULL;
head->prev = NULL;
temp = head; // Initialize temp to head

for (i = 1; i <= n; i++)


{
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of your node : ");
scanf("%d", &newnode->data);
newnode->next = NULL;
newnode->prev = temp;
temp->next = newnode;
temp = temp->next;
}
}

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

Enter your choice :

1.print
2.insert
3.delete
4.exit
2

1.Insert at any position except end position


2.Insert at end position
2

Enter the data : 40

Enter your choice :


1.print
2.insert
3.delete
4.exit
1
10 20 30 40
Enter your choice :

1.print
2.insert
3.delete
4.exit
3

1.delete at any position except end position


2.delete at end position
1
Enter the position to delete : 1

Enter your choice :

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

Object : Write a program to perform all operations on Circular


Singly Linked List.
Program :
// circular singly
#include <stdio.h>
#include <stdlib.h>

typedef struct ssl


{
int data;
struct ssl *next;
} node1;

struct ssl *new = NULL;


struct ssl *temp = NULL;
struct ssl *head = NULL;
struct ssl *createssl(int n);
void print();

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("\nEnter your choice : ");

do // 1 means condition always true


{

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;
}

struct ssl *createssl(int n)


{
int i = 0;

new = (struct ssl *)malloc(sizeof(struct ssl));


printf("\nEnter your first node : ");
scanf("\n%d", &new->data);
new->next = NULL;
head = new;
temp = head;

for (i = 1; i < n; i++)


{
new = (struct ssl *)malloc(sizeof(struct ssl));
printf("Enter data of node : ");
scanf("\n%d", &new->data);
new->next = NULL;
// head->next =new;
temp->next = new;
temp = temp->next;
}
temp->next = new;

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

Enter your first node : 10


Enter data of node : 20
Enter data of node : 30

Enter your choice :


1.print
2.insert
3.delete
4.exit 2
Enter the data : 40
Enter the position : 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

Object : Write a program to perform all operations on Circular


Doubly Linked List.
Program :
#include <stdio.h>
#include <stdlib.h>

typedef struct node


{
int data;
struct node *next, *prev;
} node1;

struct node *newnode;


struct node *head = NULL;
struct node *temp = NULL;

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);

printf("\n\n\n Enter your choice : ");

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;

head = (struct node *)malloc(sizeof(struct node));


head->next = NULL;
head->prev = NULL;
temp = head; // Initialize temp to head

for (i = 1; i <= n; i++)


{
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of your node : ");
scanf("%d", &newnode->data);
newnode->next = NULL;
newnode->prev = temp;
temp->next = newnode;
temp = temp->next;
}
temp->next = head;
head->prev = temp;
printf("\naddress of previous of head=%u\n and address of newnode=%u\n", head->prev, newnode);
printf("\naddress of head=%u\n and address of next of newnode=%u\n", head, newnode->next);
}

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 previous of head=9506136


and address of newnode=9506136

address of head=9514896
and address of next of newnode=9514896

Enter your choice :


1.print
2.insert
3.delete
4.exit
2

1.Insert at any position except end position


2.Insert at end position
2

Enter the data : 50

1.print
2.insert
3.delete
4.exit
1
10 20 30 50
1.print
2.insert
3.delete
4.exit
4

You might also like