Question

5. Stack (12 pts) Build a Stack class for a st The Stack should operat and methods also listed below ack of doubles that is compatible with the driver code below e in a LIFO (last in, first out) fashion and implement the variables a. Data Members: Declare the data item(s) you will need to manage a stack of doubles. Method Members: public void push (double input): This should add the double input to your stack public double pop (): This should remove and return the double on the top of your stack. It does not have to test if the stack is empty. public boolean isEmpty(): Returns true if the stack is empty, false otherwise You do not need to provide comments, a copy constructor), nor any additional methods beyond those specified above. The class does not have to support an infinite stack (resizing not necessary). Use an array for data storage. constructor (or no-argument

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since you have not provided the language of your choice, I am providing the code in Java.

CODE

==================

/*
* Java Program to Implement Stack
*/
import java.util.*;

/* Class arrayStack */
public class Stack
{
private double data[];
protected int top, size, len;

/* Constructor for arrayStack */
public Stack(int n)
{
size = n;
len = 0;
data = new double[size];
top = -1;
}
  
/* Function to check if stack is empty */
public boolean isEmpty()
{
return top == -1;
}
  
/* Function to add an element to the stack */
public void push(int i)
{
if(top + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
if(top + 1 < size )
data[++top] = i;
len++ ;
}
  
/* Function to delete an element from the stack */
public double pop()
{
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
len-- ;
return data[top--];
}
}

NOTE: Once submitted, I cannot edit the code. Hence, please mention the langauge while posting a coding related question in future.

Add a comment
Know the answer?
Add Answer to:
5. Stack (12 pts) Build a Stack class for a st The Stack should operat and...
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