// C program for array implementation of stack#include "stdafx.h"#include <stdlib.h>#include <limits.h>// A structure to represent a stackstruct Stack
{
int top;
unsigned capacity;
int* array;
};
// function to create a stack of given capacity. It initializes size of// stack as 0struct 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));
returnstack;
}
// Stack is full when top is equal to the last indexint isFull(struct Stack* stack)
{
returnstack->top == stack->capacity - 1;
}
// Stack is empty when top is equal to -1int isEmpty(struct Stack* stack)
{
returnstack->top == -1;
}
// Function to add an item to stack. It increases top by 1void push(struct Stack* stack, int item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}
// Function to remove an item from stack. It decreases top by 1int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
returnstack->array[stack->top--];
}
// Driver program to test above functionsint main()
{
struct Stack* stack = createStack(100);
push(stack, 10);
push(stack, 20);
push(stack, 30);
printf("%d popped from stack\n", pop(stack));
system("pause");
return0;
}
C++:
/* C++ program to implement basic stack operations */#include "stdafx.h"#include <iostream>usingnamespacestd;
#define MAX 1000class Stack
{
int top;
public:
int a[MAX]; //Maximum size of Stack
Stack() { top = -1; }
bool push(int x);
int pop();
bool isEmpty();
};
bool Stack::push(int x)
{
if (top >= MAX)
{
cout << "Stack Overflow";
returnfalse;
}
else
{
a[++top] = x;
returntrue;
}
}
int Stack::pop()
{
if (top < 0)
{
cout << "Stack Underflow";
return0;
}
else
{
int x = a[top--];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
// Driver program to test above functionsint main()
{
struct Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " Popped from stack\n";
system("pause");
return0;
}
Python:
# Python program for implementation of stack# import maxsize from sys module # Used to return -infinite when stack is emptyfrom sys import maxsize
# Function to create a stack. It initializes size of stack as 0defcreateStack():
stack = []
return stack
# Stack is empty when stack size is 0defisEmpty(stack):return len(stack) == 0# Function to add an item to stack. It increases size by 1defpush(stack, item):
stack.append(item)
print("pushed to stack " + item)
# Function to remove an item from stack. It decreases size by 1defpop(stack):if (isEmpty(stack)):
return str(-maxsize -1) #return minus infinitereturn stack.pop()
# Driver program to test above functions
stack = createStack()
push(stack, str(10))
push(stack, str(20))
push(stack, str(30))
print(pop(stack) + " popped from stack")
C语言:// C program for array implementation of stack#include "stdafx.h"#include &lt;stdlib.h&gt;#include &lt;limits.h&gt;// A structure to represent a stackstruct Stack{ int top; unsi...