Question
please answer Now
----data structure - java

Q1: Create a complete Stack data structure with its methods and apply it in java language. Then, make a method in the same cl
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey, I have tried me best to answer your question. If still you have any query, then do tell me in comments. And, if you found the solution helpful, then pls do upvote...Thanks :)

//Comments are provided for better understanding..

*************************************************************

Code-

class Stack{
  
static final int MAX=100;
int top;
int a[]=new int[MAX];
  
  
//Default Constructor
Stack(){
top=-1;
}
  
  
//method to push an element to stack
boolean push(int val){
if(top>=(MAX-1)){
System.out.println("Stack is full");
return false;
}else{
  
a[++top]=val;
System.out.println(val+" added to stack...");
return true;
}
}
  
  
//method to check if a stack is empty or not
boolean isEmpty(){
return (top<0);
}
  
//method to pop an element from the stack
int pop(){
if(top<0){
System.out.println("Stack is empty...");
return 0;
  
}else{
int val=a[top--];
return val;
}
  
}
  
  
//method to get the top element of stack
int peek(){
if(top<0){
System.out.println("Stack is empty...");
return 0;
}else{
int val=a[top];
return val;
}
}
  
//method to reverse a stack
Stack reverseStack(Stack stack){
Stack new_stack=new Stack();
while (!stack.isEmpty()){
new_stack.push(stack.pop());
}
return new_stack;
}
  
  
//Main method
public static void main(String []args){
Stack stack=new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack.peek());
Stack new_stack=stack.reverseStack(stack);
System.out.println(new_stack.peek());
}
  
  
  
  
}

Add a comment
Know the answer?
Add Answer to:
please answer Now ----data structure - java Q1: Create a complete Stack data structure with its...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT