1.//Valid Parentheses============================================================================================================================= #include #include char stack[100]; int top = -1; void push(char ch) { stack[++top] = ch; } char pop() { return stack[top--]; } int isEmpty() { return top == -1; } int isValid(char s[]) { for (int i = 0; i < strlen(s); i++) { char ch = s[i]; if (ch == '(' || ch == '{' || ch == '[') push(ch); else { if (isEmpty()) return 0; char topChar = pop(); if ((ch == ')' && topChar != '(') || (ch == '}' && topChar != '{') || (ch == ']' && topChar != '[')) return 0; } } return isEmpty(); } int main() { char s[100]; printf("Enter string: "); scanf("%s", s); if (isValid(s)) printf("Valid Parentheses"); else printf("Invalid Parentheses"); return 0; } ---------------------------------------- 2. //Binary Search=========================================================================================================================================== #include int binarySearch(int arr[], int n, int key) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == key) return mid; else if (arr[mid] < key) low = mid + 1; else high = mid - 1; } return -1; } int main() { int n, key; printf("Enter number of elements: "); scanf("%d", &n); int arr[n]; printf("Enter sorted elements:\n"); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Enter element to search: "); scanf("%d", &key); int result = binarySearch(arr, n, key); if (result != -1) printf("Found at index: %d", result); else printf("Not found"); return 0; } ---------------------------------------- 3. Bubble Sort =========================================================================================================================================== #include int main() { int n; printf("Enter number of elements: "); scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); for (int i = 0; i < n - 1; i++) for (int j = 0; j < n - i - 1; j++) if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } printf("Sorted array:\n"); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } ---------------------------------------- 4. Insertion Sort =========================================================================================================================================== #include int main() { int n; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); for (int i = 1; i < n; i++) { int key = arr[i], j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } ---------------------------------------- 5. Selection Sort =========================================================================================================================================== #include int main() { int n; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); for (int i = 0; i < n - 1; i++) { int min = i; for (int j = i + 1; j < n; j++) if (arr[j] < arr[min]) min = j; int temp = arr[i]; arr[i] = arr[min]; arr[min] = temp; } for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } ---------------------------------------- 6. Merge Sort =========================================================================================================================================== #include void merge(int arr[], int l, int m, int r) { int i = l, j = m + 1, k = 0; int temp[100]; while (i <= m && j <= r) temp[k++] = (arr[i] < arr[j]) ? arr[i++] : arr[j++]; while (i <= m) temp[k++] = arr[i++]; while (j <= r) temp[k++] = arr[j++]; for (int x = 0; x < k; x++) arr[l + x] = temp[x]; } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } } int main() { int n; scanf("%d", &n); int arr[100]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); mergeSort(arr, 0, n - 1); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } ---------------------------------------- 7. Singly Linked List =========================================================================================================================================== #include #include struct Node { int data; struct Node* next; }; struct Node* head = NULL; // Insert at end void insert() { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); printf("Enter value: "); scanf("%d", &newNode->data); newNode->next = NULL; if (head == NULL) { head = newNode; return; } struct Node* temp = head; while (temp->next != NULL) temp = temp->next; temp->next = newNode; } // Delete node void deleteNode() { int key; printf("Enter value to delete: "); scanf("%d", &key); struct Node *temp = head, *prev = NULL; if (temp != NULL && temp->data == key) { head = temp->next; free(temp); return; } while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) { printf("Not found\n"); return; } prev->next = temp->next; free(temp); } // Display void display() { struct Node* temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { int choice; while (1) { printf("\n1.Insert 2.Delete 3.Display 4.Exit\n"); scanf("%d", &choice); switch (choice) { case 1: insert(); break; case 2: deleteNode(); break; case 3: display(); break; case 4: exit(0); default: printf("Invalid choice\n"); } } } 8.doubly linked list =========================================================================================================================================== #include #include struct Node { int data; struct Node* prev; struct Node* next; }; struct Node* head = NULL; // Insert at end void insert() { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); printf("Enter value: "); scanf("%d", &newNode->data); newNode->next = newNode->prev = NULL; if (head == NULL) { head = newNode; return; } struct Node* temp = head; while (temp->next != NULL) temp = temp->next; temp->next = newNode; newNode->prev = temp; } // Delete node void deleteNode() { int key; printf("Enter value to delete: "); scanf("%d", &key); struct Node* temp = head; while (temp != NULL && temp->data != key) temp = temp->next; if (temp == NULL) { printf("Not found\n"); return; } if (temp->prev != NULL) temp->prev->next = temp->next; else head = temp->next; if (temp->next != NULL) temp->next->prev = temp->prev; free(temp); } // Display void display() { struct Node* temp = head; while (temp != NULL) { printf("%d <-> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { int choice; while (1) { printf("\n1.Insert 2.Delete 3.Display 4.Exit\n"); scanf("%d", &choice); switch (choice) { case 1: insert(); break; case 2: deleteNode(); break; case 3: display(); break; case 4: exit(0); default: printf("Invalid choice\n"); } } } 9.circular link list =========================================================================================================================================== #include #include struct Node { int data; struct Node* next; }; struct Node* head = NULL; // Insert at end void insert() { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); printf("Enter value: "); scanf("%d", &newNode->data); if (head == NULL) { head = newNode; newNode->next = head; return; } struct Node* temp = head; while (temp->next != head) temp = temp->next; temp->next = newNode; newNode->next = head; } // Delete node void deleteNode() { int key; printf("Enter value to delete: "); scanf("%d", &key); struct Node *temp = head, *prev = NULL; if (head == NULL) return; // If head node if (temp->data == key) { while (temp->next != head) temp = temp->next; if (temp == head) { free(head); head = NULL; return; } temp->next = head->next; free(head); head = temp->next; return; } temp = head; do { prev = temp; temp = temp->next; if (temp->data == key) { prev->next = temp->next; free(temp); return; } } while (temp != head); printf("Not found\n"); } // Display void display() { if (head == NULL) return; struct Node* temp = head; do { printf("%d -> ", temp->data); temp = temp->next; } while (temp != head); printf("(back to head)\n"); } int main() { int choice; while (1) { printf("\n1.Insert 2.Delete 3.Display 4.Exit\n"); scanf("%d", &choice); switch (choice) { case 1: insert(); break; case 2: deleteNode(); break; case 3: display(); break; case 4: exit(0); default: printf("Invalid choice\n"); } } } 10. insert node and delete node in binary search tree =========================================================================================================================================== #include #include // Node structure struct Node { int data; struct Node* left; struct Node* right; }; // Create new node struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->left = newNode->right = NULL; return newNode; } // Insert node struct Node* insert(struct Node* root, int value) { if (root == NULL) return createNode(value); if (value < root->data) root->left = insert(root->left, value); else if (value > root->data) root->right = insert(root->right, value); return root; } // Find minimum node struct Node* findMin(struct Node* root) { while (root->left != NULL) root = root->left; return root; } // Delete node struct Node* deleteNode(struct Node* root, int key) { if (root == NULL) return NULL; if (key < root->data) root->left = deleteNode(root->left, key); else if (key > root->data) root->right = deleteNode(root->right, key); else { // Case 1: No child if (root->left == NULL && root->right == NULL) { free(root); return NULL; } // Case 2: One child else if (root->left == NULL) { struct Node* temp = root->right; free(root); return temp; } else if (root->right == NULL) { struct Node* temp = root->left; free(root); return temp; } // Case 3: Two children struct Node* temp = findMin(root->right); root->data = temp->data; root->right = deleteNode(root->right, temp->data); } return root; } // Inorder traversal void inorder(struct Node* root) { if (root != NULL) { inorder(root->left); printf("%d ", root->data); inorder(root->right); } } // Main function int main() { struct Node* root = NULL; int choice, value; while (1) { printf("\n1.Insert 2.Delete 3.Display 4.Exit\n"); scanf("%d", &choice); switch (choice) { case 1: printf("Enter value: "); scanf("%d", &value); root = insert(root, value); break; case 2: printf("Enter value to delete: "); scanf("%d", &value); root = deleteNode(root, value); break; case 3: printf("Inorder: "); inorder(root); printf("\n"); break; case 4: exit(0); default: printf("Invalid choice\n"); } } } 11.queue using array =========================================================================================================================================== #include #define MAX 5 int queue[MAX]; int front = -1, rear = -1; void enqueue() { int val; if (rear == MAX - 1) { printf("Queue Overflow\n"); return; } printf("Enter value: "); scanf("%d", &val); if (front == -1) front = 0; queue[++rear] = val; } void dequeue() { if (front == -1 || front > rear) { printf("Queue Underflow\n"); return; } printf("Deleted: %d\n", queue[front++]); } void display() { if (front == -1 || front > rear) { printf("Queue is empty\n"); return; } for (int i = front; i <= rear; i++) printf("%d ", queue[i]); printf("\n"); } int main() { int ch; while (1) { printf("\n1.Enqueue 2.Dequeue 3.Display 4.Exit\n"); scanf("%d", &ch); switch (ch) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; default: printf("Invalid\n"); } } } 12.queue using linked list =========================================================================================================================================== #include #include struct Node { int data; struct Node* next; }; struct Node *front = NULL, *rear = NULL; void enqueue() { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); printf("Enter value: "); scanf("%d", &newNode->data); newNode->next = NULL; if (rear == NULL) { front = rear = newNode; } else { rear->next = newNode; rear = newNode; } } void dequeue() { if (front == NULL) { printf("Queue Underflow\n"); return; } struct Node* temp = front; printf("Deleted: %d\n", temp->data); front = front->next; if (front == NULL) rear = NULL; free(temp); } void display() { struct Node* temp = front; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { int ch; while (1) { printf("\n1.Enqueue 2.Dequeue 3.Display 4.Exit\n"); scanf("%d", &ch); switch (ch) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; } } } 13.Circular Queue using Array =========================================================================================================================================== #include #define MAX 5 int queue[MAX]; int front = -1, rear = -1; void enqueue() { int val; if ((rear + 1) % MAX == front) { printf("Queue Full\n"); return; } printf("Enter value: "); scanf("%d", &val); if (front == -1) front = 0; rear = (rear + 1) % MAX; queue[rear] = val; } void dequeue() { if (front == -1) { printf("Queue Empty\n"); return; } printf("Deleted: %d\n", queue[front]); if (front == rear) { front = rear = -1; } else { front = (front + 1) % MAX; } } void display() { if (front == -1) { printf("Empty\n"); return; } int i = front; while (1) { printf("%d ", queue[i]); if (i == rear) break; i = (i + 1) % MAX; } printf("\n"); } int main() { int ch; while (1) { printf("\n1.Enqueue 2.Dequeue 3.Display 4.Exit\n"); scanf("%d", &ch); switch (ch) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; } } } 14.Circular Queue using Linked List =========================================================================================================================================== #include #include struct Node { int data; struct Node* next; }; struct Node *rear = NULL; void enqueue() { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); printf("Enter value: "); scanf("%d", &newNode->data); if (rear == NULL) { rear = newNode; rear->next = rear; } else { newNode->next = rear->next; rear->next = newNode; rear = newNode; } } void dequeue() { if (rear == NULL) { printf("Queue Empty\n"); return; } struct Node* temp = rear->next; if (rear == temp) { printf("Deleted: %d\n", temp->data); rear = NULL; } else { printf("Deleted: %d\n", temp->data); rear->next = temp->next; } free(temp); } void display() { if (rear == NULL) { printf("Empty\n"); return; } struct Node* temp = rear->next; do { printf("%d ", temp->data); temp = temp->next; } while (temp != rear->next); printf("\n"); } int main() { int ch; while (1) { printf("\n1.Enqueue 2.Dequeue 3.Display 4.Exit\n"); scanf("%d", &ch); switch (ch) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; } } } 15.Priority Queue using Array =========================================================================================================================================== #include #define MAX 5 int pq[MAX], priority[MAX]; int size = 0; // Insert element with priority void enqueue() { if (size == MAX) { printf("Queue Overflow\n"); return; } int value, pr; printf("Enter value: "); scanf("%d", &value); printf("Enter priority (lower number = higher priority): "); scanf("%d", &pr); int i = size - 1; // Shift elements based on priority while (i >= 0 && pr < priority[i]) { pq[i + 1] = pq[i]; priority[i + 1] = priority[i]; i--; } pq[i + 1] = value; priority[i + 1] = pr; size++; } // Remove highest priority element void dequeue() { if (size == 0) { printf("Queue Underflow\n"); return; } printf("Deleted element: %d\n", pq[0]); // Shift all elements left for (int i = 1; i < size; i++) { pq[i - 1] = pq[i]; priority[i - 1] = priority[i]; } size--; } // Display queue void display() { if (size == 0) { printf("Queue is empty\n"); return; } printf("Element\tPriority\n"); for (int i = 0; i < size; i++) { printf("%d\t%d\n", pq[i], priority[i]); } } // Main function int main() { int choice; while (1) { printf("\n1.Enqueue 2.Dequeue 3.Display 4.Exit\n"); scanf("%d", &choice); switch (choice) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; default: printf("Invalid choice\n"); } } } =========================================================================================================================================== // all code =========================================================================================================================================== =========================================================================================================================================== 1.singly link list =========================================================================================================================================== #include #include struct node { int data; struct node *next; }; struct node *head = NULL; // Insert at beginning void insert_begin(int val) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->next = head; head = newNode; } // Insert at end void insert_end(int val) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->next = NULL; if (head == NULL) { head = newNode; return; } struct node *temp = head; while (temp->next != NULL) temp = temp->next; temp->next = newNode; } // Delete from beginning void delete_begin() { if (head == NULL) { printf("List Empty\n"); return; } struct node *temp = head; head = head->next; free(temp); } // Display void display() { struct node *temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { insert_begin(10); insert_end(20); insert_end(30); display(); delete_begin(); display(); return 0; } 2. doubly link list =========================================================================================================================================== #include #include struct node { int data; struct node *prev, *next; }; struct node *head = NULL; // Insert at beginning void insert_begin(int val) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->prev = NULL; newNode->next = head; if (head != NULL) head->prev = newNode; head = newNode; } // Insert at end void insert_end(int val) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->next = NULL; if (head == NULL) { newNode->prev = NULL; head = newNode; return; } struct node *temp = head; while (temp->next != NULL) temp = temp->next; temp->next = newNode; newNode->prev = temp; } // Delete from beginning void delete_begin() { if (head == NULL) { printf("List Empty\n"); return; } struct node *temp = head; head = head->next; if (head != NULL) head->prev = NULL; free(temp); } // Display void display() { struct node *temp = head; while (temp != NULL) { printf("%d <-> ", temp->data); temp = temp->next; } printf("NULL\n"); } int main() { insert_begin(10); insert_end(20); insert_end(30); display(); delete_begin(); display(); return 0; } 3.circular link list =========================================================================================================================================== #include #include struct node { int data; struct node *next; }; struct node *last = NULL; // Insert into empty list void insert_empty(int val) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->next = newNode; last = newNode; } // Insert at beginning void insert_begin(int val) { if (last == NULL) { insert_empty(val); return; } struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->next = last->next; last->next = newNode; } // Insert at end void insert_end(int val) { if (last == NULL) { insert_empty(val); return; } struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->next = last->next; last->next = newNode; last = newNode; } // Delete from beginning void delete_begin() { if (last == NULL) { printf("List Empty\n"); return; } struct node *temp = last->next; if (last->next == last) { last = NULL; } else { last->next = temp->next; } free(temp); } // Display void display() { if (last == NULL) return; struct node *temp = last->next; do { printf("%d -> ", temp->data); temp = temp->next; } while (temp != last->next); printf("(circular)\n"); } int main() { insert_empty(10); insert_end(20); insert_end(30); display(); delete_begin(); display(); return 0; } 4.stack using array=========================================================================================================================================== #include #define MAX 100 int stack[MAX]; int top = -1; // Push void push(int val) { if (top == MAX - 1) { printf("Stack Overflow\n"); return; } stack[++top] = val; } // Pop void pop() { if (top == -1) { printf("Stack Underflow\n"); return; } printf("Deleted element: %d\n", stack[top--]); } // Peep (Peek) void peep() { if (top == -1) { printf("Stack is Empty\n"); return; } printf("Top element: %d\n", stack[top]); } // Display void display() { if (top == -1) { printf("Stack Empty\n"); return; } for (int i = top; i >= 0; i--) { printf("%d\n", stack[i]); } } // Main int main() { int choice, val; while (1) { printf("\n--- Stack using Array ---\n"); printf("1. Push\n2. Pop\n3. Peep\n4. Display\n5. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter value: "); scanf("%d", &val); push(val); break; case 2: pop(); break; case 3: peep(); break; case 4: display(); break; case 5: return 0; default: printf("Invalid choice\n"); } } } 5.stack using link list =========================================================================================================================================== #include #include struct node { int data; struct node *next; }; struct node *top = NULL; // Push void push(int val) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->next = top; top = newNode; } // Pop void pop() { if (top == NULL) { printf("Stack Underflow\n"); return; } struct node *temp = top; printf("Deleted element: %d\n", top->data); top = top->next; free(temp); } // Peep (Peek) void peep() { if (top == NULL) { printf("Stack is Empty\n"); return; } printf("Top element: %d\n", top->data); } // Display void display() { if (top == NULL) { printf("Stack Empty\n"); return; } struct node *temp = top; while (temp != NULL) { printf("%d\n", temp->data); temp = temp->next; } } // Main int main() { int choice, val; while (1) { printf("\n--- Stack using Linked List ---\n"); printf("1. Push\n2. Pop\n3. Peep\n4. Display\n5. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter value: "); scanf("%d", &val); push(val); break; case 2: pop(); break; case 3: peep(); break; case 4: display(); break; case 5: return 0; default: printf("Invalid choice\n"); } } } 6.queue using array =========================================================================================================================================== #include #define MAX 100 int queue[MAX]; int front = -1, rear = -1; // Add (Enqueue) void enqueue(int val) { if (rear == MAX - 1) { printf("Queue Overflow\n"); return; } if (front == -1) front = 0; queue[++rear] = val; } // Remove (Dequeue) void dequeue() { if (front == -1 || front > rear) { printf("Queue Underflow\n"); return; } printf("Deleted element: %d\n", queue[front++]); } // Display void display() { if (front == -1 || front > rear) { printf("Queue Empty\n"); return; } for (int i = front; i <= rear; i++) { printf("%d ", queue[i]); } printf("\n"); } // Main int main() { int choice, val; while (1) { printf("\n--- Queue using Array ---\n"); printf("1. Add\n2. Remove\n3. Display\n4. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter value: "); scanf("%d", &val); enqueue(val); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; default: printf("Invalid choice\n"); } } } 7.queue using link list =========================================================================================================================================== #include #include struct node { int data; struct node *next; }; struct node *front = NULL, *rear = NULL; // Add (Enqueue) void enqueue(int val) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->next = NULL; if (rear == NULL) { front = rear = newNode; return; } rear->next = newNode; rear = newNode; } // Remove (Dequeue) void dequeue() { if (front == NULL) { printf("Queue Underflow\n"); return; } struct node *temp = front; printf("Deleted element: %d\n", front->data); front = front->next; if (front == NULL) rear = NULL; free(temp); } // Display void display() { if (front == NULL) { printf("Queue Empty\n"); return; } struct node *temp = front; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } // Main int main() { int choice, val; while (1) { printf("\n--- Queue using Linked List ---\n"); printf("1. Add\n2. Remove\n3. Display\n4. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter value: "); scanf("%d", &val); enqueue(val); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; default: printf("Invalid choice\n"); } } } 8.Circular Queue using Array =========================================================================================================================================== #include #define MAX 5 int queue[MAX]; int front = -1, rear = -1; // Insert (Enqueue) void enqueue(int val) { if ((rear + 1) % MAX == front) { printf("Queue Overflow\n"); return; } if (front == -1) front = 0; rear = (rear + 1) % MAX; queue[rear] = val; } // Delete (Dequeue) void dequeue() { if (front == -1) { printf("Queue Underflow\n"); return; } printf("Deleted element: %d\n", queue[front]); if (front == rear) { front = rear = -1; } else { front = (front + 1) % MAX; } } // Display void display() { if (front == -1) { printf("Queue Empty\n"); return; } int i = front; printf("Queue elements: "); while (1) { printf("%d ", queue[i]); if (i == rear) break; i = (i + 1) % MAX; } printf("\n"); } // Main int main() { int choice, val; while (1) { printf("\n--- Circular Queue (Array) ---\n"); printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter value: "); scanf("%d", &val); enqueue(val); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; default: printf("Invalid choice\n"); } } } 9.Circular Queue using Linked List =========================================================================================================================================== #include #include struct node { int data; struct node *next; }; struct node *front = NULL, *rear = NULL; // Insert (Enqueue) void enqueue(int val) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; if (rear == NULL) { front = rear = newNode; rear->next = front; return; } rear->next = newNode; rear = newNode; rear->next = front; } // Delete (Dequeue) void dequeue() { if (front == NULL) { printf("Queue Underflow\n"); return; } struct node *temp = front; printf("Deleted element: %d\n", front->data); if (front == rear) { front = rear = NULL; } else { front = front->next; rear->next = front; } free(temp); } // Display void display() { if (front == NULL) { printf("Queue Empty\n"); return; } struct node *temp = front; printf("Queue elements: "); do { printf("%d ", temp->data); temp = temp->next; } while (temp != front); printf("\n"); } // Main int main() { int choice, val; while (1) { printf("\n--- Circular Queue (Linked List) ---\n"); printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter value: "); scanf("%d", &val); enqueue(val); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; default: printf("Invalid choice\n"); } } } 10.Depth First Search (DFS) =========================================================================================================================================== #include #define MAX 10 int adj[MAX][MAX], visited[MAX]; int n; // DFS function void dfs(int v) { printf("%d ", v); visited[v] = 1; for (int i = 0; i < n; i++) { if (adj[v][i] == 1 && visited[i] == 0) { dfs(i); } } } int main() { int start; printf("Enter number of vertices: "); scanf("%d", &n); printf("Enter adjacency matrix:\n"); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &adj[i][j]); for (int i = 0; i < n; i++) visited[i] = 0; printf("Enter starting vertex: "); scanf("%d", &start); printf("DFS Traversal: "); dfs(start); return 0; } 11.Breadth First Search (BFS) =========================================================================================================================================== #include #define MAX 10 int adj[MAX][MAX], visited[MAX], queue[MAX]; int front = 0, rear = -1; int n; // Enqueue void enqueue(int v) { queue[++rear] = v; } // Dequeue int dequeue() { return queue[front++]; } // BFS function void bfs(int start) { enqueue(start); visited[start] = 1; while (front <= rear) { int v = dequeue(); printf("%d ", v); for (int i = 0; i < n; i++) { if (adj[v][i] == 1 && visited[i] == 0) { enqueue(i); visited[i] = 1; } } } } int main() { int start; printf("Enter number of vertices: "); scanf("%d", &n); printf("Enter adjacency matrix:\n"); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &adj[i][j]); for (int i = 0; i < n; i++) visited[i] = 0; printf("Enter starting vertex: "); scanf("%d", &start); printf("BFS Traversal: "); bfs(start); return 0; } 12. Tree traversal using recursive =========================================================================================================================================== #include #include struct node { int data; struct node *left, *right; }; // Create new node struct node* create(int val) { struct node* newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->left = newNode->right = NULL; return newNode; } // Inorder (LNR) void inorder(struct node* root) { if (root != NULL) { inorder(root->left); printf("%d ", root->data); inorder(root->right); } } // Preorder (NLR) void preorder(struct node* root) { if (root != NULL) { printf("%d ", root->data); preorder(root->left); preorder(root->right); } } // Postorder (LRN) void postorder(struct node* root) { if (root != NULL) { postorder(root->left); postorder(root->right); printf("%d ", root->data); } } int main() { // Example Tree struct node* root = create(1); root->left = create(2); root->right = create(3); root->left->left = create(4); root->left->right = create(5); printf("Inorder: "); inorder(root); printf("\nPreorder: "); preorder(root); printf("\nPostorder: "); postorder(root); return 0; } 13.Non-Recursive Tree Traversal =========================================================================================================================================== 13.1.Inorder using Stack ============================== #include #include #define MAX 100 struct node { int data; struct node *left, *right; }; struct node* stack[MAX]; int top = -1; void push(struct node* n) { stack[++top] = n; } struct node* pop() { return stack[top--]; } int isEmpty() { return top == -1; } void inorder(struct node* root) { struct node* curr = root; while (curr != NULL || !isEmpty()) { while (curr != NULL) { push(curr); curr = curr->left; } curr = pop(); printf("%d ", curr->data); curr = curr->right; } } 13.2.Preorder using Stack ===================================== void preorder(struct node* root) { if (root == NULL) return; push(root); while (!isEmpty()) { struct node* temp = pop(); printf("%d ", temp->data); if (temp->right) push(temp->right); if (temp->left) push(temp->left); } } 13.3.Postorder using Two Stacks ================================== struct node* stack2[MAX]; int top2 = -1; void push2(struct node* n) { stack2[++top2] = n; } struct node* pop2() { return stack2[top2--]; } void postorder(struct node* root) { if (root == NULL) return; push(root); while (!isEmpty()) { struct node* temp = pop(); push2(temp); if (temp->left) push(temp->left); if (temp->right) push(temp->right); } while (top2 != -1) { printf("%d ", pop2()->data); } } 13.4.Main Function (for Non-Recursive) ========================== int main() { struct node* root = create(1); root->left = create(2); root->right = create(3); root->left->left = create(4); root->left->right = create(5); printf("Inorder: "); inorder(root); printf("\nPreorder: "); preorder(root); printf("\nPostorder: "); postorder(root); return 0; } 14.Binary Search Tree + Traversals =========================================================================================================================================== #include #include // Node structure struct node { int data; struct node *left, *right; }; // Create new node struct node* createNode(int val) { struct node* newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->left = newNode->right = NULL; return newNode; } // Insert into BST struct node* insert(struct node* root, int val) { if (root == NULL) return createNode(val); if (val < root->data) root->left = insert(root->left, val); else if (val > root->data) root->right = insert(root->right, val); return root; } // Inorder Traversal (LNR) void inorder(struct node* root) { if (root != NULL) { inorder(root->left); printf("%d ", root->data); inorder(root->right); } } // Preorder Traversal (NLR) void preorder(struct node* root) { if (root != NULL) { printf("%d ", root->data); preorder(root->left); preorder(root->right); } } // Postorder Traversal (LRN) void postorder(struct node* root) { if (root != NULL) { postorder(root->left); postorder(root->right); printf("%d ", root->data); } } // Main Function int main() { struct node* root = NULL; int n, val; printf("Enter number of nodes: "); scanf("%d", &n); printf("Enter values:\n"); for (int i = 0; i < n; i++) { scanf("%d", &val); root = insert(root, val); } printf("\nInorder Traversal: "); inorder(root); printf("\nPreorder Traversal: "); preorder(root); printf("\nPostorder Traversal: "); postorder(root); return 0; } 15.BST Insert & Delete =========================================================================================================================================== #include #include // Node structure struct node { int data; struct node *left, *right; }; // Create new node struct node* createNode(int val) { struct node* newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = val; newNode->left = newNode->right = NULL; return newNode; } // Insert node struct node* insert(struct node* root, int val) { if (root == NULL) return createNode(val); if (val < root->data) root->left = insert(root->left, val); else if (val > root->data) root->right = insert(root->right, val); return root; } // Find minimum node (used in delete) struct node* findMin(struct node* root) { while (root->left != NULL) root = root->left; return root; } // Delete node struct node* deleteNode(struct node* root, int val) { if (root == NULL) return NULL; if (val < root->data) root->left = deleteNode(root->left, val); else if (val > root->data) root->right = deleteNode(root->right, val); else { // Case 1: No child if (root->left == NULL && root->right == NULL) { free(root); return NULL; } // Case 2: One child else if (root->left == NULL) { struct node* temp = root->right; free(root); return temp; } else if (root->right == NULL) { struct node* temp = root->left; free(root); return temp; } // Case 3: Two children else { struct node* temp = findMin(root->right); root->data = temp->data; root->right = deleteNode(root->right, temp->data); } } return root; } // Inorder traversal (for checking) void inorder(struct node* root) { if (root != NULL) { inorder(root->left); printf("%d ", root->data); inorder(root->right); } } // Main function int main() { struct node* root = NULL; int n, val, del; printf("Enter number of nodes: "); scanf("%d", &n); printf("Enter values:\n"); for (int i = 0; i < n; i++) { scanf("%d", &val); root = insert(root, val); } printf("Inorder before deletion: "); inorder(root); printf("\nEnter value to delete: "); scanf("%d", &del); root = deleteNode(root, del); printf("Inorder after deletion: "); inorder(root); return 0; } 16.Linear Search =========================================================================================================================================== #include int main() { int arr[100], n, key, i, found = 0; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements:\n"); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Enter element to search: "); scanf("%d", &key); for (i = 0; i < n; i++) { if (arr[i] == key) { printf("Element found at position %d\n", i + 1); found = 1; break; } } if (!found) { printf("Element not found\n"); } return 0; } 17.Binary Search =========================================================================================================================================== #include int main() { int arr[100], n, key; int low = 0, high, mid, found = 0; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter sorted elements:\n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } high = n - 1; printf("Enter element to search: "); scanf("%d", &key); while (low <= high) { mid = (low + high) / 2; if (arr[mid] == key) { printf("Element found at position %d\n", mid + 1); found = 1; break; } else if (arr[mid] < key) { low = mid + 1; } else { high = mid - 1; } } if (!found) { printf("Element not found\n"); } return 0; } 18.Sort Array in Ascending Order =========================================================================================================================================== #include int main() { int arr[100], n, i, j, temp; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements:\n"); for (i = 0; i < n; i++) scanf("%d", &arr[i]); // Bubble Sort 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; } } } printf("Sorted array (Ascending): "); for (i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } 19.Insertion Sort =========================================================================================================================================== #include int main() { int arr[100], n, i, j, key; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements:\n"); for (i = 0; i < n; i++) scanf("%d", &arr[i]); // Insertion Sort for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } printf("Sorted array: "); for (i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } 20.Selection Sort =========================================================================================================================================== #include int main() { int arr[100], n, i, j, min, temp; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements:\n"); for (i = 0; i < n; i++) scanf("%d", &arr[i]); // Selection Sort for (i = 0; i < n - 1; i++) { min = i; for (j = i + 1; j < n; j++) { if (arr[j] < arr[min]) min = j; } temp = arr[i]; arr[i] = arr[min]; arr[min] = temp; } printf("Sorted array: "); for (i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } 21.Merge Sort =========================================================================================================================================== #include void merge(int arr[], int low, int mid, int high) { int i = low, j = mid + 1, k = low; int temp[100]; while (i <= mid && j <= high) { if (arr[i] < arr[j]) temp[k++] = arr[i++]; else temp[k++] = arr[j++]; } while (i <= mid) temp[k++] = arr[i++]; while (j <= high) temp[k++] = arr[j++]; for (i = low; i <= high; i++) arr[i] = temp[i]; } void mergeSort(int arr[], int low, int high) { if (low < high) { int mid = (low + high) / 2; mergeSort(arr, low, mid); mergeSort(arr, mid + 1, high); merge(arr, low, mid, high); } } int main() { int arr[100], n, i; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements:\n"); for (i = 0; i < n; i++) scanf("%d", &arr[i]); mergeSort(arr, 0, n - 1); printf("Sorted array: "); for (i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }