Question: / / Java Program to Implement Stack in Java Using Array and Generics / / Importing input output classes import java.io . * ; /
Java Program to Implement Stack in Java Using Array and Generics
Importing input output classes
import java.io;
Importing all utility classes
import java.util.;
user defined class for generic stack
class stack
Empty array
ArrayList A;
Default value of top variable when stack is empty
int top ;
Variable to store size of array
int size;
Constructor of this class to initialize stack
stackint size
Storing the value of size into global variable
this.size size;
Creating array of Size size
this.A new ArrayListsize;
Method
To push generic element into stack
void pushT X
Checking if array is full
if top size
Display message when array is full
System.out.printlnStack Overflow";
else
Increment top to go to next position
top top ;
Overwriting existing element
if Asize top
Asettop X;
else
Creating new element
AaddX;
Method
To return topmost element of stack
T peek
If stack is empty
if top
Display message when there are no elements in
the stack
System.out.printlnStack Underflow";
return null;
else elements are present so
return the topmost element
else
return Agettop;
Method
To delete last element of stack
void pop
If stack is empty
if top
Display message when there are no elements in
the stack
System.out.printlnStack Underflow";
else
Delete the last element
by decrementing the top
top;
Method
To check if stack is empty or not
public boolean empty return top ;
Method
To print the stack
@Override
public String toString
String Ans ;
for int i ; i top; i
Ans String.valueOfAgeti;
Ans String.valueOfAgettop;
return Ans;
Main Class
public class StackTester
main driver method
public static void mainString args
Integer Stack
Creating an object of Stack class
Declaring objects of Integer type
stack s new stack;
Pushing elements to integer stack s
Element
spush;
Element
spush;
Element
spush;
Print the stack elements after pushing the
elements
System.out.println
s after pushing and :
s;
Now, pop from stack s
spop;
Print the stack elements after poping few
elements
System.out.printlns after pop :
s;
String Stack
Creating an object of Stack class
Declaring objects of Integer type
stack s new stack;
Pushing elements to string stack s
Element hello
spushhello;
Element world
spushworld;
Element java
spushjava;
Print string stack after pushing above string
elements
System.out.println
s after pushing elements :
s;
System.out.println
s after pushing th element :;
Pushing another element to above stack
Element GFG
spushGFG;
Float stack
Creating an object of Stack class
Declaring objects of Integer type
stack s new stack;
Pushing elements to float stack s
Element
spushf;
Element
spushf;
Print string stack after pushing above float
elements
System.out.println
s after pushing elements :
s;
Print and display top element of stack s
System.out.printlntop element of s:
speek;
System.out.printlnIs stack s empty? sempty;
Question: Make flowcharts of the StackTester.java.
IMPORTANT: For Stack Flowchart accommodate the Push puts node onto top of stack, Pop returns node value from top of stack and removes node, Peek returns node value from top of stack, empty tests isEmpty returns boolean, size returns integer, and search returns node address corresponding to the position in the stack of the particular object if found otherwise returns when not found. int x search; returns an integer.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
