-
Notifications
You must be signed in to change notification settings - Fork 383
Update stack_implementation_using_array.c #771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 72 additions & 52 deletions
Stack/stack_implementation_using_array.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,99 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.