Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Max-CoderBoi wants to merge 1 commit into codemistic:main from Max-CoderBoi:patch-7
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 72 additions & 52 deletions Stack/stack_implementation_using_array.c
View file Open in desktop
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;
}
}

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