├── Array Search Element ├── Array-Implimentation-Queue ├── Array-implimentation-of-stack ├── Binary Search ├── Bubble Sort ├── Circular-Queue ├── Doubly Linked List ├── Infix-to-postfix ├── Insertion-Sorting ├── Linear Search ├── Linked-List ├── Linked-List-All-Operations ├── Linked-Queue ├── MDrivenCircularQueue ├── Merge Sorting ├── Recursive function ├── Selection-Sort └── implementation -of stack /Array Search Element: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_SIZE 100 // Maximum array size 3 | 4 | int main() 5 | { 6 | int arr[MAX_SIZE]; 7 | int size, i, toSearch, found; 8 | 9 | /* Input size of array */ 10 | printf("Enter size of array: "); 11 | scanf("%d", &size); 12 | 13 | /* Input elements of array */ 14 | printf("Enter elements in array: "); 15 | for(i=0; i 2 | 3 | #include 4 | 5 | #define maxsize 5 6 | void insert(); 7 | void deleteq(); 8 | void display(); 9 | int front = -1, rear = -1; 10 | int queue[maxsize]; 11 | int main() { 12 | int choice; 13 | while (choice != 4) { 14 | 15 | printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n"); 16 | printf("\nEnter your choice ?"); 17 | scanf("%d", & choice); 18 | switch (choice) { 19 | case 1: 20 | insert(); 21 | break; 22 | case 2: 23 | deleteq(); 24 | break; 25 | case 3: 26 | display(); 27 | break; 28 | case 4: 29 | exit(0); 30 | break; 31 | default: 32 | printf("\nEnter valid choice??\n"); 33 | } 34 | } 35 | } 36 | void insert() { 37 | int item; 38 | printf("\nEnter the element\n"); 39 | scanf("\n%d", & item); 40 | if (rear == maxsize - 1) { 41 | printf("\nOVERFLOW\n"); 42 | return; 43 | } 44 | if (front == -1 && rear == -1) { 45 | front = 0; 46 | rear = 0; 47 | } else { 48 | rear = rear + 1; 49 | } 50 | queue[rear] = item; 51 | printf("\nValue inserted "); 52 | 53 | } 54 | void deleteq() { 55 | int item; 56 | if (front == -1 || front > rear) { 57 | printf("\nUNDERFLOW\n"); 58 | return; 59 | 60 | } else { 61 | item = queue[front]; 62 | if (front == rear) { 63 | front = -1; 64 | rear = -1; 65 | } else { 66 | front = front + 1; 67 | } 68 | printf("\nvalue deleted "); 69 | } 70 | 71 | } 72 | 73 | void display() { 74 | int i; 75 | if (rear == -1) { 76 | printf("\nEmpty queue\n"); 77 | } else { 78 | printf("\nprinting values .....\n"); 79 | for (i = front; i <= rear; i++) { 80 | printf("\n%d\n", queue[i]); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Array-implimentation-of-stack: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | //Function declarations 5 | void push(); 6 | void pop(); 7 | void peek(); 8 | 9 | //stack array of size 20 10 | int stack[20],i,j,option=0,n,top=-1; 11 | int main () 12 | { 13 | // Size of stack 14 | printf("Enter the number of elements in the stack\n"); 15 | scanf("%d",&n); 16 | printf("Stack implementation using array\n"); 17 | 18 | //Taking user input for the operation to be performed 19 | while(option != 4) 20 | { 21 | printf("Select your option...\n"); 22 | printf("\n1.Push\n2.Pop\n3.Peek\n4.Exit"); 23 | printf("\nEnter your option.\n"); 24 | scanf("%d",&option); 25 | switch(option) 26 | { 27 | case 1: 28 | { 29 | push(); 30 | break; 31 | } 32 | case 2: 33 | { 34 | pop(); 35 | break; 36 | } 37 | case 3: 38 | { 39 | peek(); 40 | break; 41 | } 42 | case 4: 43 | { 44 | printf("Exiting...\n"); 45 | break; 46 | } 47 | default: 48 | { 49 | printf("\nPlease Enter valid option.?\n"); 50 | } 51 | }; 52 | } 53 | } 54 | 55 | // Push operation function 56 | void push () 57 | { 58 | int value; 59 | if (top == n ) 60 | printf("\n Stack Overflow..!"); 61 | else 62 | { 63 | printf("Enter the value?\n"); 64 | scanf("%d",&value); 65 | top = top +1; 66 | stack[top] = value; 67 | } 68 | } 69 | 70 | // Pop operation function 71 | void pop () 72 | { 73 | if(top == -1) 74 | printf("Stack Underflow..!\n"); 75 | else 76 | top = top -1; 77 | } 78 | 79 | // peek operation function to retrive all elements 80 | void peek() 81 | { 82 | for (i=top;i>=0;i--) 83 | { 84 | printf("%d\n",stack[i]); 85 | } 86 | if(top == -1) 87 | { 88 | printf("Stack is empty..!\n"); 89 | } 90 | } 91 | 92 | 93 | -------------------------------------------------------------------------------- /Binary Search: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to perform Binary Search 4 | int binarySearch(int arr[], int l, int r, int x) { 5 | while (l <= r) { 6 | int m = l + (r - l) / 2; 7 | 8 | // Check if x is present at mid point 9 | if (arr[m] == x) 10 | return m; 11 | 12 | // If x is greater, ignore left half 13 | if (arr[m] < x) 14 | l = m + 1; 15 | 16 | // If x is smaller, ignore right half 17 | else 18 | r = m - 1; 19 | } 20 | 21 | // If we reach here, then element was not present 22 | return -1; 23 | } 24 | 25 | // Driver program to test above functions 26 | int main() { 27 | int arr[] = {2, 3, 4, 10, 40}; 28 | int n = sizeof(arr)/sizeof(arr[0]); 29 | int x = 10; // Element to be searched 30 | int result = binarySearch(arr, 0, n-1, x); 31 | 32 | if(result != -1) 33 | printf("Element found at index %d\n", result); 34 | else 35 | printf("Element not found in array.\n"); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Bubble Sort: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int swap(int *x, int *y) 4 | { 5 | int temp = *x; 6 | *x = *y; 7 | *y = temp; 8 | return 0; 9 | } 10 | 11 | int sort(int array[], int n) 12 | { 13 | int flag = 0, loop = 0; 14 | for (int i = 0; i < n - 1; i++) 15 | { 16 | for (int j = 0; j < n - i - 2; j++) 17 | { 18 | if (array[j] > array[j + 1]) 19 | { 20 | swap(&array[j], &array[j + 1]); 21 | flag++; 22 | } 23 | } 24 | loop++; 25 | } 26 | printf("\n swping done for %d || loop ran for %d \n\n", flag, loop); 27 | return 0; 28 | } 29 | 30 | int printArray(int array[], int n) 31 | { 32 | for (int i = 0; i < n - 1; i++) 33 | { 34 | printf("%d ", array[i]); 35 | } 36 | printf("\n\n"); 37 | return 0; 38 | } 39 | 40 | int main() 41 | { 42 | int array[] = {7, 4, 19, 87, 45, 90, 189, 78, -2, 334, 84, 51, 0, 251, 5, 211, 133, 9, 17, 0, 112, 11, 478, 7, 45, -18, 252, 38, 84, -25, 27, 5, 8, 42, 5}; 43 | int n = sizeof(array) / sizeof(array[0]); 44 | printf("\n before sorting: \n"); 45 | printArray(array, n); 46 | int res = sort(array, n); 47 | printf("sorted array is: \n"); 48 | printArray(array, n); 49 | } 50 | -------------------------------------------------------------------------------- /Circular-Queue: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_SIZE 5 // Define the maximum size of the Circular Queue 3 | 4 | // Circular Queue structure definition 5 | typedef struct { 6 | int data[MAX_SIZE]; 7 | int front; 8 | int rear; 9 | int count; 10 | } CircularQueue; 11 | 12 | // Initialize the Circular Queue 13 | void initQueue(CircularQueue* q) { 14 | q->front = 0; 15 | q->rear = 0; 16 | q->count = 0; 17 | } 18 | 19 | // Check if the queue is full 20 | int isFull(CircularQueue* q) { 21 | return q->count == MAX_SIZE; 22 | } 23 | 24 | // Check if the queue is empty 25 | int isEmpty(CircularQueue* q) { 26 | return q->count == 0; 27 | } 28 | 29 | // Enqueue an element to the queue 30 | int enqueue(CircularQueue* q, int item) { 31 | if (isFull(q)) { 32 | printf("Queue is full\n"); 33 | return -1; // Error code indicating full queue 34 | } 35 | 36 | q->data[q->rear] = item; // Add item to the rear of the queue 37 | q->rear = (q->rear + 1) % MAX_SIZE; // Move rear pointer in a circular manner 38 | q->count++; // Increase the item count 39 | 40 | return 0; // Success 41 | } 42 | 43 | // Dequeue an element from the queue 44 | int dequeue(CircularQueue* q) { 45 | if (isEmpty(q)) { 46 | printf("Queue is empty\n"); 47 | return -1; // Error code indicating empty queue 48 | } 49 | 50 | int item = q->data[q->front]; // Get the item at the front of the queue 51 | q->front = (q->front + 1) % MAX_SIZE; // Move front pointer in a circular manner 52 | q->count--; // Decrease the item count 53 | 54 | return item; // Return dequeued item 55 | } 56 | 57 | // Peek at the front element without removing it 58 | int peek(CircularQueue* q) { 59 | if (isEmpty(q)) { 60 | printf("Queue is empty\n"); 61 | return -1; // Error code indicating empty queue 62 | } 63 | 64 | return q->data[q->front]; // Return the front element 65 | } 66 | 67 | // Display the queue contents 68 | void displayQueue(CircularQueue* q) { 69 | if (isEmpty(q)) { 70 | printf("Queue is empty\n"); 71 | return; 72 | } 73 | 74 | int i = q->front; 75 | printf("Queue contents: "); 76 | do { 77 | printf("%d ", q->data[i]); // Print each element 78 | i = (i + 1) % MAX_SIZE; // Move index in circular manner 79 | } while (i != q->rear); // Stop when we reach the rear 80 | printf("\n"); 81 | } 82 | 83 | // Main function to demonstrate the Circular Queue operations 84 | int main() { 85 | CircularQueue q; 86 | initQueue(&q); 87 | 88 | // Enqueue operations 89 | enqueue(&q, 1); 90 | enqueue(&q, 2); 91 | enqueue(&q, 3); 92 | enqueue(&q, 4); 93 | enqueue(&q, 5); // Queue becomes full here 94 | 95 | // Display the queue after enqueuing 96 | printf("Queue after enqueuing: "); 97 | displayQueue(&q); 98 | 99 | // Dequeue operation 100 | int dequeuedItem = dequeue(&q); 101 | printf("Dequeued item: %d\n", dequeuedItem); 102 | 103 | // Display the queue after dequeuing 104 | printf("Queue after dequeuing: "); 105 | displayQueue(&q); 106 | 107 | // Peek operation 108 | int peekedItem = peek(&q); 109 | printf("Peeked item: %d\n", peekedItem); 110 | 111 | // Attempt to enqueue when the queue is full (should fail) 112 | int enqueueResult = enqueue(&q, 6); // This should fail because the queue is full 113 | if (enqueueResult == -1) { 114 | printf("Enqueue failed: Queue is full\n"); 115 | } 116 | 117 | // Dequeue remaining elements to test circular behavior 118 | dequeue(&q); 119 | dequeue(&q); 120 | dequeue(&q); 121 | dequeue(&q); // Queue becomes empty here 122 | 123 | printf("Queue after multiple dequeues: "); 124 | displayQueue(&q); 125 | 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /Doubly Linked List: -------------------------------------------------------------------------------- 1 | //doubly linked list 2 | /*#include 3 | #include 4 | 5 | struct Node 6 | { 7 | int val; 8 | struct Node *prev; 9 | struct Node *next; 10 | }; 11 | 12 | typedef struct Node N; 13 | N *start = NULL, *tmp, *ptr; 14 | 15 | void CreateList() 16 | { 17 | tmp = (N *)malloc(sizeof(N)); 18 | printf("Enter value : "); 19 | scanf("%d", &tmp->val); 20 | tmp->next = NULL; 21 | if (start == NULL) 22 | { 23 | ptr = tmp; 24 | start = tmp; 25 | tmp->prev = NULL; 26 | } 27 | else 28 | { 29 | ptr->next = tmp; 30 | tmp->prev = ptr; 31 | ptr = tmp; 32 | } 33 | } 34 | 35 | void DisplayList() 36 | { 37 | tmp = start; 38 | while (tmp->next != NULL) 39 | { 40 | printf("%d -> ", tmp->val); 41 | tmp = tmp->next; 42 | } 43 | while (tmp != NULL) 44 | { 45 | printf("%d <- ", tmp->val); 46 | tmp = tmp->prev; 47 | } 48 | } 49 | 50 | void InsertBeg() 51 | { 52 | ptr = (N *)malloc(sizeof(N)); 53 | printf("Enter value : "); 54 | scanf("%d",&ptr->val); 55 | ptr->prev = NULL; 56 | ptr->next = start; 57 | tmp = start; 58 | tmp->prev = ptr; 59 | } 60 | 61 | void InsertEnd() 62 | { 63 | tmp = start; 64 | while(tmp->next!=NULL) 65 | { 66 | tmp = tmp->next; 67 | } 68 | ptr = (N *)malloc(sizeof(N)); 69 | printf("Enter value : "); 70 | scanf("%d",&ptr->val); 71 | ptr->next = NULL; 72 | ptr->prev = tmp; 73 | tmp->next = ptr; 74 | } 75 | 76 | void InsertAfter() 77 | { 78 | int n; 79 | printf("Enter node after which you want to insert : "); 80 | scanf("%d",&n); 81 | ptr = start; 82 | while(ptr->val!=n) 83 | { 84 | ptr = ptr->next; 85 | } 86 | tmp = (N *)malloc(sizeof(N)); 87 | printf("Enter value : "); 88 | scanf("%d",&tmp->val); 89 | tmp->next = ptr->next; 90 | ptr->next->prev = tmp; 91 | ptr->next = tmp; 92 | tmp->prev = ptr; 93 | } 94 | 95 | void DeleteFirst() 96 | { 97 | tmp = start; 98 | start = tmp->next; 99 | tmp->next->prev = NULL; 100 | free(tmp); 101 | } 102 | 103 | void DeleteLast() 104 | { 105 | tmp = start; 106 | while(tmp->next->next!=NULL) 107 | { 108 | tmp = tmp->next; 109 | } 110 | free(tmp->next); 111 | tmp->next = NULL; 112 | } 113 | 114 | void DeleteAny() 115 | { 116 | int n; 117 | printf("Enter value to delete : "); 118 | scanf("%d",&n); 119 | tmp = start; 120 | while(tmp->next->val!=n) 121 | { 122 | tmp = tmp->next; 123 | } 124 | ptr = tmp->next; 125 | tmp->next = tmp->next->next; 126 | tmp->next->next->prev = tmp; 127 | free (ptr); 128 | } 129 | 130 | int main() 131 | { 132 | int ch; 133 | do 134 | { 135 | printf("\n\t-: M E N U :- \n================================\n"); 136 | printf("\n1.....Create List."); 137 | printf("\n2.....Display List."); 138 | printf("\n3.....Insert at Beginning."); 139 | printf("\n4.....Insert at the end."); 140 | printf("\n5.....Insert After a node."); 141 | printf("\n6.....Delete First node."); 142 | printf("\n7.....Delete Last node."); 143 | printf("\n8.....Delete Any node."); 144 | printf("\n0.....Exit."); 145 | printf("\nEnter your choice : "); 146 | scanf("%d", &ch); 147 | switch (ch) 148 | { 149 | case 0: 150 | printf("\nEnd of Program\n"); 151 | break; 152 | case 1: 153 | CreateList(); 154 | break; 155 | case 2: 156 | DisplayList(); 157 | break; 158 | case 3: 159 | InsertBeg(); 160 | break; 161 | case 4: 162 | InsertEnd(); 163 | break; 164 | case 5: 165 | InsertAfter(); 166 | break; 167 | case 6: 168 | DeleteFirst(); 169 | break; 170 | case 7: 171 | DeleteLast(); 172 | break; 173 | case 8: 174 | DeleteAny(); 175 | break; 176 | default: 177 | printf("\nInvalid Choice!!\n"); 178 | } 179 | 180 | } while (ch != 0); 181 | return 0; 182 | } 183 | -------------------------------------------------------------------------------- /Infix-to-postfix: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char stack[100]; 5 | int top = -1; 6 | 7 | void push(char x) 8 | { 9 | stack[++top] = x; 10 | } 11 | 12 | char pop() 13 | { 14 | if(top == -1) 15 | return -1; 16 | else 17 | return stack[top--]; 18 | } 19 | 20 | int priority(char x) 21 | { 22 | if(x == '(') 23 | return 0; 24 | if(x == '+' || x == '-') 25 | return 1; 26 | if(x == '*' || x == '/') 27 | return 2; 28 | return 0; 29 | } 30 | 31 | int main() 32 | { 33 | char exp[100]; 34 | char *e, x; 35 | printf("Enter the expression : "); 36 | scanf("%s",exp); 37 | printf("\n"); 38 | e = exp; 39 | 40 | while(*e != '\0') 41 | { 42 | if(isalnum(*e)) 43 | printf("%c ",*e); 44 | else if(*e == '(') 45 | push(*e); 46 | else if(*e == ')') 47 | { 48 | while((x = pop()) != '(') 49 | printf("%c ", x); 50 | } 51 | else 52 | { 53 | while(priority(stack[top]) >= priority(*e)) 54 | printf("%c ",pop()); 55 | push(*e); 56 | } 57 | e++; 58 | } 59 | 60 | while(top != -1) 61 | { 62 | printf("%c ",pop()); 63 | }return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Insertion-Sorting: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int printArray(int array[], int n) 4 | { 5 | for (int i = 0; i < n - 1; i++) 6 | { 7 | printf("%d ", array[i]); 8 | } 9 | printf("\n\n"); 10 | } 11 | 12 | int main(){ 13 | int arr[] = {7,98,79,-2,8,98,7,-7,4,54,87,8,45,9}; 14 | 15 | 16 | int n = ((sizeof arr) /( sizeof arr[0])); 17 | 18 | 19 | printf("before sorting: \n"); 20 | printArray(arr, n); 21 | 22 | for (int i = 1; i < n-1; i++) 23 | { 24 | while (arr[i] < arr[i-1] && i-1 >= 0) 25 | { 26 | int temp = arr[i-1]; 27 | arr[i-1] = arr[i]; 28 | arr[i] = temp; 29 | i--; 30 | } 31 | 32 | } 33 | printf("After sorting: \n"); 34 | printArray(arr, n); 35 | } 36 | -------------------------------------------------------------------------------- /Linear Search: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to perform Linear Search 4 | int linearSearch(int arr[], int n, int target) { 5 | for (int i = 0; i < n; i++) { 6 | // If element is found, return its index 7 | if (arr[i] == target) 8 | return i; 9 | } 10 | // Element not found 11 | return -1; 12 | } 13 | 14 | // Driver program to test above functions 15 | int main() { 16 | int arr[] = {10, 20, 30, 40, 50}; 17 | int n = sizeof(arr)/sizeof(arr[0]); 18 | int target = 30; 19 | 20 | int result = linearSearch(arr, n, target); 21 | 22 | if(result != -1) 23 | printf("Element found at index %d\n", result); 24 | else 25 | printf("Element not found in array.\n"); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Linked-List: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | struct node 7 | { 8 | int num; 9 | struct node *ptr; 10 | }; 11 | typedef struct node NODE; 12 | 13 | NODE *head, *first, *temp = 0; 14 | int count = 0; 15 | int choice = 1; 16 | first = 0; 17 | 18 | while (choice) 19 | { 20 | head = (NODE *)malloc(sizeof(NODE)); 21 | printf("Enter the data item\n"); 22 | scanf("%d", &head-> num); 23 | if (first != 0) 24 | { 25 | temp->ptr = head; 26 | temp = head; 27 | } 28 | else 29 | { 30 | first = temp = head; 31 | } 32 | fflush(stdin); 33 | printf("Do you want to continue(Type 0 or 1)?\n"); 34 | scanf("%d", &choice); 35 | 36 | } 37 | temp->ptr = 0; 38 | // reset temp to the //beginning 39 | temp = first; 40 | printf("\n status of the linked list is\n"); 41 | while (temp != 0) 42 | { 43 | printf("%d=>", temp->num); 44 | count++; 45 | temp = temp -> ptr; 46 | } 47 | printf("NULL\n"); 48 | printf("No. of nodes in the list = %d\n", count); 49 | } 50 | -------------------------------------------------------------------------------- /Linked-List-All-Operations: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // Function prototypes 4 | void create(); 5 | void display(); 6 | void insert_begin(); 7 | void insert_end(); 8 | void insert_pos(int pos); 9 | void delete_begin(); 10 | void delete_end(); 11 | void delete_pos(int pos); 12 | int count_nodes(); // Added function prototype 13 | 14 | struct node { 15 | int info; 16 | struct node *next; 17 | }; 18 | 19 | struct node *start = NULL; 20 | 21 | int main() { 22 | int choice, pos; 23 | while (1) 24 | { 25 | printf("\n MENU \n"); 26 | printf("\n 1.Create \n"); 27 | printf("\n 2.Display \n"); 28 | printf("\n 3.Insert at the beginning \n"); 29 | printf("\n 4.Insert at the end \n"); 30 | printf("\n 5.Insert at specified position \n"); 31 | printf("\n 6.Delete from beginning \n"); 32 | printf("\n 7.Delete from the end \n"); 33 | printf("\n 8.Delete from specified position \n"); 34 | printf("\n 9.Exit \n"); 35 | printf("\n--------------------------------------\n"); 36 | printf("\nEnter your choice:\t"); 37 | scanf("%d", &choice); 38 | 39 | switch (choice) 40 | { 41 | case 1: 42 | create(); 43 | break; 44 | case 2: 45 | display(); 46 | break; 47 | case 3: 48 | insert_begin(); 49 | break; 50 | case 4: 51 | insert_end(); 52 | break; 53 | case 5: 54 | printf("Enter the position: "); 55 | scanf("%d", &pos); 56 | insert_pos(pos); 57 | break; 58 | case 6: 59 | delete_begin(); 60 | break; 61 | case 7: 62 | delete_end(); 63 | break; 64 | case 8: 65 | printf("Enter the position: "); 66 | scanf("%d", &pos); 67 | delete_pos(pos); 68 | break; 69 | case 9: 70 | exit(0); 71 | break; 72 | default: 73 | printf("\nWrong Choice:\n"); 74 | break; 75 | } 76 | } 77 | return 0; 78 | } 79 | 80 | void create() 81 | { 82 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 83 | if (temp == NULL) 84 | { 85 | printf("\nOut of Memory Space:\n"); 86 | exit(0); 87 | } 88 | printf("\nEnter the data value for the node:\t"); 89 | scanf("%d", &temp->info); 90 | temp->next = NULL; 91 | if (start == NULL) 92 | { 93 | start = temp; 94 | } else 95 | { 96 | struct node *ptr = start; 97 | while (ptr->next != NULL) 98 | { 99 | ptr = ptr->next; 100 | } 101 | ptr->next = temp; 102 | } 103 | } 104 | 105 | void display() { 106 | struct node *ptr = start; 107 | if (start == NULL) { 108 | printf("\nList is empty:\n"); 109 | return; 110 | } 111 | printf("\nThe List elements are:\n"); 112 | while (ptr != NULL) { 113 | printf("%d => ", ptr->info); 114 | ptr = ptr->next; 115 | } 116 | printf("NULL\n"); 117 | } 118 | 119 | void insert_begin() { 120 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 121 | if (temp == NULL) { 122 | printf("\nOut of Memory Space:\n"); 123 | return; 124 | } 125 | printf("\nEnter the data value for the node:\t"); 126 | scanf("%d", &temp->info); 127 | temp->next = start; 128 | start = temp; 129 | } 130 | 131 | void insert_end() { 132 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 133 | if (temp == NULL) { 134 | printf("\nOut of Memory Space:\n"); 135 | return; 136 | } 137 | printf("\nEnter the data value for the node:\t"); 138 | scanf("%d", &temp->info); 139 | temp->next = NULL; 140 | if (start == NULL) { 141 | start = temp; 142 | } else { 143 | struct node *ptr = start; 144 | while (ptr->next != NULL) { 145 | ptr = ptr->next; 146 | } 147 | ptr->next = temp; 148 | } 149 | } 150 | 151 | void insert_pos(int pos) { 152 | if (pos <= 0 || pos > count_nodes() + 1) { 153 | printf("\nInvalid position.\n"); 154 | return; 155 | } 156 | struct node *temp = (struct node *)malloc(sizeof(struct node)); 157 | if (temp == NULL) { 158 | printf("\nOut of Memory Space:\n"); 159 | return; 160 | } 161 | printf("\nEnter the data value for the node:\t"); 162 | scanf("%d", &temp->info); 163 | temp->next = NULL; 164 | if (pos == 1) { 165 | insert_begin(); 166 | return; 167 | } 168 | struct node *ptr = start; 169 | for (int i = 1; i < pos - 1; i++) { 170 | if (ptr == NULL) { 171 | printf("\nPosition out of range.\n"); 172 | return; 173 | } 174 | ptr = ptr->next; 175 | } 176 | temp->next = ptr->next; 177 | ptr->next = temp; 178 | } 179 | 180 | void delete_begin() { 181 | if (start == NULL) { 182 | printf("\nList is empty.\n"); 183 | return; 184 | } 185 | struct node *ptr = start; 186 | start = start->next; 187 | free(ptr); 188 | } 189 | 190 | void delete_end() { 191 | if (start == NULL) { 192 | printf("\nList is empty.\n"); 193 | return; 194 | } 195 | if (start->next == NULL) { 196 | free(start); 197 | start = NULL; 198 | return; 199 | } 200 | struct node *ptr = start; 201 | while (ptr->next->next != NULL) { 202 | ptr = ptr->next; 203 | } 204 | free(ptr->next); 205 | ptr->next = NULL; 206 | } 207 | 208 | void delete_pos(int pos) { 209 | if (start == NULL) { 210 | printf("\nList is empty.\n"); 211 | return; 212 | } 213 | if (pos <= 0 || pos > count_nodes()) { 214 | printf("\nInvalid position.\n"); 215 | return; 216 | } 217 | struct node *ptr = start; 218 | for (int i = 1; i < pos - 1; i++) { 219 | if (ptr == NULL) { 220 | printf("\nPosition out of range.\n"); 221 | return; 222 | } 223 | ptr = ptr->next; 224 | } 225 | struct node *toDelete = ptr->next; 226 | ptr->next = toDelete->next; 227 | free(toDelete); 228 | } 229 | 230 | int count_nodes() { 231 | int count = 0; 232 | struct node *ptr = start; 233 | while (ptr != NULL) { 234 | count++; 235 | ptr = ptr->next; 236 | } 237 | return count; 238 | } 239 | -------------------------------------------------------------------------------- /Linked-Queue: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_SIZE 5 4 | 5 | typedef struct { 6 | int data[MAX_SIZE]; 7 | int front; 8 | int rear; 9 | int count; 10 | } CircularQueue; 11 | 12 | void initQueue(CircularQueue* q) { 13 | q->front = 0; 14 | q->rear = 0; 15 | q->count = 0; 16 | } 17 | 18 | int isFull(CircularQueue* q) { 19 | return q->count == MAX_SIZE; 20 | } 21 | 22 | int isEmpty(CircularQueue* q) { 23 | return q->count == 0; 24 | } 25 | 26 | int enqueue(CircularQueue* q, int item) { 27 | if (isFull(q)) { 28 | printf("Queue is full\n"); 29 | return -1; // Error code 30 | } 31 | 32 | q->data[q->rear] = item; 33 | q->rear = (q->rear + 1) % MAX_SIZE; 34 | q->count++; 35 | 36 | return 0; // Success 37 | } 38 | 39 | int dequeue(CircularQueue* q) { 40 | if (isEmpty(q)) { 41 | printf("Queue is empty\n"); 42 | return -1; // Error code 43 | } 44 | 45 | int item = q->data[q->front]; 46 | q->data[q->front] = 0; // Clear the dequeued element 47 | q->front = (q->front + 1) % MAX_SIZE; 48 | q->count--; 49 | 50 | return item; 51 | } 52 | 53 | int peek(CircularQueue* q) { 54 | if (isEmpty(q)) { 55 | printf("Queue is empty\n"); 56 | return -1; // Error code 57 | } 58 | 59 | return q->data[q->front]; 60 | } 61 | 62 | void displayQueue(CircularQueue* q) { 63 | if (isEmpty(q)) { 64 | printf("Queue is empty\n"); 65 | return; 66 | } 67 | 68 | int i = q->front; 69 | do { 70 | printf("%d ", q->data[i]); 71 | i = (i + 1) % MAX_SIZE; 72 | } while (i != q->rear); 73 | printf("\n"); 74 | } 75 | 76 | int main() { 77 | CircularQueue q; 78 | initQueue(&q); 79 | 80 | // Enqueue operations 81 | enqueue(&q, 1); 82 | enqueue(&q, 2); 83 | enqueue(&q, 3); 84 | enqueue(&q, 4); 85 | enqueue(&q, 5); 86 | 87 | printf("Queue after enqueuing: "); 88 | displayQueue(&q); 89 | 90 | // Dequeue operation 91 | int dequeuedItem = dequeue(&q); 92 | printf("Dequeued item: %d\n", dequeuedItem); 93 | 94 | printf("Queue after dequeuing: "); 95 | displayQueue(&q); 96 | 97 | // Peek operation 98 | int peekedItem = peek(&q); 99 | printf("Peeked item: %d\n", peekedItem); 100 | 101 | // Attempt to enqueue when full 102 | enqueue(&q, 6); // This should fail 103 | 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /MDrivenCircularQueue: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_SIZE 5 4 | 5 | typedef struct { 6 | int data[MAX_SIZE]; 7 | int front; 8 | int rear; 9 | int count; 10 | } 11 | CircularQueue; 12 | 13 | // Function to initialize the queue 14 | void initQueue(CircularQueue * q) { 15 | q -> front = 0; 16 | q -> rear = 0; 17 | q -> count = 0; 18 | } 19 | 20 | // Function to check if the queue is full 21 | int isFull(CircularQueue * q) { 22 | return q -> count == MAX_SIZE; 23 | } 24 | 25 | // Function to check if the queue is empty 26 | int isEmpty(CircularQueue * q) { 27 | return q -> count == 0; 28 | } 29 | 30 | // Function to add an element to the queue 31 | void enqueue(CircularQueue * q, int item) { 32 | if (isFull(q)) { 33 | printf("Queue is full. Cannot enqueue.\n"); 34 | return; 35 | } 36 | 37 | q -> data[q -> rear] = item; 38 | q -> rear = (q -> rear + 1) % MAX_SIZE; 39 | q -> count++; 40 | 41 | printf("%d has been enqueued.\n", item); 42 | } 43 | 44 | // Function to remove an element from the queue 45 | int dequeue(CircularQueue * q) { 46 | if (isEmpty(q)) { 47 | printf("Queue is empty. Cannot dequeue.\n"); 48 | return -1; // Return -1 to indicate failure 49 | } 50 | 51 | int item = q -> data[q -> front]; 52 | q -> data[q -> front] = 0; // Clear the dequeued element 53 | q -> front = (q -> front + 1) % MAX_SIZE; 54 | q -> count--; 55 | 56 | printf("%d has been dequeued.\n", item); 57 | return item; 58 | } 59 | 60 | // Function to display the contents of the queue 61 | void displayQueue(CircularQueue * q) { 62 | if (isEmpty(q)) { 63 | printf("Queue is empty.\n"); 64 | return; 65 | } 66 | 67 | printf("Queue contents: "); 68 | int i = q -> front; 69 | do { 70 | printf("%d ", q -> data[i]); 71 | i = (i + 1) % MAX_SIZE; 72 | } while (i != q -> rear); 73 | printf("\n"); 74 | } 75 | 76 | // Main function to demonstrate the usage of circular queue 77 | int main() { 78 | CircularQueue q; 79 | initQueue( & q); 80 | 81 | printf("Circular Queue Operations:\n"); 82 | printf("1. Enqueue\n"); 83 | printf("2. Dequeue\n"); 84 | printf("3. Display\n"); 85 | printf("4. Exit\n"); 86 | 87 | int choice, item; 88 | 89 | while (1) { 90 | printf("\nEnter your choice: "); 91 | scanf("%d", & choice); 92 | 93 | switch (choice) { 94 | case 1: 95 | printf("Enter element to enqueue: "); 96 | scanf("%d", & item); 97 | enqueue( & q, item); 98 | break; 99 | case 2: 100 | dequeue( & q); 101 | break; 102 | case 3: 103 | displayQueue( & q); 104 | break; 105 | case 4: 106 | printf("Exiting...\n"); 107 | return 0; 108 | default: 109 | printf("Invalid choice. Please choose again.\n"); 110 | } 111 | } 112 | 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /Merge Sorting: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Function to merge two sorted subarrays 5 | void merge(int arr[], int left, int mid, int right) { 6 | int n1 = mid - left + 1; 7 | int n2 = right - mid; 8 | 9 | // Temporary arrays 10 | int L[n1], R[n2]; 11 | 12 | // Copy data to temporary arrays L[] and R[] 13 | for (int i = 0; i < n1; i++) 14 | L[i] = arr[left + i]; 15 | for (int j = 0; j < n2; j++) 16 | R[j] = arr[mid + 1 + j]; 17 | 18 | // Merge the temporary arrays 19 | 20 | // Initial indexes of first and second subarrays 21 | int i = 0, j = 0; 22 | 23 | // Initial index of merged subarray array 24 | int k = left; 25 | while (i < n1 && j < n2) { 26 | if (L[i] <= R[j]) { 27 | arr[k] = L[i]; 28 | i++; 29 | } else { 30 | arr[k] = R[j]; 31 | j++; 32 | } 33 | k++; 34 | } 35 | 36 | // Copy the remaining elements of L[], if there are any 37 | while (i < n1) { 38 | arr[k] = L[i]; 39 | i++; 40 | k++; 41 | } 42 | 43 | // Copy the remaining elements of R[], if there are any 44 | while (j < n2) { 45 | arr[k] = R[j]; 46 | j++; 47 | k++; 48 | } 49 | } 50 | 51 | // MergeSort function 52 | void mergeSort(int arr[], int left, int right) { 53 | if (left < right) { 54 | // Find the middle point 55 | int mid = left + (right - left) / 2; 56 | 57 | // Sort first and second halves 58 | mergeSort(arr, left, mid); 59 | mergeSort(arr, mid + 1, right); 60 | 61 | // Merge the sorted halves 62 | merge(arr, left, mid, right); 63 | } 64 | } 65 | 66 | int main() { 67 | int n; 68 | printf("Enter the number of elements: "); 69 | scanf("%d", &n); 70 | 71 | int *arr = (int *)malloc(n * sizeof(int)); 72 | printf("Enter %d elements:\n", n); 73 | for (int i = 0; i < n; i++) { 74 | scanf("%d", &arr[i]); 75 | } 76 | 77 | mergeSort(arr, 0, n - 1); 78 | 79 | printf("\nSorted array is \n"); 80 | for (int i = 0; i < n; i++) 81 | printf("%d ", arr[i]); 82 | 83 | free(arr); // Free dynamically allocated memory 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /Recursive function: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Recursive function to calculate factorial 4 | long long factorial(int n) { 5 | // Base case: factorial of 0 or 1 is 1 6 | if (n == 0 || n == 1) { 7 | return 1; 8 | } 9 | // Recursive case: n! = n * (n-1)! 10 | else { 11 | return n * factorial(n - 1); 12 | } 13 | } 14 | 15 | int main() { 16 | int num; 17 | 18 | printf("Enter a positive integer: "); 19 | scanf("%d", &num); 20 | 21 | if (num < 0) { 22 | printf("Error! Factorial of a negative number doesn't exist."); 23 | } 24 | else { 25 | printf("Factorial of %d = %lld\n", num, factorial(num)); 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Selection-Sort: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to swap values at two pointers 4 | void swap(int *xp, int *yp) { 5 | int temp = *xp; 6 | *xp = *yp; 7 | *yp = temp; 8 | } 9 | 10 | // Function to perform Selection Sort 11 | void selectionSort(int arr[], int n) { 12 | int i, j, min_idx; 13 | 14 | // One by one move boundary of unsorted subarray 15 | for (i = 0; i < n-1; i++) { 16 | // Find the minimum element in unsorted array 17 | min_idx = i; 18 | for (j = i+1; j < n; j++) 19 | if (arr[j] < arr[min_idx]) 20 | min_idx = j; 21 | 22 | // Swap the found minimum element with the first element 23 | swap(&arr[min_idx], &arr[i]); 24 | } 25 | } 26 | 27 | // Function to print an array 28 | void printArray(int arr[], int size) { 29 | int i; 30 | for (i=0; i < size; i++) 31 | printf("%d ", arr[i]); 32 | printf("\n"); 33 | } 34 | 35 | // Driver program to test above functions 36 | int main() { 37 | int arr[] = {64, 25, 12, 22, 11}; 38 | int n = sizeof(arr)/sizeof(arr[0]); 39 | selectionSort(arr, n); 40 | printf("Sorted array: \n"); 41 | printArray(arr, n); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /implementation -of stack: -------------------------------------------------------------------------------- 1 | //Linked list implimentation of stack 2 | 3 | 4 | #include 5 | #include 6 | 7 | struct node 8 | { 9 | int info; 10 | struct node *ptr; 11 | }*top,*top1,*temp; 12 | 13 | int topelement(); 14 | void push(int data); 15 | void pop(); 16 | void empty(); 17 | void display(); 18 | void destroy(); 19 | void stack_count(); 20 | void create(); 21 | 22 | int count = 0; 23 | 24 | int main() 25 | { 26 | int no, ch, e; 27 | 28 | printf("\n 1 - Push"); 29 | printf("\n 2 - Pop"); 30 | printf("\n 3 - Top"); 31 | printf("\n 4 - Empty"); 32 | printf("\n 5 - Exit"); 33 | printf("\n 6 - Dipslay"); 34 | printf("\n 7 - Stack Count"); 35 | printf("\n 8 - Destroy stack"); 36 | printf("\n 9-Exit"); 37 | 38 | create(); 39 | 40 | while (1) 41 | { 42 | printf("\n Enter choice : "); 43 | scanf("%d", &ch); 44 | 45 | switch (ch) 46 | { 47 | case 1: 48 | printf("\nEnter data : \n"); 49 | scanf("%d", &no); 50 | push(no); 51 | break; 52 | case 2: 53 | pop(); 54 | break; 55 | case 3: 56 | if (top == NULL) 57 | printf("\nNo elements in stack\n"); 58 | else 59 | { 60 | e = topelement(); 61 | printf("\n Top element : %d\n", e); 62 | } 63 | break; 64 | case 4: 65 | empty(); 66 | break; 67 | case 5: 68 | exit(0); 69 | case 6: 70 | display(); 71 | break; 72 | case 7: 73 | stack_count(); 74 | break; 75 | case 8: 76 | destroy(); 77 | break; 78 | case 9: 79 | exit(0); 80 | default : 81 | printf("\nWrong choice, Please enter correct choice\n"); 82 | break; 83 | } 84 | } 85 | } 86 | 87 | /* Create empty stack */ 88 | void create() 89 | { 90 | top = NULL; 91 | } 92 | 93 | /* Count stack elements */ 94 | void stack_count() 95 | { 96 | printf("\n No. of elements in stack : %d\n", count); 97 | } 98 | 99 | /* Push data into stack */ 100 | void push(int data) 101 | { 102 | if (top == NULL) 103 | { 104 | top =(struct node *)malloc(1*sizeof(struct node)); 105 | top->ptr = NULL; 106 | top->info = data; 107 | } 108 | else 109 | { 110 | temp =(struct node *)malloc(1*sizeof(struct node)); 111 | temp->ptr = top; 112 | temp->info = data; 113 | top = temp; 114 | } 115 | count++; 116 | } 117 | 118 | /* Display stack elements */ 119 | void display() 120 | { 121 | top1 = top; 122 | 123 | if (top1 == NULL) 124 | { 125 | printf("\nStack is empty\n"); 126 | return; 127 | } 128 | 129 | while (top1 != NULL) 130 | { 131 | printf("%d ", top1->info); 132 | top1 = top1->ptr; 133 | } 134 | } 135 | 136 | /* Pop Operation on stack */ 137 | void pop() 138 | { 139 | top1 = top; 140 | 141 | if (top1 == NULL) 142 | { 143 | printf("\n Error : Trying to pop from empty stack\n"); 144 | return; 145 | } 146 | else 147 | top1 = top1->ptr; 148 | printf("\n Popped value : %d", top->info); 149 | free(top); 150 | top = top1; 151 | count--; 152 | } 153 | 154 | /* Return top element */ 155 | int topelement() 156 | { 157 | return(top->info); 158 | } 159 | 160 | /* Check if stack is empty or not */ 161 | void empty() 162 | { 163 | if (top == NULL) 164 | printf("\n Stack is empty\n"); 165 | else 166 | printf("\n Stack is not empty with %d elements\n", count); 167 | } 168 | 169 | /* Destroy entire stack */ 170 | void destroy() 171 | { 172 | top1 = top; 173 | 174 | while (top1 != NULL) 175 | { 176 | top1 = top->ptr; 177 | free(top); 178 | top = top1; 179 | top1 = top1->ptr; 180 | } 181 | free(top1); 182 | top = NULL; 183 | 184 | printf("\n All stack elements destroyed\n"); 185 | count = 0; 186 | } 187 | 188 | 189 | --------------------------------------------------------------------------------