From 9edd9ac19f1b9f8c1a32dad9739bd0cda0d62d21 Mon Sep 17 00:00:00 2001 From: Light Date: Mon, 8 Dec 2025 21:20:15 +0530 Subject: [PATCH] init --- circularQueue.c | 93 +++++++++++++++++++++++++++++++++++++++ linkedStack.c | 54 +++++++++++++++++++++++ linkedlist.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++ poly.c | 57 ++++++++++++++++++++++++ stack.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 431 insertions(+) create mode 100644 circularQueue.c create mode 100644 linkedStack.c create mode 100644 linkedlist.c create mode 100644 poly.c create mode 100644 stack.c diff --git a/circularQueue.c b/circularQueue.c new file mode 100644 index 0000000..16cc309 --- /dev/null +++ b/circularQueue.c @@ -0,0 +1,93 @@ +#include +#include +#include + +typedef struct { + int *keys; + int size; + int front; + int rear; + +} cq; + +cq *createqueue(int size) { + cq *q = (cq *)malloc(sizeof(cq)); + q->size = size; + q->front = -1; + q->rear = -1; + q->keys = (int *)malloc(size * sizeof(int)); + return q; +} + +bool isFull(cq *q) { return ((q->rear + 1) % q->size == q->front); } +bool isEmpty(cq *q) { return q->front == -1; } + +void enqueue(cq *q, int v) { + if (isFull(q)) { + printf("is full"); + } + if (isEmpty(q)) { + q->front = 0; + q->rear = 0; + } else { + q->rear = (q->rear + 1) % q->size; + } + q->keys[q->rear] = v; +} + +int dequeue(cq *q) { + if (isEmpty(q)) { + printf("Queue is empty! Cannot delete.\n"); + return -1; + } + int v = q->keys[q->front]; + if (q->front == q->rear) { + q->front = q->rear = -1; + + } else { + q->front = (q->front + 1) % q->size; + } + return v; +} + +void display(cq *q) { + if (isEmpty(q)) { + printf("Queue is empty!\n"); + return; + } + int i = q->front; + while (1) { + printf("%d", q->keys[i]); + if (i == q->rear) { + break; + } + i = (i + 1) % q->size; + } +} +int main() { + int n; + printf("Enter size of queue: "); + scanf("%d", &n); + + cq *q = createqueue(n); + + enqueue(q, 10); + enqueue(q, 20); + enqueue(q, 30); + display(q); + + dequeue(q); + display(q); + + enqueue(q, 40); + enqueue(q, 50); + display(q); + + dequeue(q); + display(q); + + free(q->keys); + free(q); + + return 0; +} diff --git a/linkedStack.c b/linkedStack.c new file mode 100644 index 0000000..931e5f4 --- /dev/null +++ b/linkedStack.c @@ -0,0 +1,54 @@ +#include +#include +#include + +struct Node { + int data; + struct Node *ptr; +}; + +struct Node *top = NULL; + +void push(int v) { + struct Node *new = (struct Node *)malloc(sizeof(struct Node)); + new->data = v; + new->ptr = top; + top = new; +} + +int pop() { + struct Node *temp = top; + int v = temp->data; + top = temp->ptr; + + free(temp); + return v; +} +int peek() { + if (top == NULL) { + printf("Stack empty\n"); + return -1; + } + return top->data; +} + +void display() { + struct Node *temp = top; + printf("Stack: "); + while (temp != NULL) { + printf("%d ", temp->data); + temp = temp->ptr; + } + printf("\n"); +} +int main() { + push(10); + push(20); + push(30); + display(); + + printf("Popped: %d\n", pop()); + display(); + + printf("Top: %d\n", peek()); +} diff --git a/linkedlist.c b/linkedlist.c new file mode 100644 index 0000000..51ffa01 --- /dev/null +++ b/linkedlist.c @@ -0,0 +1,113 @@ +#include +#include +#include + +struct Node { + struct Node *ptr; + int data; +}; + +struct Node *head = NULL; + +struct Node *createNode(int v) { + struct Node *new = (struct Node *)malloc(sizeof(struct Node)); + new->data = v; + new->ptr = NULL; + return new; +} + +void insertend(int v, struct Node *new) { + if (head == NULL) { + head = new; + return; + } + struct Node *temp = head; + while (temp->ptr != NULL) { + temp = temp->ptr; + } + temp->ptr = new; + new->ptr = NULL; +} + +void insertstart(int v, struct Node *new) { + struct Node *temp = head; + head = new; + new->ptr = temp; +} + +void insert(int v, struct Node *new, int x) { + struct Node *temp = head; + int w = 0; + while (temp->ptr != NULL && w < x) { + temp = temp->ptr; + w++; + } + struct Node *temp2 = temp->ptr; + temp->ptr = new; + new->ptr = temp2; +} +void display() { + struct Node *temp = head; + while (temp != NULL) { + printf("%d \n", temp->data); + temp = temp->ptr; + } +} +void deletestart() { + if (head == NULL) { + printf("list empty"); + return; + } + struct Node *temp = head; + + head = temp->ptr; + free(temp); +} +void deletelast() { + if (head == NULL) { + printf("list empty"); + return; + } else { + struct Node *temp = head; + struct Node *prev = NULL; + while (temp->ptr != NULL) { + prev = temp; + temp = temp->ptr; + } + prev->ptr = NULL; + free(temp); + } +} + +void deletex(int x) { + if (head == NULL) { + printf("list empty"); + return; + } else { + struct Node *temp = head; + struct Node *prev = NULL; + int k = 0; + while (temp->ptr != NULL && k < x - 1) { + prev = temp; + temp = temp->ptr; + k++; + } + // If x is beyond the last node + if (temp == NULL) { + printf("Position %d not found\n", x); + return; + } + prev->ptr = temp->ptr; + free(temp); + } +} +int main() { + struct Node *first = createNode(5); + insertstart(5, first); + display(); + struct Node *second = createNode(6); + insertend(6, second); + display(); + deletelast(); + display(); +} diff --git a/poly.c b/poly.c new file mode 100644 index 0000000..884d19b --- /dev/null +++ b/poly.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +typedef struct { + int top; + int capacity; + char *array; +} stack; + +stack *createStack(int c) { + stack *Stack = (stack *)malloc(sizeof(stack)); + if (!Stack) { + return NULL; + } + Stack->capacity = c; + Stack->top = -1; + Stack->array = (char *)malloc(Stack->capacity); + if (!Stack->array) { + free(Stack); + return NULL; + } + return Stack; +} + +bool isfull(stack *s) { return s->top == s->capacity - 1; } +bool isempty(stack *s) { return s->top == -1; } + +int push(stack *s, char v) { + if (isfull(s)) { + printf("stack overflow"); + return -1; + } + s->array[++s->top] = v; + return s->top; +} +int pop(stack *s) { + if (isempty(s)) { + printf("empty"); + return -1; + } + return s->array[s->top--]; +} + +void display(stack *s) { + for (int i = 0; i <= s->top; i++) { + printf("%c", s->array[i]); + } +} + +int main() { + stack *ms = createStack(3); + + push(ms, 'a'); + push(ms, 'b'); + display(ms); +} diff --git a/stack.c b/stack.c new file mode 100644 index 0000000..d2c8d87 --- /dev/null +++ b/stack.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include +typedef struct { + char key; +} element; +element stack[100]; +int top = -1; +bool isfull() { + if (top == 99) { + return true; + } + return false; +} +bool isempty() { + if (top < 0) { + return true; + } + return false; +} + +void push(char key) { + if (isfull()) { + printf("stack overflow \n"); + return; + } + stack[++top].key = key; +} + +char pop() { + if (isempty()) { + printf("staxk is empyyt"); + return '\0'; + } + return stack[top--].key; +} + +int precedence(char op) { + switch (op) { + case '^': + return 3; + case '*': + case '/': + return 2; + case '+': + case '-': + return 1; + default: + return 0; + } +} +bool isRight(char op) { + if (op == '^') { + return true; + } + return false; +} +char peek() { + if (isempty()) { + printf("stack is empty"); + } + return stack[top].key; +} + +int main() { + char infix[100]; + char postfix[100]; + printf("enter infix\n"); + scanf("%s", infix); + + int k = 0; + int i = 0; + + while (infix[i] != '\0') { + char current = infix[i]; + + if (isalpha(current)) { + postfix[k++] = current; + } else if (current == '(') { + push(current); + } else if (current == ')') { + while (!isempty() && peek() != '(') { + postfix[k++] = pop(); + } + if (!isempty() && peek() == '(') { + pop(); // Remove '(' from stack + } + } else { // Operator + int currPrec = precedence(current); + while (!isempty() && peek() != '(') { + char topOp = peek(); + int topPrec = precedence(topOp); + + if (topPrec > currPrec || (topPrec == currPrec && !isRight(current))) { + postfix[k++] = pop(); + } else { + break; + } + } + push(current); + } + i++; + } + + while (!isempty()) { + postfix[k++] = pop(); + } + + postfix[k] = '\0'; + printf("Postfix: %s\n", postfix); + + return 0; +}