Dsa Lab Assignment 05
Dsa Lab Assignment 05
traverse(*head);
}
void insertionTwoll(struct node **first, struct node **second)
{
struct node *t = *first;
struct node *t1 = *second;
struct node *third = NULL;
struct node *t3 = third;
while (t->next != NULL || t1->next != NULL)
{
if (t->data == t1->data && third == NULL)
{
t3->data = t->data;
t = t->next;
t1 = t1->next;
}
// }
// return 1;
}
int cycle(struct node **head)
{
int n = 3;
int c = 0;
while (c == 0)
{
struct node *t = *head;
struct node *prev = t;
for (int i = 0; i < n - 1; i++)
{
t = t->next;
}
while (prev->data != t->next->data && t != NULL)
{
t = t->next;
prev = prev->next;
}
if (prev->data == t->next->data)
{
c++;
}
else
n++;
}
if (c > 0)
{
return 1;
}
else
{
return 0;
}
}
void reverseEven(struct node **head)
{
struct node *t = *head;
struct node *eve = NULL;
struct node *teve;
while (t != NULL)
{
if (t->data % 2 == 0)
{
if (eve == NULL)
{
eve = t;
teve = eve;
}
else
{
struct node *p = createNode(t->data);
teve->next = p;
teve = teve->next;
}
}
t = t->next;
}
reverseNode(eve, NULL, &eve);
traverse(eve);
}
void swapNode(struct node **head, int k1, int k2)
{
int len = length(head) - k2 + 1;
struct node *t = *head;
struct node *j = t;
for (int i = 0; i < k1; i++)
{
t = t->next;
}
for (int i = 0; i < len; i++)
{
j = j->next;
}
int temp = t->data;
t->data = j->data;
j->data = temp;
traverse(*head);
}
struct node *reverseKNode(struct node **head, int k)
{
struct node *t = *head;
struct node *rev = t;
struct node *prev;
int n = k;
if (t == NULL)
return rev;
t->next=rev;
}
return *head;
}
int main()
{
struct node *head = NULL;
push(&head, 1);
push(&head, 6);
push(&head, 4);
push(&head, 2);
push(&head, 1);
traverse(head);
struct node *p = NULL;
push(&p, 2);
push(&p, 3);
push(&p, 40);
push(&p, 55);
push(&p, 61);
push(&p, 67);
printf("This is menu drive program \n");
int a;
printf("1.Search an element ,Delete it and add at First\n2. Find middle node element
\n3.Reverse m element\n4.Check if list is sorted or not\n5. Insert an element into the
linked list in sorted way\n6.The intersections elements of two linked list and store them
in a third linked list.\n7. Program to modify the linked list \n8. Palindrom \n9. Cycle in
ll \n10.Reverse only even position nodes\n11. Swap kth node from beginning with kth node
from end\n12. Reverse every k nodes.\n13. Rotate the linked list counter-clockwise by k
nodes.\n14. EXIT\n");
while (a != 14)
{
printf("enter your choice\n");
scanf("%d", &a);
switch (a)
{
case 1:
condition(&head, 21);
traverse(head);
break;
case 2:
middle(&head);
break;
case 3:
break;
case 4:
if (checkSort(&head))
{
printf("sorted");
}
else
printf("not sorted");
break;
case 5:
insertElement(&p);
break;
case 6:
insertionTwoll(&head, &p);
break;
case 7:
modify1(&p);
break;
case 8:
// if (palindrom(&head))
// {
// printf("the ll is palindromic ");
// }else{
// printf("the ll is not a plaindromic");
// }
palindrom(&head);
break;
case 9:
if (cycle(&head))
{
printf("cycle is present ");
}
else
{
printf("cycle not present");
}
break;
case 10:
reverseEven(&head);
break;
case 11:
printf("enter kth node from beginning");
int k1;
scanf("%d", &k1);
printf("enter kth node from end");
int k2;
scanf("%d", &k2);
swapNode(&head, k1, k2);
break;
case 12:
printf("enter no of node ");
int k;
scanf("%d", &k);
traverse(reverseKNode(&head, k));
break;
case 13:
printf("enter no of node ");
int v;
scanf("%d", &v);
traverse( counterClockwise(&head,v));
break;
case 14:
exit(0);
break;
default:
printf("you enter a invalid input\n");
break;
}
}
}
Output:
while (a != NULL)
{
c->next = a;
c = c->next;
a = a->next;
}
}
return head;
}
void push(struct Node **head_ref, int new_data)
{
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
int n, m;
printf("enter no of node to be created of first ll: ");
scanf("%d", &n);
printf("enter no of node to be created of second ll: ");
scanf("%d", &m);
Output:
Q3. Write a program to represent a polynomial using linked list. Write a function to add two
polynomials.
Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int cof;
int exp;
struct node *next;
};
struct node *createNode(int cof, int exp)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
p->cof = cof;
p->exp = exp;
p->next = NULL;
}
void push(struct node **head, int cof, int exp)
{
struct node *t = *head;
struct node *p = createNode(cof, exp);
if (*head == NULL)
{
*head = p;
}
else
{
Output:
Q4. Write a program to represent a sparse matrix in three tuple format using an array and perform
addition.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
int row_pos;
int col_pos;
struct Node *next;
};
m3 = curr_Node3;
while (curr_Node1)
{
curr_Node3->next = createNode(curr_Node1->data, curr_Node1->row_pos, curr_Node1-
>col_pos);
curr_Node1 = curr_Node1->next;
curr_Node3 = curr_Node3->next;
}
while (curr_Node2)
{
curr_Node3->next = createNode(curr_Node2->data, curr_Node2->row_pos, curr_Node2-
>col_pos);
curr_Node2 = curr_Node2->next;
curr_Node3 = curr_Node3->next;
}
return m3;
}
int main()
{
int SparseData[5]; // First row of sparse matrix for containing number of rows,
columns, and elements with values for matrices 1, 2, and the sum matrix respectively
SparseData[2] = SparseData[3] = SparseData[4] = 0;
int d;
printf("Enter number of rows and columns: ");
scanf("%d%d", &SparseData[0], &SparseData[1]);
struct Node *matrix1 = NULL, *matrix2 = NULL;
printf("Enter the elements for matrix 1:\n");
for (int i = 0; i < SparseData[0]; i++)
{
for (int j = 0; j < SparseData[1]; j++)
{
scanf("%d", &d);
if (d)
{
createSparse(&matrix1, d, i, j);
SparseData[2]++;
}
}
}
printf("\nEnter the elements for matrix 2:\n");
for (int i = 0; i < SparseData[0]; i++)
{
for (int j = 0; j < SparseData[1]; j++)
{
scanf("%d", &d);
if (d)
{
createSparse(&matrix2, d, i, j);
SparseData[3]++;
}
}
}
printf("\nEntered Sparse Matrices are:\n");
printf("\nSparse Matrix 1:\n");
printf("Row\tColumn\tValue\n");
//printf("Row\tColumn\tValue\n%d\t%d\t%d\n", SparseData[0], SparseData[1],
SparseData[2]);
printSparse(matrix1);
printf("\nSparse Matrix 2:\n");
printf("Row\tColumn\tValue\n");
//printf("Row\tColumn\tValue\n%d\t%d\t%d\n", SparseData[0], SparseData[1],
SparseData[3]);
printSparse(matrix2);
struct Node *matrix3 = addSparse(matrix1, matrix2);
printf("\nResult of summation of the given Sparse Matrices:\n");
printf("Row\tColumn\tValue\n");
printSparse(matrix3);
return 0;
}
Output: