Sitemap

QUEUE USING ARRAY

1 min readJul 13, 2023

--

Implement stack using Array 1

Write a program to implement stack using Array. Implement all the stack related functions — push, pop. print stack.

Input Format

· Input consists of

· n = Total number of operations

· String — Operation to be performed

· Integer — Elements of stack > 0

Output Format

Output consists of Integer.

Sample Input 0

10

Push

1

Pop

Push

5

Push

2

Pop

Push

7

Push

9

Push

10

Pop

Print

Sample Output 0

5

7

9

//SOURCE CODE

import java.io.*;

import java.util.*;

Get Logapranesh S’s stories in your inbox

Join Medium for free to get updates from this writer.

public class Solution {

public static void main(String[] args) {

Scanner sc=new Scanner (System.in);

Stack s=new Stack();

int n=sc.nextInt();

for(int i=0;i<n;i++){

String str=sc.next();

if(str.equals(“Push”)){

int val=sc.nextInt();

s.push(val);

}

else if(str.equals(“Pop”)){

s.pop();

}

else if(str.equals(“Print”)){

s.print();

}

}

}

}

class Stack{

final int size=100;

int arr[]=new int [size];

int top=-1;

public void push(int val){

arr[++top]=val;

}

public void pop(){

top — ;

}

public void print(){

for(int i=0;i<=top;i++)

System.out.println(arr[i]);

}

}

--

--