Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. QUEUE USING LINKED LISTS
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- int data;
- struct node* next;
- };
- struct node* front = NULL;
- struct node* rear = NULL;
- void enqueue(int x) {
- struct node* temp = (struct node*)malloc(sizeof(struct node));
- temp->data = x;
- temp->next = NULL;
- if (front == NULL && rear == NULL) {
- front = rear = temp;
- return;
- }
- rear->next = temp;
- rear = temp;
- }
- void dequeue() {
- struct node* temp = front;
- if (front == NULL) {
- printf("Queue is empty\n");
- return;
- }
- if (front == rear) {
- front = rear = NULL;
- }
- else {
- front = front->next;
- }
- printf("Dequeued element: %d\n", temp->data);
- free(temp);
- }
- void display() {
- struct node* temp = front;
- if (front == NULL) {
- printf("Queue is empty\n");
- return;
- }
- while (temp != NULL) {
- printf("%d ", temp->data);
- temp = temp->next;
- }
- printf("\n");
- }
- int main() {
- int choice, x;
- do {
- printf("1. Enqueue\n");
- printf("2. Dequeue\n");
- printf("3. Display\n");
- printf("4. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("Enter the element to enqueue: ");
- scanf("%d", &x);
- enqueue(x);
- break;
- case 2:
- dequeue();
- break;
- case 3:
- display();
- break;
- case 4:
- exit(0);
- default:
- printf("Invalid choice\n");
- }
- } while (choice != 4);
- return 0;
- }
- --------------------------------------------------------------------------------------------------------
- 2. BINARY SEARCH TREE
- #include <stdio.h>
- #include <stdlib.h>
- // Binary Search Tree node structure
- struct node {
- int data;
- struct node* left;
- struct node* right;
- };
- // Function prototypes
- void insert(struct node** root, int data);
- void inorder(struct node* root);
- void preorder(struct node* root);
- void postorder(struct node* root);
- // Main function
- int main() {
- struct node* root = NULL;
- int choice, data;
- do {
- printf("\nBinary Search Tree Operations\n");
- printf("----------------------------\n");
- printf("1. Insert\n");
- printf("2. Inorder Traversal\n");
- printf("3. Preorder Traversal\n");
- printf("4. Postorder Traversal\n");
- printf("5. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch(choice) {
- case 1:
- printf("Enter the data to insert: ");
- scanf("%d", &data);
- insert(&root, data);
- break;
- case 2:
- printf("Inorder Traversal: ");
- inorder(root);
- printf("\n");
- break;
- case 3:
- printf("Preorder Traversal: ");
- preorder(root);
- printf("\n");
- break;
- case 4:
- printf("Postorder Traversal: ");
- postorder(root);
- printf("\n");
- break;
- case 5:
- printf("Exiting...\n");
- break;
- default:
- printf("Invalid choice! Try again.\n");
- break;
- }
- } while(choice != 5);
- return 0;
- }
- // Function to insert data into the binary search tree
- void insert(struct node** root, int data) {
- struct node* newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = data;
- newNode->left = NULL;
- newNode->right = NULL;
- if (*root == NULL) {
- *root = newNode;
- } else {
- struct node* curr = *root;
- while (1) {
- if (data < curr->data) {
- if (curr->left == NULL) {
- curr->left = newNode;
- break;
- } else {
- curr = curr->left;
- }
- } else {
- if (curr->right == NULL) {
- curr->right = newNode;
- break;
- } else {
- curr = curr->right;
- }
- }
- }
- }
- }
- // Function to perform inorder traversal of the binary search tree
- void inorder(struct node* root) {
- if (root != NULL) {
- inorder(root->left);
- printf("%d ", root->data);
- inorder(root->right);
- }
- }
- // Function to perform preorder traversal of the binary search tree
- void preorder(struct node* root) {
- if (root != NULL) {
- printf("%d ", root->data);
- preorder(root->left);
- preorder(root->right);
- }
- }
- // Function to perform postorder traversal of the binary search tree
- void postorder(struct node* root) {
- if (root != NULL) {
- postorder(root->left);
- postorder(root->right);
- printf("%d ", root->data);
- }
- }
- --------------------------------------------------------------------------------------------------------
- 3. QUEUE IMPLEMENTATION USING STACKS & LL
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define MAX_SIZE 100
- // Node struct for linked list implementation
- typedef struct Node {
- int data;
- struct Node* next;
- } Node;
- // Queue struct for both array and linked list implementations
- typedef struct Queue {
- int front;
- int rear;
- int size;
- int arr[MAX_SIZE];
- Node* head;
- } Queue;
- // Initialize the queue
- void init(Queue* q) {
- q->front = -1;
- q->rear = -1;
- q->size = 0;
- q->head = NULL;
- }
- // Check if the queue is empty
- bool is_empty(Queue* q) {
- return (q->front == -1 && q->rear == -1 && q->head == NULL);
- }
- // Check if the queue is full
- bool is_full(Queue* q) {
- return (q->rear == MAX_SIZE - 1);
- }
- // Enqueue an element into the queue (array implementation)
- void enqueue_array(Queue* q, int data) {
- if (is_full(q)) {
- printf("Queue overflow! Cannot enqueue element.\n");
- return;
- }
- if (is_empty(q)) {
- q->front = 0;
- q->rear = 0;
- } else {
- q->rear++;
- }
- q->arr[q->rear] = data;
- q->size++;
- printf("Enqueued element: %d\n", data);
- }
- // Dequeue an element from the queue (array implementation)
- void dequeue_array(Queue* q) {
- if (is_empty(q)) {
- printf("Queue underflow! Cannot dequeue element.\n");
- return;
- }
- int data = q->arr[q->front];
- q->front++;
- q->size--;
- printf("Dequeued element: %d\n", data);
- if (q->front > q->rear) {
- q->front = -1;
- q->rear = -1;
- }
- }
- // Display the contents of the queue (array implementation)
- void display_array(Queue* q) {
- if (is_empty(q)) {
- printf("Queue is empty!\n");
- return;
- }
- printf("Queue contents: ");
- for (int i = q->front; i <= q->rear; i++) {
- printf("%d ", q->arr[i]);
- }
- printf("\n");
- }
- // Enqueue an element into the queue (linked list implementation)
- void enqueue_linked_list(Queue* q, int data) {
- Node* new_node = (Node*)malloc(sizeof(Node));
- new_node->data = data;
- new_node->next = NULL;
- if (is_empty(q)) {
- q->head = new_node;
- q->front = 0;
- q->rear = 0;
- } else {
- q->rear++;
- Node* curr = q->head;
- while (curr->next != NULL) {
- curr = curr->next;
- }
- curr->next = new_node;
- }
- q->size++;
- printf("Enqueued element: %d\n", data);
- }
- // Dequeue an element from the queue (linked list implementation)
- void dequeue_linked_list(Queue* q) {
- if (is_empty(q)) {
- printf("Queue underflow! Cannot dequeue element.\n");
- return;
- }
- int data = q->head->data;
- Node* temp = q->head;
- q->head = q->head->next;
- free(temp);
- q->size--;
- printf("Dequeued element: %d\n", data);
- if (q->size == 0) {
- q->front = -1;
- q->rear = -1;
- }
- }
- // Display the contents of the queue (linked list implementation)
- void display_linked_list(Queue* q) {
- if (is_empty(q)) {
- printf("Queue is empty!\n");
- return;
- }
- printf("Queue contents: ");
- Node* curr = q->head;
- while (curr != NULL) {
- printf("%d ", curr->data);
- curr = curr->next;
- }
- printf("\n");
- }
- int main() {
- Queue q;
- init(&q);
- int choice, data;
- do {
- printf("\nQueue Operations:\n");
- printf("1. Enqueue (Array Implementation)\n");
- printf("2. Dequeue (Array Implementation)\n");
- printf("3. Display (Array Implementation)\n");
- printf("4. Enqueue (Linked List Implementation)\n");
- printf("5. Dequeue (Linked List Implementation)\n");
- printf("6. Display (Linked List Implementation)\n");
- printf("7. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("Enter element to enqueue: ");
- scanf("%d", &data);
- enqueue_array(&q, data);
- break;
- case 2:
- dequeue_array(&q);
- break;
- case 3:
- display_array(&q);
- break;
- case 4:
- printf("Enter element to enqueue: ");
- scanf("%d", &data);
- enqueue_linked_list(&q, data);
- break;
- case 5:
- dequeue_linked_list(&q);
- break;
- case 6:
- display_linked_list(&q);
- break;
- case 7:
- printf("Exiting...\n");
- exit(0);
- default:
- printf("Invalid choice!\n");
- break;
- }
- } while (choice != 7);
- return 0;
- }
- --------------------------------------------------------------------------------------------------------
- 4. QUEUE W/ LL loop
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- int data;
- struct node* next;
- };
- struct node* front = NULL;
- struct node* rear = NULL;
- void enqueue(int x) {
- struct node* temp = (struct node*)malloc(sizeof(struct node));
- temp->data = x;
- temp->next = NULL;
- if (front == NULL && rear == NULL) {
- front = rear = temp;
- return;
- }
- rear->next = temp;
- rear = temp;
- }
- void dequeue() {
- struct node* temp = front;
- if (front == NULL) {
- printf("Queue is empty\n");
- return;
- }
- if (front == rear) {
- front = rear = NULL;
- }
- else {
- front = front->next;
- }
- printf("Dequeued element: %d\n", temp->data);
- free(temp);
- }
- void display() {
- struct node* temp = front;
- if (front == NULL) {
- printf("Queue is empty\n");
- return;
- }
- while (temp != NULL) {
- printf("%d ", temp->data);
- temp = temp->next;
- }
- printf("\n");
- }
- int main() {
- int n, x, i;
- printf("Enter the number of items to be entered: ");
- scanf("%d", &n);
- for (i = 0; i < n; i++) {
- printf("Enter the element to enqueue: ");
- scanf("%d", &x);
- enqueue(x);
- }
- printf("Queue: ");
- display();
- while (1) {
- int choice;
- printf("1. Enqueue\n");
- printf("2. Dequeue\n");
- printf("3. Display\n");
- printf("4. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("Enter the element to enqueue: ");
- scanf("%d", &x);
- enqueue(x);
- printf("Queue: ");
- display();
- break;
- case 2:
- dequeue();
- printf("Queue: ");
- display();
- break;
- case 3:
- printf("Queue: ");
- display();
- break;
- case 4:
- exit(0);
- default:
- printf("Invalid choice\n");
- }
- }
- return 0;
- }
- ------------------------------------------------------------------------------------------------------------------
- 5. BST W/ loop
- #include <stdio.h>
- #include <stdlib.h>
- // Binary Search Tree node structure
- struct node {
- int data;
- struct node* left;
- struct node* right;
- };
- // Function prototypes
- void insert(struct node** root, int data);
- void inorder(struct node* root);
- void preorder(struct node* root);
- void postorder(struct node* root);
- // Main function
- int main() {
- struct node* root = NULL;
- int choice, data, n;
- printf("Enter the number of terms to be entered: ");
- scanf("%d", &n);
- for (int i = 0; i < n; i++) {
- printf("Enter term %d: ", i + 1);
- scanf("%d", &data);
- insert(&root, data);
- }
- do {
- printf("\nBinary Search Tree Operations\n");
- printf("----------------------------\n");
- printf("1. Inorder Traversal\n");
- printf("2. Preorder Traversal\n");
- printf("3. Postorder Traversal\n");
- printf("4. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch(choice) {
- case 1:
- printf("Inorder Traversal: ");
- inorder(root);
- printf("\n");
- break;
- case 2:
- printf("Preorder Traversal: ");
- preorder(root);
- printf("\n");
- break;
- case 3:
- printf("Postorder Traversal: ");
- postorder(root);
- printf("\n");
- break;
- case 4:
- printf("Exiting...\n");
- break;
- default:
- printf("Invalid choice! Try again.\n");
- break;
- }
- } while(choice != 4);
- return 0;
- }
- // Function to insert data into the binary search tree
- void insert(struct node** root, int data) {
- struct node* newNode = (struct node*)malloc(sizeof(struct node));
- newNode->data = data;
- newNode->left = NULL;
- newNode->right = NULL;
- if (*root == NULL) {
- *root = newNode;
- } else {
- struct node* curr = *root;
- while (1) {
- if (data < curr->data) {
- if (curr->left == NULL) {
- curr->left = newNode;
- break;
- } else {
- curr = curr->left;
- }
- } else {
- if (curr->right == NULL) {
- curr->right = newNode;
- break;
- } else {
- curr = curr->right;
- }
- }
- }
- }
- }
- // Function to perform inorder traversal of the binary search tree
- void inorder(struct node* root) {
- if (root != NULL) {
- inorder(root->left);
- printf("%d ", root->data);
- inorder(root->right);
- }
- }
- // Function to perform preorder traversal of the binary search tree
- void preorder(struct node* root) {
- if (root != NULL) {
- printf("%d ", root->data);
- preorder(root->left);
- preorder(root->right);
- }
- }
- // Function to perform postorder traversal of the binary search tree
- void postorder(struct node* root) {
- if (root != NULL) {
- postorder(root->left);
- postorder(root->right);
- printf("%d ", root->data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement