Implementation of Stack
Implementation of Stack
#include <stdio.h>
// Global variables
int stack[100], choice, n, top, x, i;
// Function prototypes
void push(void);
void pop(void);
void display(void);
int main() {
// Initialize stack top index
top = -1;
do {
printf("\n Enter the Choice: ");
scanf("%d", &choice);
return 0;
}
int main() {
int a[30], n, i;
printf("Enter no of elements:");
scanf("%d", &n);
printf("Enter array elements:");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
mergesort(a, 0, n-1); // Call mergesort function to sort the array
printf("\nSorted array is :");
for(i = 0; i < n; i++)
printf("%d ", a[i]); // Print sorted array
return 0;
}
void merge(int a[], int i1, int j1, int i2, int j2) {
int temp[50]; // Temporary array for merging
int i, j, k;
i = i1; // Initialize i with the starting index of the first list
j = i2; // Initialize j with the starting index of the second list
k = 0; // Initialize k for the temporary array index
// Merge elements from both lists in sorted order
while(i <= j1 && j <= j2) {
if(a[i] < a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
// Copy remaining elements of the first list
while(i <= j1)
temp[k++] = a[i++];
// Copy remaining elements of the second list
while(j <= j2)
temp[k++] = a[j++];
// Transfer elements from temp[] back to a[]
for(i = i1, j = 0; i <= j2; i++, j++)
a[i] = temp[j];
}
Bubble Sort
#include <stdio.h>
int main() {
int a[10], i, j, temp, n;
printf("\n Enter the max no.of Elements to Sort: \n");
scanf("%d", &n);
printf("\n Enter the Elements : \n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(i = 0; i < n; i++)
for(j = i + 1; j < n; j++) {
if(a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
printf("\nSorted array is: ");
for(i = 0; i < n; i++) {
printf("%d\t", a[i]);
}
return 0;
}
Implementation of Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
int main() {
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
printf("\nProgram For Binary Search Tree ");
do {
printf("\n1. Create");
printf("\n2. Search");
printf("\n3. Recursive Traversals");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
do {
new_node = get_node();
printf("\nEnter The Element: ");
scanf("%d", &new_node->data);
if (root == NULL) // Tree is not created
root = new_node;
else
insert(root, new_node);
printf("\nWant To enter More Elements?(y/n): ");
scanf(" %c", &ans);
} while (ans == 'y');
break;
case 2:
printf("\nEnter Element to be searched: ");
scanf("%d", &key);
tmp = search(root, key, &parent);
if (tmp != NULL)
printf("\nParent of node %d is %d", tmp->data, (parent != NULL ? parent->data :
-1));
else
printf("\nElement not found");
break;
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf("\nThe Inorder display: ");
inorder(root);
printf("\nThe Preorder display: ");
preorder(root);
printf("\nThe Postorder display: ");
postorder(root);
}
break;
}
} while (choice != 4);
}
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
int main() {
int num = 0;
printf("Enter a number: ");
scanf("%d", &num);
if (checkPrime(num, num / 2) == 1)
printf("Given number is a prime number\n");
else
printf("Given number is not a prime number\n");
return 0;
}
2. Reversing an Integer Using Recursion
#include <stdio.h>
int main() {
int number, reverse;
printf("Enter a positive integer: ");
scanf("%d", &number);
reverse = Reverse(number, 0);
printf("The reverse of %d is: %d\n", number, reverse);
return 0;
}
3. Finding Factorial Using Recursion
#include <stdio.h>
int fact(int n) {
if (n == 0)
return 1;
return n * fact(n - 1);
}
int main() {
int n;
printf("Enter the Number to Find Factorial: ");
scanf("%d", &n);
printf("Factorial of %d is %d\n", n, fact(n));
return 0;
}
4. Tower of Hanoi Implementation in C
#include <stdio.h>
int main() {
int n;
printf("How many plates? ");
scanf("%d", &n);
TOH(n, 'A', 'B', 'C');
return 0;
}
Implementation of Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void insert_at_begin() {
struct node *point;
int value;
point = (struct node *) malloc(sizeof(struct node));
if (point == NULL) {
printf("\nMemory allocation failed!");
return;
}
printf("\nEnter the value: ");
scanf("%d", &value);
point->data = value;
point->next = HEAD;
HEAD = point;
printf("\nNode inserted at the beginning!");
}
void insert_at_last() {
struct node *point, *tmp;
int value;
point = (struct node *) malloc(sizeof(struct node));
if (point == NULL) {
printf("\nMemory allocation failed!");
return;
}
printf("\nEnter the value: ");
scanf("%d", &value);
point->data = value;
point->next = NULL;
if (HEAD == NULL) {
HEAD = point;
} else {
tmp = HEAD;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = point;
}
printf("\nNode inserted at the end!");
}
void insert_random() {
int pos, value, a;
struct node *point, *tmp;
point = (struct node *) malloc(sizeof(struct node));
if (point == NULL) {
printf("\nMemory allocation failed!");
return;
}
printf("\nEnter the value of element: ");
scanf("%d", &value);
point->data = value;
printf("\nEnter the position where you want to insert: ");
scanf("%d", &pos);
tmp = HEAD;
for (a = 0; a < pos - 1; a++) {
if (tmp == NULL) {
printf("\nPosition out of range!");
free(point);
return;
}
tmp = tmp->next;
}
point->next = tmp->next;
tmp->next = point;
printf("\nNode inserted at position %d!", pos);
}
void delete_at_begin() {
struct node *point;
if (HEAD == NULL) {
printf("\nThe list is empty!");
return;
}
point = HEAD;
HEAD = point->next;
free(point);
printf("\nNode deleted from the beginning!");
}
void delete_at_last() {
struct node *point, *point1;
if (HEAD == NULL) {
printf("\nThe list is empty!");
return;
}
if (HEAD->next == NULL) {
free(HEAD);
HEAD = NULL;
printf("\nNode deleted from the end!");
return;
}
point = HEAD;
while (point->next != NULL) {
point1 = point;
point = point->next;
}
point1->next = NULL;
free(point);
printf("\nNode deleted from the end!");
}
void delete_random() {
struct node *point, *point1;
int pos, a;
printf("\nEnter the position of the node after which you want to delete: ");
scanf("%d", &pos);
point = HEAD;
for (a = 0; a < pos; a++) {
point1 = point;
point = point->next;
if (point == NULL) {
printf("\nPosition out of range!");
return;
}
}
point1->next = point->next;
free(point);
printf("\nNode deleted at position %d!", pos + 1);
}
void searching() {
struct node *point;
int value, a = 0;
point = HEAD;
if (point == NULL) {
printf("\nThe list is empty!");
return;
}
printf("\nEnter the value to search: ");
scanf("%d", &value);
while (point != NULL) {
if (point->data == value) {
printf("Value found at position %d", a + 1);
return;
}
point = point->next;
a++;
}
printf("Value not found in the list!");
}
void print() {
struct node *point;
point = HEAD;
if (point == NULL) {
printf("\nThe list is empty!");
return;
}
printf("\nList elements:");
while (point != NULL) {
printf(" %d", point->data);
point = point->next;
}
printf("\n");
}
int main() {
int your_choice = 0;
while (your_choice != 9) {
printf("\n**************************************\n");
printf("\nOperations on Singly Linked List!\n");
printf("Choose an option from the below list:\n");
printf("\n**************************************\n");
printf("1. Insertion at beginning\n");
printf("2. Insertion at last\n");
printf("3. Insertion at random location\n");
printf("4. Deletion from the beginning\n");
printf("5. Deletion from the end\n");
printf("6. Deletion of a node after a specified node\n");
printf("7. Searching for an element\n");
printf("8. Display all elements\n");
printf("9. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &your_choice);
switch (your_choice) {
case 1: insert_at_begin(); break;
case 2: insert_at_last(); break;
case 3: insert_random(); break;
case 4: delete_at_begin(); break;
case 5: delete_at_last(); break;
case 6: delete_random(); break;
case 7: searching(); break;
case 8: print(); break;
case 9: exit(0); break;
default: printf("Invalid choice!"); break;
}
}
return 0;
}