Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

Implementation of Stack

Uploaded by

sauravsubedi1128
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Implementation of Stack

Uploaded by

sauravsubedi1128
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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;

// Input the size of the stack


printf("\n Enter the size of STACK [MAX=100]: ");
scanf("%d", &n);

// Display menu for stack operations


printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do {
printf("\n Enter the Choice: ");
scanf("%d", &choice);

// Perform operations based on user choice using switch statement


switch (choice) {
case 1:
push(); // Call push function when choice is 1
break;
case 2:
pop(); // Call pop function when choice is 2
break;
case 3:
display(); // Call display function when choice is 3
break;
case 4:
printf("\n\t EXIT POINT ");
break;
default:
printf("\n\t Please Enter a Valid Choice (1/2/3/4)");
}
} while (choice != 4); // Repeat until user chooses to exit (choice 4)

return 0;
}

// Function to push an element onto the stack


void push() {
if (top >= n - 1) {
printf("\n\tSTACK is overflow");
} else {
printf(" Enter a value to be pushed: ");
scanf("%d", &x);
top++;
stack[top] = x;
}
}

// Function to pop an element from the stack


void pop() {
if (top <= -1) {
printf("\n\t Stack is underflow");
} else {
printf("\n\t The popped element is %d", stack[top]);
top--;
}
}

// Function to display the elements of the stack


void display() {
if (top >= 0) {
printf("\n The elements in STACK \n");
for (i = top; i >= 0; i--)
printf("\n%d", stack[i]);
printf("\n Press Next Choice");
} else {
printf("\n The STACK is empty");
}
}
Merge Sort
#include <stdio.h>

void mergesort(int a[], int i, int j);


void merge(int a[], int i1, int j1, int i2, int j2);

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 mergesort(int a[], int i, int j) {


int mid;
if(i < j) {
mid = (i + j) / 2; // Calculate the middle index
mergesort(a, i, mid); // Recursive call for left half
mergesort(a, mid + 1, j); // Recursive call for right half
merge(a, i, mid, mid + 1, j); // Merge the sorted halves
}
}

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>

typedef struct BST {


int data;
struct BST *lchild, *rchild;
} node;

void insert(node *, node *);


void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);

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;
}

void insert(node *root, node *new_node) {


if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data) {
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}

node *search(node *root, int key, node **parent) {


node *temp;
temp = root;
while (temp != NULL) {
if (temp->data == key) {
printf("\nThe %d Element is Present", temp->data);
return temp;
}
*parent = temp;
if (temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}

void inorder(node *temp) {


if (temp != NULL) {
inorder(temp->lchild);
printf("%d ", temp->data);
inorder(temp->rchild);
}
}

void preorder(node *temp) {


if (temp != NULL) {
printf("%d ", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}

void postorder(node *temp) {


if (temp != NULL) {
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d ", temp->data);
}}
Recursion

1. Checking Prime Numbers Using Recursion


#include <stdio.h>

int checkPrime(int num, int i) {


if (i != 1) {
if (num % i != 0) {
return checkPrime(num, i - 1);
} else {
return 0;
}
} else {
return 1;
}
}

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 Reverse(int n, int sum) {


if (n == 0)
return sum;
return Reverse(n / 10, sum * 10 + n % 10);
}

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>

void TOH(int n, char x, char y, char z) {


if (n > 0) {
TOH(n - 1, x, z, y);
printf("\n%c -> %c", x, y);
TOH(n - 1, z, y, x);
}
}

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;
};

struct node *HEAD = NULL;

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;
}

You might also like