From daa429e9612a5f8506a1fd4746c0fe7a9a2ab2c9 Mon Sep 17 00:00:00 2001 From: "akash.patil" Date: 2025年2月19日 22:38:14 +0530 Subject: [PATCH] Update stack_implementation_using_array.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Const correctness: Functions like display(), peek(), isEmpty(), and isFull() use const for read-only operations. ✅ Memory safety: destroy() ensures no memory leaks. ✅ Error handling: Proper bounds checking and error messages. ✅ More readable & efficient code structure. --- Stack/stack_implementation_using_array.c | 124 +++++++++++++---------- 1 file changed, 72 insertions(+), 52 deletions(-) diff --git a/Stack/stack_implementation_using_array.c b/Stack/stack_implementation_using_array.c index 5d54605a..5035d6ec 100644 --- a/Stack/stack_implementation_using_array.c +++ b/Stack/stack_implementation_using_array.c @@ -1,79 +1,99 @@ #include #include -struct Stack -{ + +struct Stack { int size; int top; int *S; }; -void create(struct Stack *st) -{ - printf("Enter Size"); + +// Create the stack +void create(struct Stack *st) { + printf("Enter Stack Size: "); scanf("%d", &st->size); st->top = -1; st->S = (int *)malloc(st->size * sizeof(int)); + if (!st->S) { + fprintf(stderr, "Memory allocation failed!\n"); + exit(EXIT_FAILURE); + } } -void Display(struct Stack st) -{ - int i; - for (i = st.top; i>= 0; i--) - printf("%d ", st.S[i]); + +// Display stack elements +void display(const struct Stack *st) { + if (st->top == -1) { + printf("Stack is empty\n"); + return; + } + for (int i = st->top; i>= 0; i--) + printf("%d ", st->S[i]); printf("\n"); } -void push(struct Stack *st, int x) -{ - if (st->top == st->size - 1) - printf("Stack overflow\n"); - else - { - st->top++; - st->S[st->top] = x; + +// Push an element onto the stack +void push(struct Stack *st, int x) { + if (st->top == st->size - 1) { + fprintf(stderr, "Stack Overflow\n"); + return; + } + st->S[++st->top] = x; +} + +// Pop an element from the stack +int pop(struct Stack *st) { + if (st->top == -1) { + fprintf(stderr, "Stack Underflow\n"); + return -1; } + return st->S[st->top--]; } -int pop(struct Stack *st) -{ - int x = -1; - if (st->top == -1) - printf("Stack Underflow\n"); - else - { - x = st->S[st->top--]; + +// Peek at a specific position from the top +int peek(const struct Stack *st, int index) { + int pos = st->top - index + 1; + if (pos < 0 || pos> st->top) { + fprintf(stderr, "Invalid Index\n"); + return -1; } - return x; + return st->S[pos]; } -int peek(struct Stack st, int index) -{ - int x = -1; - if (st.top - index + 1 < 0) - printf("Invalid Index \n"); - x = st.S[st.top - index + 1]; - return x; + +// Check if the stack is empty +int isEmpty(const struct Stack *st) { + return st->top == -1; } -int isEmpty(struct Stack st) -{ - if (st.top == -1) - return 1; - return 0; + +// Check if the stack is full +int isFull(const struct Stack *st) { + return st->top == st->size - 1; } -int isFull(struct Stack st) -{ - return st.top == st.size - 1; + +// Get the top element of the stack +int stackTop(const struct Stack *st) { + return isEmpty(st) ? -1 : st->S[st->top]; } -int stackTop(struct Stack st) -{ - if (!isEmpty(st)) - return st.S[st.top]; - return -1; + +// Free allocated memory +void destroy(struct Stack *st) { + free(st->S); + st->S = NULL; + st->size = st->top = 0; } -int main() -{ + +int main() { struct Stack st; create(&st); + push(&st, 10); push(&st, 20); push(&st, 30); push(&st, 40); - printf("%d \n", peek(st, 2)); - Display(st); + + printf("Peek at index 2: %d\n", peek(&st, 2)); + + display(&st); + + destroy(&st); // Free allocated memory + return 0; -} \ No newline at end of file +}

AltStyle によって変換されたページ (->オリジナル) /