From a033c5175f06d47c71b3520f757921ab405e9017 Mon Sep 17 00:00:00 2001 From: aadit Date: Mon, 5 Aug 2024 01:10:12 +0530 Subject: [PATCH] Upload files to "DS/C/Lab/Week3" --- DS/C/Lab/Week3/DecBaseCon.c | 52 ++++++++++++++++++++++++++++++++ DS/C/Lab/Week3/MulSta.c | 45 +++++++++++++++++++++++++++ DS/C/Lab/Week3/Parenthesis.c | 51 +++++++++++++++++++++++++++++++ DS/C/Lab/Week3/StackPalindrome.c | 46 ++++++++++++++++++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 DS/C/Lab/Week3/DecBaseCon.c create mode 100644 DS/C/Lab/Week3/MulSta.c create mode 100644 DS/C/Lab/Week3/Parenthesis.c create mode 100644 DS/C/Lab/Week3/StackPalindrome.c diff --git a/DS/C/Lab/Week3/DecBaseCon.c b/DS/C/Lab/Week3/DecBaseCon.c new file mode 100644 index 0000000..63a33a7 --- /dev/null +++ b/DS/C/Lab/Week3/DecBaseCon.c @@ -0,0 +1,52 @@ +#include +#define MAX_SIZE 100 + +int top = -1; +char stack[MAX_SIZE]; + +void push(char c) { + if (top < MAX_SIZE - 1) { + stack[++top] = c; + } +} + +char pop() { + if (top >= 0) { + return stack[top--]; + } + return '\0'; +} + +void decimal_to_base(int decimal, int base) { + char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + while (decimal > 0) { + push(digits[decimal % base]); + decimal /= base; + } + + printf("Converted number: "); + while (top >= 0) { + printf("%c", pop()); + } + printf("\n"); +} + +int main() { + int decimal, base; + + printf("Enter a decimal number: "); + scanf("%d", &decimal); + + printf("Enter the base to convert to (2-36): "); + scanf("%d", &base); + + if (base < 2 || base > 36) { + printf("Invalid base. Please enter a base between 2 and 36.\n"); + return 1; + } + + decimal_to_base(decimal, base); + + return 0; +} diff --git a/DS/C/Lab/Week3/MulSta.c b/DS/C/Lab/Week3/MulSta.c new file mode 100644 index 0000000..3e14b18 --- /dev/null +++ b/DS/C/Lab/Week3/MulSta.c @@ -0,0 +1,45 @@ +#include +#define MAX_SIZE 100 +#define NUM_STACKS 3 + +int tops[NUM_STACKS] = {-1, -1, -1}; +int stack[MAX_SIZE]; +int stack_size = MAX_SIZE / NUM_STACKS; + +void push(int stack_num, int value) { + int index = stack_num * stack_size + tops[stack_num] + 1; + if (tops[stack_num] < stack_size - 1) { + tops[stack_num]++; + stack[index] = value; + printf("Pushed %d to stack %d\n", value, stack_num); + } else { + printf("Stack %d is full\n", stack_num); + } +} + +int pop(int stack_num) { + int index = stack_num * stack_size + tops[stack_num]; + if (tops[stack_num] >= 0) { + int value = stack[index]; + tops[stack_num]--; + printf("Popped %d from stack %d\n", value, stack_num); + return value; + } else { + printf("Stack %d is empty\n", stack_num); + return -1; + } +} + +int main() { + push(0, 10); + push(0, 20); + push(1, 30); + push(2, 40); + + pop(0); + pop(1); + pop(2); + pop(2); + + return 0; +} diff --git a/DS/C/Lab/Week3/Parenthesis.c b/DS/C/Lab/Week3/Parenthesis.c new file mode 100644 index 0000000..fa2880b --- /dev/null +++ b/DS/C/Lab/Week3/Parenthesis.c @@ -0,0 +1,51 @@ +#include +#define MAX_SIZE 100 + +char stack[MAX_SIZE]; +int top = -1; + +void push(char c) { + if (top < MAX_SIZE - 1) { + stack[++top] = c; + } +} + +char pop() { + if (top >= 0) { + return stack[top--]; + } + return '\0'; +} + +int is_matching_pair(char opening, char closing) { + return (opening == '(' && closing == ')') || + (opening == '[' && closing == ']') || + (opening == '{' && closing == '}'); +} + +int are_parentheses_balanced(char expr[]) { + int i; + for (i = 0; expr[i] != '\0'; i++) { + if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{') { + push(expr[i]); + } else if (expr[i] == ')' || expr[i] == ']' || expr[i] == '}') { + if (top == -1 || !is_matching_pair(pop(), expr[i])) { + return 0; + } + } + } + return (top == -1); + +int main() { + char expr[MAX_SIZE]; + printf("Enter an expression: "); + scanf("%s", expr); + + if (are_parentheses_balanced(expr)) { + printf("Parentheses are balanced\n"); + } else { + printf("Parentheses are not balanced\n"); + } + + return 0; +} diff --git a/DS/C/Lab/Week3/StackPalindrome.c b/DS/C/Lab/Week3/StackPalindrome.c new file mode 100644 index 0000000..7ec7007 --- /dev/null +++ b/DS/C/Lab/Week3/StackPalindrome.c @@ -0,0 +1,46 @@ +#include +#define MAX_SIZE 100 + +int top = -1; +char stack[MAX_SIZE]; + +void push(char c) { + if (top < MAX_SIZE - 1) { + stack[++top] = c; + } +} + +char pop() { + if (top >= 0) { + return stack[top--]; + } + return '\0'; +} + +int is_palindrome(char *str) { + int i; + for (i = 0; str[i] != '\0'; i++) { + push(str[i]); + } + + for (i = 0; str[i] != '\0'; i++) { + if (str[i] != pop()) { + return 0; + } + } + return 1; +} + +int main() { + char str[MAX_SIZE]; + printf("Enter a string: "); + scanf("%s", str); + + if (is_palindrome(str)) { + printf("%s is a palindrome.\n", str); + } else { + printf("%s is not a palindrome.\n", str); + } + + return 0; +}