Question

JAVA Write a class to implement the Stack ADT using an array as the underlying structure....

  • JAVA
  • Write a class to implement the Stack ADT using an array as the underlying structure.
  • Implement the pop function so that pop not only removes the top item from the stack, but also reports that value.

Homework Answers

Answer #1

CODE

import java.util.*;

class ArrayBasedStack
{
    protected int arr[];
    protected int top, size, len;
  
    public ArrayBasedStack(int n)
    {
        size = n;
        len = 0;
        arr = new int[size];
        top = -1;
    }
  
    public boolean isEmpty()
    {
        return top == -1;
    }
  
    public boolean isFull()
    {
        return top == size -1 ;      
    }
  
    public int getSize()
    {
        return len ;
    }
  
    public int peek()
    {
        if( isEmpty() )
            throw new NoSuchElementException("Underflow Exception");
        return arr[top];
    }
  
    public void push(int i)
    {
        if(top + 1 >= size)
            throw new IndexOutOfBoundsException("Overflow Exception");
        if(top + 1 < size )
            arr[++top] = i;
        len++ ;
    }
  
    public int pop()
    {
        if( isEmpty() )
            throw new NoSuchElementException("Underflow Exception");
        len-- ;
        return arr[top--];
    }
}

Know the answer?
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
Similar Questions
10.6 LAB: Implement a stack using an array Given main() complete the Stack class by writing...
10.6 LAB: Implement a stack using an array Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Using Java, in the most simple algorithm please Implement a static stack class of char. Your...
Using Java, in the most simple algorithm please Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
Instructions: SLLStack (12 pts) ● Using the two properties below, implement the stack interface using the...
Instructions: SLLStack (12 pts) ● Using the two properties below, implement the stack interface using the SLLNode class from Lab 2: ○ top_node → SLLNode object or None ○ size → int, keep track of stack size ● Implement the push( ), pop( ), top( ) methods ● Use SLLNode methods: get_item( ), set_item( ), get_next( ), set_next( ) ● (5 pts) In push(item): ○ Create new SLLNode with item ○ Add new node before top node ○ Update top_node...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...
Assume you had to implement a stack-like ADT in which pop() always returns the largest element....
Assume you had to implement a stack-like ADT in which pop() always returns the largest element. Let A be an array and n be the number of elements currently in the array. Suppose a student’s array-based implementation performs the push() function as follows: Call Binary-Search(x, A, n) to locate the index i in the array A at which x should be inserted to keep A sorted in ascending order. Shift the elements at indices i through n – 1 one...
Consider a linked implementation of a Stack. Implement the pop() method below. Assume a typical singly...
Consider a linked implementation of a Stack. Implement the pop() method below. Assume a typical singly linked node, with an instance variables data and next. public class LinkedStack<T> implements Stack<T> { private Node<T> head;    public LinkedStack() { head = new Node<T>(); } // pops the top item off the stack // returning the popped value // throw an EmptyStackException if there are no // items in the stack public T pop() { // write your implementation below } }
Write a C program: Implement the abstract data type (ADT) queue (FIFO) of strings. ADT has...
Write a C program: Implement the abstract data type (ADT) queue (FIFO) of strings. ADT has the following methods: clear – clears the queue; is_empty – returns 1 if the queue is empty, otherwise 0; is_full – returns 1 if the queue is full, otherwise 0; add – adds a new string at the end of the queue; remove – removes the string from the front of the queue. Use ADT queue to solve the following problems: • Write an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT