Implement a stack using an array
Example 1:
Push(1): true: isEmpty(): false: Peek() : 1: Size() : 1
Peek(): 1: isEmpty(): false: Pop() : 1: Size() : 0
Underflow!! Underflow!! Peek(): -2147483648: isEmpty(): true: Pop() : -2147483648: Size() : 0
Push(1): true: isEmpty(): false: Peek() : 1: Size() : 1
Push(2): true: isEmpty(): false: Peek() : 2: Size() : 2
Push(3): true: isEmpty(): false: Peek() : 3: Size() : 3
Overflow!! Push(4): false: isEmpty(): false: Peek() : 3: Size() : 3
Approach:
Java
public class StackImlArray {public static void main(String[] args) {// create stack with sizeStackArray s = new StackArray(3);// push 1 elementSystem.out.println("Push(1): " + s.push(1)+ ": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());// pop elementSystem.out.println("Peek(): " + s.peek()+ ": isEmpty(): " + s.isEmpty() + ": Pop() : " + s.pop()+ ": Size() : " + s.size());// pop elementSystem.out.println("Peek(): " + s.peek()+ ": isEmpty(): " + s.isEmpty() + ": Pop() : " + s.pop()+ ": Size() : " + s.size());// push another elementSystem.out.println("Push(1): " + s.push(1)+ ": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());// push another elementSystem.out.println("Push(2): " + s.push(2)+ ": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());// push another elementSystem.out.println("Push(3): " + s.push(3)+ ": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());// push another elementSystem.out.println("Push(4): " + s.push(4)+ ": isEmpty(): " + s.isEmpty() + ": Peek() : " + s.peek()+ ": Size() : " + s.size());}}class StackArray{// initialize the default capacityprivate int DEFAULT_CAPACITY = 10;// arrayprivate int[] array;// initialization of top=-1private int TOP = -1;// constructor with capacitypublic StackArray(int maxSize) {this.DEFAULT_CAPACITY = maxSize;this.array = new int[DEFAULT_CAPACITY];this.TOP = -1;}// constructor with default capacitypublic StackArray() {this.array = new int[DEFAULT_CAPACITY];this.TOP = -1;}// add value in stackboolean push(int number) {// if not full, then add else Overflowif (!isFull()) {TOP++;array[TOP] = number;return true;} else {System.out.print("Overflow!! ");return false;}}// get top and top remove element from stackint pop() {if (!this.isEmpty())return array[TOP--];else {System.out.print("Underflow!! ");return Integer.MIN_VALUE;}}// only get top element from stackint peek() {if (!this.isEmpty())return array[TOP];else {System.out.print("Underflow!! ");return Integer.MIN_VALUE;}}public int size() {return TOP + 1;}boolean isEmpty() {return TOP == -1;}boolean isFull() {return TOP == DEFAULT_CAPACITY - 1;}}
C++
#include <bits/stdc++.h>using namespace std;class Stack {// initialize the default capacityint DEFAULT_CAPACITY = 10;// arrayint array[10];// initialization of top=-1int TOP = -1;// constructor with capacitypublic:Stack(int maxSize) {this->DEFAULT_CAPACITY = maxSize;array[DEFAULT_CAPACITY];this->TOP = -1;}// constructor with default capacityStack() {array[DEFAULT_CAPACITY];this->TOP = -1;}// add value in stackvoid push(int number) {// if not full, then add else Overflowif (!isFull()) {TOP++;array[TOP] = number;} else {cout<<"Overflow!! ";}}// get top and top remove element from stackvoid pop() {if (!this->isEmpty())array[TOP--];else {cout<<"Underflow!! ";}}// only get top element from stackint peek() {if (!this->isEmpty())return array[TOP];else {cout<<"Underflow!! ";return INT_MIN;}}int size() {return TOP + 1;}bool isEmpty() {return TOP == -1;}bool isFull() {return TOP == DEFAULT_CAPACITY - 1;}};int main(){// create stack with sizeStack s(3);// push 1 elementcout<<"Push(1): ";s.push(1);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";// pop elementcout<<"Peek(): "<<s.peek();cout<<": isEmpty(): "<<s.isEmpty();cout<<": Pop() : ";s.pop();cout<<": Size() : "<<s.size()<<"\n";// pop elementcout<<" Peek(): "<<s.peek();cout<<": isEmpty(): "<<s.isEmpty();cout<<": Pop() : ";s.pop();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<" Push(1): ";s.push(1);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<"Push(2): ";s.push(2);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<"Push(3): ";s.push(3);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<" Push(4): ";s.push(4);cout<<": isEmpty(): "<<s.isEmpty();cout<<": Peek() : "<<s.peek();cout<<": Size() : "<<s.size()<<"\n";return 0;}
No comments:
Post a Comment