Dsa Lab
Dsa Lab
Dsa Lab
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* merged = NULL;
int n1, n2, val;
return 0;
}
Mioddle element
int main() {
scanf("%d", &n1);
scanf("%d", &val);
newNode->data = val;
newNode->next = head1;
head1 = newNode;
scanf("%d", &val);
newNode->data = val;
newNode->next = head2;
head2 = newNode;
printList(merged);
slow = slow->next;
fast = fast->next->next;
return 0;
}
reverse
int main() {
scanf("%d", &n1);
scanf("%d", &val);
newNode->data = val;
newNode->next = head1;
head1 = newNode;
scanf("%d", &n2);
scanf("%d", &val);
newNode->data = val;
newNode->next = head2;
head2 = newNode;
}
printList(merged);
next = current->next;
current->next = prev;
prev = current;
current = next;
merged = prev;
printList(merged);
return 0;
}
sum
int main() {
scanf("%d", &n1);
scanf("%d", &val);
newNode->data = val;
newNode->next = head1;
head1 = newNode;
scanf("%d", &n2);
scanf("%d", &val);
newNode->data = val;
newNode->next = head2;
head2 = newNode;
printList(merged);
sum += current->data;
current = current->next;
printf("The sum of the elements in the merged list is: %d\n", sum);
return 0;
Sum of alternative
int main() {
scanf("%d", &n1);
scanf("%d", &val);
newNode->data = val;
newNode->next = head1;
head1 = newNode;
scanf("%d", &n2);
scanf("%d", &val);
newNode->data = val;
newNode->next = head2;
head2 = newNode;
printList(merged);
// Traverse the merged list and add up alternate node's data values
if (count % 2 == 0) {
sum += current->data;
current = current->next;
count++;
printf("The sum of the alternate positions of the merged list is: %d\n", sum);
return 0;
}
#include <stdio.h>
#include <limits.h>
// find the minimum key value in the set of vertices not yet included in the MST
return min_index;
printf("Edge \tWeight\n");
int key[V]; // key values used to pick minimum weight edge in cut
// pick the minimum key vertex from the set of vertices not yet included in MST
mstSet[u] = true;
// update key and parent arrays of adjacent vertices of the picked vertex
printMST(parent, graph);
int main() {
/* Example graph
2 3
(0)--(1)--(2)
| /\ |
6| 8/ \5 |7
|/ \|
(3)-------(4)
9 */
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0},
};
primMST(graph);
return 0;
}
Dijkstras
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
min = dist[i];
min_index = i;
return min_index;
int dist[V];
int visited[V];
dist[i] = INT_MAX;
visited[i] = 0;
dist[start] = 0;
if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
int main() {
int graph[V][V] = {
{0, 5, 0, 9, 2, 0},
{5, 0, 2, 0, 0, 7},
{0, 2, 0, 3, 0, 0},
{9, 0, 3, 0, 0, 8},
{2, 0, 0, 0, 0, 0},
{0, 7, 0, 8, 0, 0}
};
dijkstra(graph, 0);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
min = dist[i];
min_index = i;
return min_index;
int dist[V];
int visited[V];
dist[i] = INT_MAX;
visited[i] = 0;
dist[start] = 0;
visited[u] = 1;
for (int v = 0; v < n; v++) {
if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
{
int main() {
int n;
scanf("%d", &n);
int graph[V][V];
scanf("%d", &graph[i][j]);
int start;
return 0;
Prims
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int graph[MAX_VERTICES][MAX_VERTICES];
int n;
void primMST() {
int parent[n];
int key[n];
int mstSet[n];
key[i] = INT_MAX;
mstSet[i] = 0;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < n - 1; count++) {
minKey = key[v];
minIndex = v;
mstSet[minIndex] = 1;
parent[v] = minIndex;
key[v] = graph[minIndex][v];
printf("Edges in MST:\n");
int main() {
scanf("%d", &n);
scanf("%d", &graph[i][j]);
primMST();
return 0;
Krushals
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
while(ne < n)
for(i=1,min=999;i<=n;i++)
min=cost[i][j];
a=u=i;
b=v=j;
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
cost[a][b]=cost[b][a]=999;
getch();
int find(int i)
while(parent[i])
i=parent[i];
return i;
if(i!=j)
parent[j]=i;
return 1;
return 0;
}
Stack
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
scanf("%d",&n);
printf("\n----------------------------------------------\n");
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
printf("Exiting....");
break;
default:
};
void push ()
int val;
if (top == n )
printf("\n Overflow");
else
scanf("%d",&val);
stack[top] = val;
}
void pop ()
if(top == -1)
printf("Underflow");
else
void show()
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
printf("Stack is empty");
}
Queue
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\
n===================================================
==============\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\
n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");
}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
}
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
for(i=front;i<=rear;i++)
{
printf("\n%d\n",queue[i]);
}
}
}