A stack is an abstract data type that serves as a collection of
elements with two main operations: Push, which adds an element to
the collection, and Pop, which removes the most recently added
element.
The order in which an element added to or removed
from a stack is described as last in, first out, referred to by the
acronym LIFO.
class Stack:
def __init__(self):
self.stack = []
def push(self,data):
self.stack.append(data)
def pop(self):
ele = self.stack.pop()
return ele
def print(self):
print(self.stack)
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
struct Stack* createStack(unsigned capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack->capacity * sizeof(int));
return stack;
}
int isFull(struct Stack* stack) {
return stack->top == stack->capacity - 1;
}
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
void push(struct Stack* stack, int item) {
if (isFull(stack))
return;
stack->array[++stack->top] = item;
}
int pop(struct Stack* stack) {
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
}
int peek(struct Stack* stack) {
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}
int main() {
struct Stack* stack = createStack(100);
push(stack, 10);
push(stack, 20);
push(stack, 30);
printf("%d popped from stack\n", pop(stack));
return 0;
}