├── Linear Search.c ├── Tower of Hanoi.c ├── Insertion Sort.c ├── Bubble Sort.c ├── Selection Sort.c ├── Binary Search.c ├── Quick Sort.c ├── Push and Pop on Stack.c ├── Insert a node in a linked list.c ├── Merge sort.c ├── Stack Implementation.c ├── Insert values in a queue implemented through array.c ├── Implementation of Binary Tree.c ├── Insert element in circular queue in linked list.c ├── Algorithm to delete an element from a queue.c ├── Implement Queue Using Circular array.c ├── Infix to postfix.c └── LICENSE /Linear Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int linearSearch(int arr[], int n, int key) { 4 | for (int i = 0; i < n; i++) { 5 | if (arr[i] == key) 6 | return i; 7 | } 8 | return -1; 9 | } 10 | 11 | int main() { 12 | int arr[] = {2, 4, 7, 9, 12, 15, 19, 23}; 13 | int n = sizeof(arr) / sizeof(arr[0]); 14 | int key = 12; 15 | 16 | int result = linearSearch(arr, n, key); 17 | 18 | if (result != -1) 19 | printf("%d found at index %d.\n", key, result); 20 | else 21 | printf("%d not found.\n", key); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Tower of Hanoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) { 4 | if (n == 1) { 5 | printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod); 6 | return; 7 | } 8 | towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); 9 | printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod); 10 | towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); 11 | } 12 | 13 | int main() { 14 | int n; 15 | printf("Enter the number of disks: "); 16 | scanf("%d", &n); 17 | 18 | towerOfHanoi(n, 'A', 'C', 'B'); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Insertion Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void insertionSort(int arr[], int n) { 4 | int i, key, j; 5 | for (i = 1; i < n; i++) { 6 | key = arr[i]; 7 | j = i - 1; 8 | 9 | while (j >= 0 && arr[j] > key) { 10 | arr[j + 1] = arr[j]; 11 | j = j - 1; 12 | } 13 | arr[j + 1] = key; 14 | } 15 | } 16 | 17 | int main() { 18 | int arr[] = {64, 25, 12, 22, 11}; 19 | int n = sizeof(arr) / sizeof(arr[0]); 20 | 21 | insertionSort(arr, n); 22 | 23 | printf("Sorted array: "); 24 | for (int i = 0; i < n; i++) 25 | printf("%d ", arr[i]); 26 | printf("\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Bubble Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void bubbleSort(int arr[], int n) { 4 | for (int i = 0; i < n - 1; i++) { 5 | for (int j = 0; j < n - i - 1; j++) { 6 | if (arr[j] > arr[j + 1]) { 7 | int temp = arr[j]; 8 | arr[j] = arr[j + 1]; 9 | arr[j + 1] = temp; 10 | } 11 | } 12 | } 13 | } 14 | 15 | int main() { 16 | int arr[] = {64, 25, 12, 22, 11}; 17 | int n = sizeof(arr) / sizeof(arr[0]); 18 | 19 | bubbleSort(arr, n); 20 | 21 | printf("Sorted array: "); 22 | for (int i = 0; i < n; i++) 23 | printf("%d ", arr[i]); 24 | printf("\n"); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Selection Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void selectionSort(int arr[], int n) { 4 | int i, j, min_idx; 5 | for (i = 0; i < n - 1; i++) { 6 | min_idx = i; 7 | for (j = i + 1; j < n; j++) { 8 | if (arr[j] < arr[min_idx]) 9 | min_idx = j; 10 | } 11 | int temp = arr[min_idx]; 12 | arr[min_idx] = arr[i]; 13 | arr[i] = temp; 14 | } 15 | } 16 | 17 | int main() { 18 | int arr[] = {64, 25, 12, 22, 11}; 19 | int n = sizeof(arr) / sizeof(arr[0]); 20 | 21 | selectionSort(arr, n); 22 | 23 | printf("Sorted array: "); 24 | for (int i = 0; i < n; i++) 25 | printf("%d ", arr[i]); 26 | printf("\n"); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Binary Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int binarySearch(int arr[], int l, int r, int key) { 4 | while (l <= r) { 5 | int mid = l + (r - l) / 2; 6 | if (arr[mid] == key) 7 | return mid; 8 | if (arr[mid] < key) 9 | l = mid + 1; 10 | else 11 | r = mid - 1; 12 | } 13 | return -1; 14 | } 15 | 16 | int main() { 17 | int arr[] = {2, 4, 7, 9, 12, 15, 19, 23}; 18 | int n = sizeof(arr) / sizeof(arr[0]); 19 | int key = 12; 20 | 21 | int result = binarySearch(arr, 0, n - 1, key); 22 | 23 | if (result != -1) 24 | printf("%d found at index %d.\n", key, result); 25 | else 26 | printf("%d not found.\n", key); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Quick Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int* a, int* b) { 4 | int t = *a; 5 | *a = *b; 6 | *b = t; 7 | } 8 | 9 | int partition(int arr[], int low, int high) { 10 | int pivot = arr[high]; 11 | int i = (low - 1); 12 | 13 | for (int j = low; j <= high - 1; j++) { 14 | if (arr[j] < pivot) { 15 | i++; 16 | swap(&arr[i], &arr[j]); 17 | } 18 | } 19 | swap(&arr[i + 1], &arr[high]); 20 | return (i + 1); 21 | } 22 | 23 | void quickSort(int arr[], int low, int high) { 24 | if (low < high) { 25 | int pi = partition(arr, low, high); 26 | 27 | quickSort(arr, low, pi - 1); 28 | quickSort(arr, pi + 1, high); 29 | } 30 | } 31 | 32 | int main() { 33 | int arr[] = {10, 7, 8, 9, 1, 5}; 34 | int n = sizeof(arr) / sizeof(arr[0]); 35 | 36 | quickSort(arr, 0, n - 1); 37 | 38 | printf("Sorted array: "); 39 | for (int i = 0; i < n; i++) 40 | printf("%d ", arr[i]); 41 | printf("\n"); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Push and Pop on Stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 100 5 | 6 | struct Stack { 7 | int items[MAX_SIZE]; 8 | int top; 9 | }; 10 | 11 | void initStack(struct Stack* stack) { 12 | stack->top = -1; 13 | } 14 | 15 | int isEmpty(struct Stack* stack) { 16 | return stack->top == -1; 17 | } 18 | 19 | int isFull(struct Stack* stack) { 20 | return stack->top == MAX_SIZE - 1; 21 | } 22 | 23 | void push(struct Stack* stack, int value) { 24 | if (isFull(stack)) { 25 | printf("Stack overflow\n"); 26 | return; 27 | } 28 | stack->top++; 29 | stack->items[stack->top] = value; 30 | printf("Pushed %d onto the stack.\n", value); 31 | } 32 | 33 | int pop(struct Stack* stack) { 34 | if (isEmpty(stack)) { 35 | printf("Stack underflow\n"); 36 | exit(1); 37 | } 38 | int poppedItem = stack->items[stack->top]; 39 | stack->top--; 40 | return poppedItem; 41 | } 42 | 43 | int main() { 44 | struct Stack stack; 45 | initStack(&stack); 46 | 47 | push(&stack, 1); 48 | push(&stack, 2); 49 | push(&stack, 3); 50 | 51 | printf("Popped %d from the stack.\n", pop(&stack)); 52 | printf("Popped %d from the stack.\n", pop(&stack)); 53 | printf("Popped %d from the stack.\n", pop(&stack)); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Insert a node in a linked list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Node* createNode(int data) { 10 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 11 | if (newNode == NULL) { 12 | printf("Memory allocation failed\n"); 13 | exit(1); 14 | } 15 | newNode->data = data; 16 | newNode->next = NULL; 17 | return newNode; 18 | } 19 | 20 | void insertEnd(struct Node** head, int data) { 21 | struct Node* newNode = createNode(data); 22 | if (*head == NULL) { 23 | *head = newNode; 24 | return; 25 | } 26 | struct Node* temp = *head; 27 | while (temp->next != NULL) { 28 | temp = temp->next; 29 | } 30 | temp->next = newNode; 31 | } 32 | 33 | 34 | void displayList(struct Node* head) { 35 | struct Node* temp = head; 36 | while (temp != NULL) { 37 | printf("%d ", temp->data); 38 | temp = temp->next; 39 | } 40 | printf("\n"); 41 | } 42 | 43 | int main() { 44 | struct Node* head = NULL; 45 | 46 | 47 | insertEnd(&head, 1); 48 | insertEnd(&head, 2); 49 | insertEnd(&head, 3); 50 | insertEnd(&head, 4); 51 | insertEnd(&head, 5); 52 | 53 | printf("Linked list: "); 54 | displayList(head); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Merge sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void merge(int arr[], int l, int m, int r) { 4 | int i, j, k; 5 | int n1 = m - l + 1; 6 | int n2 = r - m; 7 | 8 | int L[n1], R[n2]; 9 | 10 | for (i = 0; i < n1; i++) 11 | L[i] = arr[l + i]; 12 | for (j = 0; j < n2; j++) 13 | R[j] = arr[m + 1 + j]; 14 | 15 | i = 0; 16 | j = 0; 17 | k = l; 18 | while (i < n1 && j < n2) { 19 | if (L[i] <= R[j]) { 20 | arr[k] = L[i]; 21 | i++; 22 | } else { 23 | arr[k] = R[j]; 24 | j++; 25 | } 26 | k++; 27 | } 28 | 29 | while (i < n1) { 30 | arr[k] = L[i]; 31 | i++; 32 | k++; 33 | } 34 | 35 | while (j < n2) { 36 | arr[k] = R[j]; 37 | j++; 38 | k++; 39 | } 40 | } 41 | 42 | void mergeSort(int arr[], int l, int r) { 43 | if (l < r) { 44 | int m = l + (r - l) / 2; 45 | 46 | mergeSort(arr, l, m); 47 | mergeSort(arr, m + 1, r); 48 | 49 | merge(arr, l, m, r); 50 | } 51 | } 52 | 53 | int main() { 54 | int arr[] = {12, 11, 13, 5, 6, 7}; 55 | int n = sizeof(arr) / sizeof(arr[0]); 56 | 57 | mergeSort(arr, 0, n - 1); 58 | 59 | printf("Sorted array: "); 60 | for (int i = 0; i < n; i++) 61 | printf("%d ", arr[i]); 62 | printf("\n"); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Stack Implementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 100 5 | 6 | struct Stack { 7 | int items[MAX_SIZE]; 8 | int top; 9 | }; 10 | 11 | void initStack(struct Stack* stack) { 12 | stack->top = -1; 13 | } 14 | 15 | int isEmpty(struct Stack* stack) { 16 | return stack->top == -1; 17 | } 18 | 19 | int isFull(struct Stack* stack) { 20 | return stack->top == MAX_SIZE - 1; 21 | } 22 | 23 | void push(struct Stack* stack, int value) { 24 | if (isFull(stack)) { 25 | printf("Stack overflow\n"); 26 | return; 27 | } 28 | stack->top++; 29 | stack->items[stack->top] = value; 30 | } 31 | 32 | int pop(struct Stack* stack) { 33 | if (isEmpty(stack)) { 34 | printf("Stack underflow\n"); 35 | exit(1); 36 | } 37 | return stack->items[stack->top--]; 38 | } 39 | 40 | int peek(struct Stack* stack) { 41 | if (isEmpty(stack)) { 42 | printf("Stack is empty\n"); 43 | exit(1); 44 | } 45 | return stack->items[stack->top]; 46 | } 47 | 48 | int main() { 49 | struct Stack stack; 50 | initStack(&stack); 51 | 52 | push(&stack, 1); 53 | push(&stack, 2); 54 | push(&stack, 3); 55 | 56 | printf("Top element: %d\n", peek(&stack)); 57 | 58 | printf("Elements: "); 59 | while (!isEmpty(&stack)) 60 | printf("%d ", pop(&stack)); 61 | printf("\n"); 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Insert values in a queue implemented through array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 100 5 | 6 | struct Queue { 7 | int items[MAX_SIZE]; 8 | int front; 9 | int rear; 10 | }; 11 | 12 | void initQueue(struct Queue* q) { 13 | q->front = -1; 14 | q->rear = -1; 15 | } 16 | 17 | int isEmpty(struct Queue* q) { 18 | return (q->rear == -1 && q->front == -1); 19 | } 20 | 21 | int isFull(struct Queue* q) { 22 | return q->rear == MAX_SIZE - 1; 23 | } 24 | 25 | void enqueue(struct Queue* q, int value) { 26 | if (isFull(q)) { 27 | printf("Queue is full\n"); 28 | return; 29 | } 30 | if (isEmpty(q)) { 31 | q->front = q->rear = 0; 32 | } else { 33 | q->rear++; 34 | } 35 | q->items[q->rear] = value; 36 | printf("%d enqueued to the queue\n", value); 37 | } 38 | 39 | int dequeue(struct Queue* q) { 40 | if (isEmpty(q)) { 41 | printf("Queue is empty\n"); 42 | exit(1); 43 | } 44 | int dequeuedItem = q->items[q->front]; 45 | if (q->front == q->rear) { 46 | q->front = q->rear = -1; 47 | } else { 48 | q->front++; 49 | } 50 | return dequeuedItem; 51 | } 52 | 53 | int main() { 54 | struct Queue q; 55 | initQueue(&q); 56 | 57 | enqueue(&q, 1); 58 | enqueue(&q, 2); 59 | enqueue(&q, 3); 60 | 61 | printf("%d dequeued from the queue\n", dequeue(&q)); 62 | printf("%d dequeued from the queue\n", dequeue(&q)); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Implementation of Binary Tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | struct Node { 6 | int data; 7 | struct Node* left; 8 | struct Node* right; 9 | }; 10 | 11 | struct Node* createNode(int data) { 12 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 13 | if (newNode == NULL) { 14 | printf("Memory allocation failed\n"); 15 | exit(1); 16 | } 17 | newNode->data = data; 18 | newNode->left = NULL; 19 | newNode->right = NULL; 20 | return newNode; 21 | } 22 | 23 | struct Node* insert(struct Node* root, int data) { 24 | if (root == NULL) { 25 | return createNode(data); 26 | } 27 | if (data < root->data) { 28 | root->left = insert(root->left, data); 29 | } else if (data > root->data) { 30 | root->right = insert(root->right, data); 31 | } 32 | return root; 33 | } 34 | 35 | void inorderTraversal(struct Node* root) { 36 | if (root != NULL) { 37 | inorderTraversal(root->left); 38 | printf("%d ", root->data); 39 | inorderTraversal(root->right); 40 | } 41 | } 42 | 43 | int main() { 44 | struct Node* root = NULL; 45 | 46 | root = insert(root, 10); 47 | root = insert(root, 5); 48 | root = insert(root, 15); 49 | root = insert(root, 3); 50 | root = insert(root, 7); 51 | root = insert(root, 12); 52 | root = insert(root, 17); 53 | 54 | printf("Inorder traversal: "); 55 | inorderTraversal(root); 56 | printf("\n"); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Insert element in circular queue in linked list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Queue { 10 | struct Node* front; 11 | struct Node* rear; 12 | }; 13 | 14 | struct Queue* createQueue() { 15 | struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue)); 16 | q->front = NULL; 17 | q->rear = NULL; 18 | return q; 19 | } 20 | 21 | int isEmpty(struct Queue* q) { 22 | return q->front == NULL; 23 | } 24 | 25 | void enqueue(struct Queue* q, int value) { 26 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 27 | newNode->data = value; 28 | newNode->next = NULL; 29 | if (isEmpty(q)) { 30 | q->front = newNode; 31 | } else { 32 | q->rear->next = newNode; 33 | } 34 | q->rear = newNode; 35 | q->rear->next = q->front; // Circular linking 36 | printf("%d enqueued to the queue\n", value); 37 | } 38 | 39 | void displayQueue(struct Queue* q) { 40 | if (isEmpty(q)) { 41 | printf("Queue is empty\n"); 42 | return; 43 | } 44 | struct Node* temp = q->front; 45 | printf("Queue: "); 46 | do { 47 | printf("%d ", temp->data); 48 | temp = temp->next; 49 | } while (temp != q->front); 50 | printf("\n"); 51 | } 52 | 53 | int main() { 54 | struct Queue* q = createQueue(); 55 | 56 | enqueue(q, 1); 57 | enqueue(q, 2); 58 | enqueue(q, 3); 59 | enqueue(q, 4); 60 | enqueue(q, 5); 61 | 62 | displayQueue(q); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Algorithm to delete an element from a queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 100 5 | 6 | struct Queue { 7 | int items[MAX_SIZE]; 8 | int front; 9 | int rear; 10 | }; 11 | 12 | void initQueue(struct Queue* q) { 13 | q->front = -1; 14 | q->rear = -1; 15 | } 16 | 17 | int isEmpty(struct Queue* q) { 18 | return (q->rear == -1 && q->front == -1); 19 | } 20 | 21 | int isFull(struct Queue* q) { 22 | return q->rear == MAX_SIZE - 1; 23 | } 24 | 25 | void enqueue(struct Queue* q, int value) { 26 | if (isFull(q)) { 27 | printf("Queue is full\n"); 28 | return; 29 | } 30 | if (isEmpty(q)) { 31 | q->front = q->rear = 0; 32 | } else { 33 | q->rear++; 34 | } 35 | q->items[q->rear] = value; 36 | printf("%d enqueued to the queue\n", value); 37 | } 38 | 39 | int dequeue(struct Queue* q) { 40 | if (isEmpty(q)) { 41 | printf("Queue is empty\n"); 42 | exit(1); 43 | } 44 | int dequeuedItem = q->items[q->front]; 45 | if (q->front == q->rear) { 46 | q->front = q->rear = -1; 47 | } else { 48 | q->front++; 49 | } 50 | return dequeuedItem; 51 | } 52 | 53 | void deleteNElements(struct Queue* q, int n) { 54 | if (isEmpty(q)) { 55 | printf("Queue is empty\n"); 56 | return; 57 | } 58 | if (n > (q->rear - q->front + 1)) { 59 | printf("Not enough elements to delete\n"); 60 | return; 61 | } 62 | for (int i = 0; i < n; i++) { 63 | dequeue(q); 64 | } 65 | printf("%d elements deleted from the queue\n", n); 66 | } 67 | 68 | int main() { 69 | struct Queue q; 70 | initQueue(&q); 71 | 72 | enqueue(&q, 1); 73 | enqueue(&q, 2); 74 | enqueue(&q, 3); 75 | enqueue(&q, 4); 76 | enqueue(&q, 5); 77 | 78 | deleteNElements(&q, 3); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Implement Queue Using Circular array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 5 5 | 6 | struct Queue { 7 | int items[MAX_SIZE]; 8 | int front, rear; 9 | unsigned int size; 10 | }; 11 | 12 | void initQueue(struct Queue* q) { 13 | q->front = -1; 14 | q->rear = -1; 15 | q->size = 0; 16 | } 17 | 18 | int isEmpty(struct Queue* q) { 19 | return q->size == 0; 20 | } 21 | 22 | int isFull(struct Queue* q) { 23 | return q->size == MAX_SIZE; 24 | } 25 | 26 | void enqueue(struct Queue* q, int value) { 27 | if (isFull(q)) { 28 | printf("Queue is full\n"); 29 | return; 30 | } 31 | if (isEmpty(q)) { 32 | q->front = 0; 33 | } 34 | q->rear = (q->rear + 1) % MAX_SIZE; 35 | q->items[q->rear] = value; 36 | q->size++; 37 | printf("%d enqueued to the queue\n", value); 38 | } 39 | 40 | int dequeue(struct Queue* q) { 41 | if (isEmpty(q)) { 42 | printf("Queue is empty\n"); 43 | exit(1); 44 | } 45 | int dequeuedItem = q->items[q->front]; 46 | if (q->front == q->rear) { 47 | q->front = -1; 48 | q->rear = -1; 49 | } else { 50 | q->front = (q->front + 1) % MAX_SIZE; 51 | } 52 | q->size--; 53 | return dequeuedItem; 54 | } 55 | 56 | int main() { 57 | struct Queue q; 58 | initQueue(&q); 59 | 60 | enqueue(&q, 1); 61 | enqueue(&q, 2); 62 | enqueue(&q, 3); 63 | enqueue(&q, 4); 64 | enqueue(&q, 5); 65 | 66 | printf("%d dequeued from the queue\n", dequeue(&q)); 67 | printf("%d dequeued from the queue\n", dequeue(&q)); 68 | 69 | enqueue(&q, 6); 70 | enqueue(&q, 7); 71 | 72 | printf("%d dequeued from the queue\n", dequeue(&q)); 73 | printf("%d dequeued from the queue\n", dequeue(&q)); 74 | printf("%d dequeued from the queue\n", dequeue(&q)); 75 | printf("%d dequeued from the queue\n", dequeue(&q)); 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /Infix to postfix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define MAX_SIZE 100 7 | 8 | struct Stack { 9 | char items[MAX_SIZE]; 10 | int top; 11 | }; 12 | 13 | void initStack(struct Stack* stack) { 14 | stack->top = -1; 15 | } 16 | 17 | int isEmpty(struct Stack* stack) { 18 | return stack->top == -1; 19 | } 20 | 21 | int isFull(struct Stack* stack) { 22 | return stack->top == MAX_SIZE - 1; 23 | } 24 | 25 | void push(struct Stack* stack, char value) { 26 | if (isFull(stack)) { 27 | printf("Stack overflow\n"); 28 | exit(1); 29 | } 30 | stack->top++; 31 | stack->items[stack->top] = value; 32 | } 33 | 34 | char pop(struct Stack* stack) { 35 | if (isEmpty(stack)) { 36 | printf("Stack underflow\n"); 37 | exit(1); 38 | } 39 | return stack->items[stack->top--]; 40 | } 41 | 42 | int precedence(char ch) { 43 | if (ch == '+' || ch == '-') 44 | return 1; 45 | else if (ch == '*' || ch == '/') 46 | return 2; 47 | return 0; 48 | } 49 | 50 | void infixToPostfix(char* infix, char* postfix) { 51 | struct Stack stack; 52 | initStack(&stack); 53 | int i = 0, j = 0; 54 | 55 | while (infix[i] != '\0') { 56 | if (isdigit(infix[i]) || isalpha(infix[i])) { 57 | postfix[j++] = infix[i]; 58 | } else if (infix[i] == '(') { 59 | push(&stack, infix[i]); 60 | } else if (infix[i] == ')') { 61 | while (!isEmpty(&stack) && stack.items[stack.top] != '(') { 62 | postfix[j++] = pop(&stack); 63 | } 64 | if (!isEmpty(&stack) && stack.items[stack.top] != '(') { 65 | printf("Invalid expression\n"); 66 | exit(1); 67 | } else { 68 | pop(&stack); 69 | } 70 | } else { 71 | while (!isEmpty(&stack) && precedence(infix[i]) <= precedence(stack.items[stack.top])) { 72 | postfix[j++] = pop(&stack); 73 | } 74 | push(&stack, infix[i]); 75 | } 76 | i++; 77 | } 78 | 79 | while (!isEmpty(&stack)) { 80 | postfix[j++] = pop(&stack); 81 | } 82 | postfix[j] = '\0'; 83 | } 84 | 85 | int main() { 86 | char infix[MAX_SIZE]; 87 | printf("Enter infix expression: "); 88 | scanf("%s", infix); 89 | 90 | char postfix[MAX_SIZE]; 91 | infixToPostfix(infix, postfix); 92 | printf("Postfix expression: %s\n", postfix); 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------