DS 5 to 15
DS 5 to 15
Output 6
Program No: 7
Write a program to implement Stack using Linked List .
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // Define the maximum size of the stack
struct stack {
int info;
struct stack *next;};
typedef struct stack stk;
stk *top = NULL;
void push();
void pull();
void display();
int main() {
int ch;
printf("Stack Menu:\n1. PUSH\n2. PULL\n3. DISPLAY\n4. EXIT\n");
do {
printf("\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
push();
break;
case 2:
pull();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid selection. Please try again.\n");}
} while (ch != 4);
return 0;}
void push() {
int val, count = 0;
stk *p;
for (p = top; p != NULL; p = p->next) {
count++;}
if (count == MAX) {
printf("\nOVERFLOW: Stack is Full\n");
} else {
stk *ptr = (stk *)malloc(sizeof(stk));
if (ptr == NULL) {
printf("\nMemory allocation failed.\n");
return;}
printf("\nEnter the value to push onto the stack: ");
scanf("%d", &val);
ptr->info = val; // Assign value to the new node
ptr->next = top; // Link the new node to the current top
top = ptr; // Update the top pointer
printf("\nPush is successful.\n");}}
void pull() {
if (top == NULL) {
printf("\nUNDERFLOW: Stack is empty.\n");
} else {
stk *p = top; // Pointer to the top node
printf("\nPopped value: %d\n", p->info);
top = top->next; // Update the top pointer
free(p); // Free the memory of the popped node
printf("\nPull is successful.\n");}}
void display() {
if (top == NULL) {
printf("\nStack is empty.\n");
} else {
printf("\nStack elements are:\n");
stk *p = top;
while (p != NULL) {
printf("%d\n", p->info); // Print the value of each node
p = p->next; }
int count = 0;
for (p = top; p != NULL; p = p->next) {
count++;}
if (count == MAX) {
printf("\nStack is Full.\n");}}}
Output 7
Program No: 8
Write a program to implement Queue using Linked List.
#include <stdio.h>
#include <stdlib.h>
struct queue {
int info;
struct queue *next;};
typedef struct queue q;
q *front = NULL, *rear = NULL; // Front and rear pointers for the queue
void insert();
void delet();
void display();
int main() {
int ch;
printf("Queue Menu:\n1. INSERT\n2. DELETE\n3. DISPLAY\n4. EXIT\n");
do {
printf("\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid selection. Please try again.\n");}
} while (ch != 4);
return 0;}
void insert() {
int val;
q *ptr = (q *)malloc(sizeof(q));
if (ptr == NULL) {
printf("\nMemory allocation failed.\n");
return;}
printf("\nEnter the value to insert into the queue: ");
scanf("%d", &val);
ptr->info = val;
ptr->next = NULL;
if (rear == NULL) {
front = rear = ptr;
} else {
rear->next = ptr;
rear = ptr;}
printf("\nInsertion is successful.\n");}
void delet() {
if (front == NULL) {
printf("\nUNDERFLOW: Queue is empty.\n");
} else {
q *p = front; // Pointer to the node to be deleted
printf("\nDeleted value: %d\n", front->info);
front = front->next;
if (front == NULL) {
rear = NULL; }
free(p); // Free the memory of the deleted node
printf("\nDeletion is successful.\n");}}
void display() {
if (front == NULL) {
printf("\nQueue is empty.\n");
} else {
printf("\nQueue elements are:\n");
q *p = front;
while (p != NULL) {
printf("%d\n", p->info); // Print each element
p = p->next;}}}
Output 8
Program No: 9
Write a program to implement Bubble Sort.
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;}}}}
int main() {
int arr[50], i, n;
printf("\nEnter the number of array elements: ");
scanf("%d", &n);
if (n <= 0 || n > 50) {
printf("Invalid input. Number of elements should be between 1 and 50.\n");
return 1;}
printf("Enter the array elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);}
bubbleSort(arr, n);
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);}
printf("\n");
return 0;}
Output9
Program No: 10
Write a program to implement Selection Sort.
#include <stdio.h>
#define MAX 10
int main() {
int i,j,data[MAX],n,t;
printf("\n Enter the number of array elements");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&data[i]);
for(i=0;i<n-1;i++) {
for(j=i+1;j<n;j++) {
if(data[i]>data[j]) {
t=data[i];
data[i]=data[j];
data[j]=t; } } }
printf("\n Sorted Array");
for(i=0;i<n;i++)
printf("\n %d ",data[i]);
return 0; }
Output 10
Program No: 11
Write a program to implement Insertion Sort.
#include <stdio.h>
int main() {
int arr[20];
int i, key, j,n;
printf("\n Enter the number of array elements");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
printf("Sorted array: \n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output 11
Program No: 12
Write a program to implement Quick Sort.
#include <stdio.h>
int partition(int arr[], int low, int high) {
int pivot = arr[low]; // Choosing the first element as pivot
int t;
int left = low + 1;
int right = high;
while (left <= right) {
while (left <= right && arr[left] <= pivot) {
left++;}
while (left <= right && arr[right] > pivot) {
right--;}
if (left < right) {
t = arr[left];
arr[left] = arr[right];
arr[right] = t;}}
t = arr[low];
arr[low] = arr[right];
arr[right] = t;
return right;}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1); // Recursively sort left subarray
quickSort(arr, pivotIndex + 1, high); // Recursively sort right subarray}}
int main() {
int arr[50], i, n;
printf("\nEnter the number of array elements: ");
scanf("%d", &n);
if (n <= 0 || n > 50) {
printf("Invalid input. Number of elements should be between 1 and 50.\n");
return 1;}
printf("Enter the array elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);}
quickSort(arr, 0, n - 1);
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);}
printf("\n");
return 0;
}
Output 12
Program No: 13
Write a program to implement Merge Sort.
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1; // Size of the left subarray
int n2 = r - m; // Size of the right subarray
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;}
k++;}
while (i < n1) {
arr[k] = L[i];
i++;
k++;}
while (j < n2) {
arr[k] = R[j];
j++;
k++;}}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);}}
void printArray(int A[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");}
int main() {
int arr[50], n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
if (n <= 0 || n > 50) {
printf("Invalid input. The number of elements must be between 1 and 50.\n");
return 1;}
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);}
printf("Given array is:\n");
printArray(arr, n);
mergeSort(arr, 0, n - 1);
printf("\nSorted array is:\n");
printArray(arr, n);
return 0;
}
Output 13
Program No: 14
Write a program to implement Inorder, Preorder and Postorder traversal on a binary tree.
#include <stdio.h>
#include <stdlib.h>
struct node {
int element;
struct node* left;
struct node* right;};
struct node* createNode(int val) {
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->element = val;
Node->left = NULL;
Node->right = NULL;
return Node;}
void traversePreorder(struct node* root) {
if (root == NULL)
return;
printf(" %d ", root->element);
traversePreorder(root->left);
traversePreorder(root->right);}
void traverseInorder(struct node* root) {
if (root == NULL)
return;
traverseInorder(root->left);
printf(" %d ", root->element);
traverseInorder(root->right);}
void traversePostorder(struct node* root) {
if (root == NULL)
return;
traversePostorder(root->left);
traversePostorder(root->right);
printf(" %d ", root->element);}
int main() {
struct node* root = createNode(36);
root->left = createNode(26);
root->right = createNode(46);
root->left->left = createNode(21);
root->left->right = createNode(31);
root->left->left->left = createNode(11);
root->left->left->right = createNode(24);
root->right->left = createNode(41);
root->right->right = createNode(56);
root->right->right->left = createNode(51);
root->right->right->right = createNode(66);
printf("\nThe Preorder traversal of the given binary tree is:\n");
traversePreorder(root);
printf("\n\nThe Inorder traversal of the given binary tree is:\n");
traverseInorder(root);
printf("\n\nThe Postorder traversal of the given binary tree is:\n");
traversePostorder(root);
return 0;
}
Output 14