diff --git a/C/CircularQueue.c b/C/CircularQueue.c new file mode 100644 index 0000000..f9d9bce --- /dev/null +++ b/C/CircularQueue.c @@ -0,0 +1,114 @@ +// Circular Queue implementation in C + +#include + +#define SIZE 5 + +int items[SIZE]; +int front = -1, rear = -1; + +// Check if the queue is full +int isFull() +{ + if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) + return 1; + return 0; +} + +// Check if the queue is empty +int isEmpty() +{ + if (front == -1) + return 1; + return 0; +} + +// Adding an element +void enQueue(int element) +{ + if (isFull()) + printf("\n Queue is full!! \n"); + else + { + if (front == -1) + front = 0; + rear = (rear + 1) % SIZE; + items[rear] = element; + printf("\n Inserted -> %d", element); + } +} + +// Removing an element +int deQueue() +{ + int element; + if (isEmpty()) + { + printf("\n Queue is empty !! \n"); + return (-1); + } + else + { + element = items[front]; + if (front == rear) + { + front = -1; + rear = -1; + } + // Q has only one element, so we reset the + // queue after dequeing it. ? + else + { + front = (front + 1) % SIZE; + } + printf("\n Deleted element -> %d \n", element); + return (element); + } +} + +// Display the queue +void display() +{ + int i; + if (isEmpty()) + printf(" \n Empty Queue\n"); + else + { + printf("\n Front -> %d ", front); + printf("\n Items -> "); + for (i = front; i != rear; i = (i + 1) % SIZE) + { + printf("%d ", items[i]); + } + printf("%d ", items[i]); + printf("\n Rear -> %d \n", rear); + } +} + +int main() +{ + // Fails because front = -1 + deQueue(); + + enQueue(1); + enQueue(2); + enQueue(3); + enQueue(4); + enQueue(5); + + // Fails to enqueue because front == 0 && rear == SIZE - 1 + enQueue(6); + + display(); + deQueue(); + + display(); + + enQueue(7); + display(); + + // Fails to enqueue because front == rear + 1 + enQueue(8); + + return 0; +} \ No newline at end of file diff --git a/C/Stack.c b/C/Stack.c new file mode 100644 index 0000000..0d42d36 --- /dev/null +++ b/C/Stack.c @@ -0,0 +1,153 @@ +#include +#include +#define N 10 +int stack[N]; + +int top = -1; + +void push() +{ + + int x; + + printf("Enter the Data: \n"); + + scanf("%d", &x); + + if (top == N - 1) + + { + + printf("Stack Overflow\n"); + } + + else + + { + + top++; + + stack[top] = x; + } +} + +void pop() +{ + + int item; + + if (top == -1) + + { + + printf("Stack Underflow\n"); + } + + else + + { + + item = stack[top]; + + top--; + + printf("The popped element is %d: \n", item); + } +} + +void display() +{ + + int i; + + for (i = top; i >= 0; i++) + + { + + printf("%d \n", stack[i]); + } +} + +void peek() +{ + + if (top == -1) + + { + + printf("Stack is Empty\n"); + } + + else + + { + + printf("The top element is: %d\n", stack[top]); + } +} + +void main() +{ + + int choice; + + printf("Enter your Choice: \n0:Exit\n1:Push\n2:Pop\n3:Peek\n4:Display\n"); + + do + + { + + scanf("%d", &choice); + + switch (choice) + + { + + case 1: + + { + + push(); + + break; + } + + case 2: + + { + + pop(); + + break; + } + + case 3: + + { + + peek(); + + break; + } + + case 4: + + { + + display(); + + break; + } + + default: + + { + + printf("Invalid Choice\n"); + + break; + } + } + + } while (choice != 0); +}