Data Structure Lab (MCA-6251)
Data Structure Lab (MCA-6251)
#include <stdio.h>
void bubble_sort(int[], int);
int main() {
int a[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
bubble_sort(a, n);
printf("\nSorted Elements: \n");
for (int i = 0; i < n; i++)
printf("%d\n", a[i]);
return 0;
}
void bubble_sort(int x[], int y) {
int swap;
for (int i = 0; i < y - 1; i++) {
for (int j = 0; j < y - 1 - i; j++) {
if (x[j] > x[j + 1]) {
swap = x[j];
x[j] = x[j + 1];
x[j + 1] = swap;
}
}
}
}
Q2a
#include<stdio.h>
one:
if(num == 1) {
printf("\nMove top disk from needle %c to needle %c\n", sndl, dndl);
goto four;
}
two:
top++;
stkn[top] = num;
stksndl[top] = sndl;
stkindl[top] = indl;
stkdndl[top] = dndl;
stkadd[top] = 3;
num--;
sndl = sndl;
temp = indl;
indl = dndl;
dndl = temp;
goto one;
three:
printf("\nMove top disk from needle %c to needle %c\n", sndl, dndl);
top++;
stkn[top] = num;
stksndl[top] = sndl;
stkindl[top] = indl;
stkdndl[top] = dndl;
stkadd[top] = 5;
num--;
temp = sndl;
sndl = indl;
indl = temp;
dndl = dndl;
goto one;
four:
if(top == -1)
return;
num = stkn[top];
sndl = stksndl[top];
indl = stkindl[top];
dndl = stkdndl[top];
add = stkadd[top];
top--;
if(add == 3)
goto three;
else if(add == 5)
goto four;
}
int main() {
int no;
printf("Enter the number of disks to be transferred: ");
scanf("%d", &no);
if(no < 1)
printf("There's nothing to move.\n");
else {
printf("\nNon-Recursive:\n");
hanoiNonRecursion(no, 'A', 'B', 'C');
printf("\nRecursive:\n");
hanoiRecursion(no, 'A', 'B', 'C');
}
return 0;
}
Q3a
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
int choice, data;
while (1) {
printf("\nLinked List Operations:\n");
printf("1. Insertion at beginning in sorted list\n");
printf("2. Deletion of first node\n");
printf("3. Display the list\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
head = insertSorted(head, data);
printf("Node inserted.\n");
break;
case 2:
head = deleteFirstNode(head);
break;
case 3:
printf("Current linked list: ");
displayList(head);
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
visited[0] = 1;
int main() {
int i, j, n;
int cost[10][10];
return 0;
}
Q4b
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
// If including this edge doesn't cause a cycle, include it in the result and increment the
index of result for next edge
if (x != y) {
result[e++] = next_edge;
unionSets(parent, rank, x, y);
}
// Else discard the next_edge
}
int main() {
int n, E;
Graph graph;
return 0;
}
Q5b
#include <stdio.h>
#include <stdlib.h>
push(&stack, start);
visited[start] = 1;
while (!isEmpty(&stack)) {
int node = pop(&stack);
printf("%d ", node);
// Visit all adjacent unvisited nodes
for (int i = 0; i < n; i++) {
if (graph[node][i] == 1 && !visited[i]) {
push(&stack, i);
visited[i] = 1;
}
}
}
printf("\n");
}
int main() {
int n, start;
int graph[MAX][MAX];
// Perform DFS
DFS(graph, n, start);
return 0;
}