dsa code 306
dsa code 306
NO: 24MCA306
PART - A
Program No: 1
Program statement: Write a program to implement STACK with the following
operations.
a. Push
b. Pop
c. Display
Source code:
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
void main()
{
int ch;
printf("1.PUSH \n 2.POP \n 3.DISPLAY \n 4.EXIT \n");
while(1)
{
printf(" \nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice \n");
}
}
}
void push()
{
int data;
if(top == MAX-1)
printf("Stack Overflow \n");
else
{
printf("Enter the element to push: ");
scanf("%d",&data);
top++;
a[top]=data;
}
}
void pop()
{
if(top==-1)
printf("Stack Underflow \n");
else
{
printf("Popped element is:%d\n",a[top]);
--top;
}
}
void display()
{
int i;
if(top>=0)
{
printf("Elements are: \n");
for(i=top;i>=0;i--)
printf("%d \n",a[i]);
}
else
{
printf("Stack is empty \n");
}
Output:
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Elements are:
10
Program No: 2
Program statement: Write a program to implement QUEUE with the following
operations
a. Enqueue
b. Dequeue
c. Display
Source code
#include <stdio.h>
#include<stdlib.h>
#define SIZE 10
void enqueue();
void dequeue();
void display();
int arr[SIZE];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
printf("\n1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice \n");
}
}
}
void enqueue() {
int item;
if (rear == SIZE - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Enter the element to be inserted: ");
scanf("%d",&item);
rear = rear + 1;
arr[rear] = item;
}
}
void dequeue() {
if (front == - 1 || front > rear)
printf("Queue Underflow \n");
else
{
printf("Element deleted from queue is : %d\n", arr[front]);
front = front + 1;
}
}
void display() {
if (front == - 1 || front > rear)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (int i = front; i <= rear; i++)
printf("%d ", arr[i]);
printf("\n");
}
}
Output:
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is empty
Program No: 3
Program statement: Write a program to implement Insertion Sort.
Source code:
#include <stdio.h>
void insertionSort(int a[], int n)
{
for (int i = 1; i < n; i++)
{
int key = a[i];
int j = i - 1;
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
}
}
int main()
{
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter the array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
insertionSort(a, n);
printf("\nSorted array: \n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
Output:
Sorted array:
-2 0 9 11 45
Program No: 4
Program statement: Write a program to implement Selection Sort.
Source code:
#include <stdio.h>
void selectionSort(int a[], int n)
{
int i, j, min, temp;
for (i = 0; i < n-1; i++)
{
min = i;
for (j = i+1; j < n; j++)
{
if (a[j] < a[min])
{
min = j;
}
}
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
int main()
{
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter the array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
selectionSort(a, n);
printf("Sorted array: \n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return 0;
}
Output:
Program No: 5
Program statement: Write a program to implement Bubble Sort.
Source code:
#include <stdio.h>
void bubbleSort(int a[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
int main()
{
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter the array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
bubbleSort(a, n);
printf("\nSorted array: \n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
Output:
Sorted array:
-12 0 3 5 9
Program No: 6
Program statement: Write a program to implement Linear Search.
Source code:
#include <stdio.h>
int main() {
int n,arr[100],key;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the key element:");
scanf("%d",&key);
int i = linearSearch(arr, n, key);
if (i == -1)
printf("Key Not Found");
else
printf("Key Found at Index: %d", i);
return 0;
}
Output 1:
Enter the number of elements:5
Enter the elements:2 9 3 1 0
Enter the key element:9
Key Found at Index: 1
Output 2:
Enter the number of elements:5
Enter the elements:2 9 3 1 0
Enter the key element:5
Key Not Found
Program No: 7
Program statement: Write a program to implement Binary Search.
Source code:
#include <stdio.h>
int main() {
int array[100],target,n,i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
printf("enter the target element:");
scanf("%d",&target);
int result = binarySearch(array, n ,target);
if (result == -1)
printf("Element is not present in the array”);
else
printf(“Element is present at index %d\n", result);
return 0;
}
Output:
Program No: 8
Program statement: Write a program to implement Breadth First Search.
Source code:
#include <stdio.h>
printf("BFS: ");
while (front < rear) {
int curr = queue[front++];
printf("%d ", curr);
int main() {
int vertices, edges, start;
Output:
Program No: 9
Program statement: Write a program to implement Depth First Search.
Source code:
#include <stdio.h>
printf("DFS: ");
while (top >= 0) {
int curr = stack[top--];
printf("%d ", curr);
int main() {
int vertices, edges, v1, v2, start;
dfs(start, vertices);
return 0;
}
Output:
PART - B
Program No: 1
Program statement: Write a C program to evaluate postfix expression.
Source code:
#include<stdio.h>
#include<ctype.h>
int stack[20];
int top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() {
return stack[top--];
}
int main() {
char exp[20];
char *e;
int n1, n2, n3, num;
case '-':
n3 = n2 - n1;
break;
case '*':
n3 = n2 * n1;
break;
case '/':
n3 = n2 / n1;
break;
}
push(n3);
}
e++;
}
printf("The result of expression %s = %d\n", exp, pop());
return 0;
}
Output:
Program No: 2
Program statement: Write a C program that converts infix expression to
postfix expression.
Source code:
#include<stdio.h>
#include<ctype.h>
char stack[20];
int top=-1;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x=='(')
return 0;
if(x=='+' || x=='-')
return 1;
if(x=='*' || x=='/')
return 2;
if(x=='$')
return 3;
}
int main()
{
char exp[20];
char *e,x;
printf("Enter the Infix Expression:: ");
scanf("%s", exp);
e=exp;
printf("\nPostfix Expression:: ");
while(*e!='\0')
{
if(isalnum(*e))
printf("%c", *e);
else if(*e=='(')
push(*e);
else if(*e==')'){
while((x=pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c", pop());
push(*e);
}
e++;
}
while(top!=-1){
printf("%c", pop());
}
printf("\n");
}
Output:
Program No: 3
Program statement: Write a program to implement Merge Sort.
Source code:
#include <stdio.h>
#include <stdlib.h>
void mergesort(int, int);
void merge(int, int, int);
int a[100];
int main() {
int i, n;
printf("Enter the range: ");
scanf("%d", &n);
printf("\nEnter the %d numbers: ", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
mergesort(0, n - 1);
Output:
After sorting: -2 0 3 4 7
Program No: 4
Program statement: Write a C program to implement Dijkstra’s Algorithm.
Source code:
#include <stdio.h>
#include <limits.h>
#define MAX 100
}
}
}
int main() {
int n, e, u, v, w;
int graph[MAX][MAX] = {0};
dijkstra(graph, n, u);
return 0;
}
Output:
Program No: 5
Program statement: Write a C program to implement Kruskal’s Algorithm.
Source code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
int u, v, w;
} Edge;
int parent[MAX];
int find(int i) {
while (parent[i] != i)
i = parent[i];
return i;
}
int totalWeight = 0;
printf("Edges in the Minimum Spanning Tree:\n");
if (find(u) != find(v)) {
printf("(%d, %d) -> %d\n", u, v, w);
totalWeight += w;
unionSets(u, v);
}
}
printf("Total weight of the Minimum Spanning Tree: %d\n", totalWeight);
}
int main() {
int n, e;
Edge edges[MAX];
kruskal(edges, n, e);
return 0;
}
Output:
Program No: 6
Program statement: Write a C program to implement Prims Algorithm.
Source code:
#include <stdio.h>
#include <limits.h>
int find() {
int min_weight = INT_MAX, min_index = -1;
for (int i = 0; i < m; i++) {
if ((visited[edges[i][0]] && !visited[edges[i][1]]) ||
(visited[edges[i][1]] && !visited[edges[i][0]])) {
if (edges[i][2] < min_weight) {
min_weight = edges[i][2];
min_index = i;
}
}
}
return min_index;
}
int main() {
int total_weight = 0, count = 0;
Output:
Program No: 7
Program statement: Write a C program to perform Inorder, Preorder, and Postorder
traversal of the Binary tree.
Source code:
#include <stdio.h>
#define MAX_NODES 100
int main() {
int tree[MAX_NODES], n;
printf("Enter the number of nodes in the binary tree: ");
scanf("%d", &n);
inorder(tree, 0, n);
printf("\n");
return 0;
}
Output:
Inorder Traversal: 4 2 5 1 6 3 7
Preorder Traversal: 1 2 4 5 3 6 7
Postorder Traversal: 4 5 2 6 7 3 1
Program No: 8
Program statement: Write a C program to implement the Bellman-Ford algorithm.
Source code:
#include <stdio.h>
#include <limits.h>
#define MAX_EDGES 100
#define MAX_VERTICES 100
int main()
{
int V, E;
printf("Enter the number of vertices: ");
scanf("%d", &V);
printf("Enter the number of edges: ");
scanf("%d", &E);
int graph[MAX_EDGES][3];
int src;
printf("Enter the source vertex: ");
scanf("%d", &src);
BellmanFord(graph, V, E, src);
return 0;
}
Output:
Program No: 9
Program statement: Write a c program to perform Singly Linked list operations
a) Insertion at the beginning
b) Deletion at the beginning
c) Insertion at the End
d) Deletion at the end
e) Traversing
Source code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
}
(*size)--; // Decrease size
}
int main() {
int list[MAX];
int size = 0;
int choice, data;
while (1) {
printf("\n1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete from Beginning\n");
printf("4. Delete from End\n");
printf("5. Print List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
insertAtBeginning(list, &size, data);
break;
case 2:
printf("Enter data to insert at the end: ");
scanf("%d", &data);
insertAtEnd(list, &size, data);
break;
case 3:
deleteFromBeginning(list, &size);
break;
case 4:
deleteFromEnd(list, &size);
break;
case 5:
printList(list, size);
break;
case 6:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Output:
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 5
NULL
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 5
10 -> NULL
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 1
Enter data to insert at the beginning: 20
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 5
20 -> 10 -> NULL
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 2
Enter data to insert at the end: 30
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 5
20 -> 10 -> 30 -> NULL
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 3
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 5
10 -> 30 -> NULL
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 4
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 5
10 -> NULL
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 3
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 3
List is empty. Nothing to delete from the beginning.
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 4
List is empty. Nothing to delete from the end.
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 5
NULL
1. Insert at Beginning
2. Insert at End
3. Delete from Beginning
4. Delete from End
5. Print List
6. Exit
Enter your choice: 6