Question
I used the following code and needed to change according to instruction. public class ArrayStack { public static final int CAPACITY = 100; // default
I used the following code and needed to change according to instruction.
public class ArrayStack { public static final int CAPACITY = 100; // default array capacity private E[ ] data; // generic array used for storage private int topElement = -1; // index of the top element in stack public ArrayStack() { this(CAPACITY); } // constructs stack with default capacity public ArrayStack(int capacity) { // constructs stack with given capacity data = (E[ ]) new Object[capacity]; // safe cast; compiler may give warning } public int size() { return (topElement + 1); } public boolean isEmpty() { return (topElement == -1); } public void push(E e) throws IllegalStateException { if (size( ) == data.length) throw new IllegalStateException("Stack is full"); data[++topElement] = e; } public E top() { if (isEmpty()) return null; return data[topElement]; } public E pop() { if (isEmpty( )) return null; E answer = data[topElement]; data[topElement] = null; topElement--; return answer; } public static void main(String args[]) { ArrayStack S = new ArrayStack(); S.push(5); S.push(3); //System.out.println("Stack ... " + S.data); System.out.println("Size of stack: " + S.size( )); System.out.println("POP stack: " + S.pop( )); System.out.println("Is stack empty? " + S.isEmpty( )); System.out.println("POP stack: " + S.pop( )); System.out.println("Is stack empty? " + S.isEmpty( )); System.out.println("POP stack: " + S.pop( )); S.push(7); S.push(9); S.push(10); System.out.println("TOP stack: " + S.top( )); S.push(4); System.out.println("Size of stack: " + S.size( )); System.out.println("POP stack: " + S.pop( )); S.push(6); S.push(8); System.out.println("POP stack: " + S.pop( )); } }
1. Using your favorite programming language, implement a singly linked list with the following operations: addFirst, removeFirst, addLast, removeLast, getFirst, getLast, size. 2. Implement a stack on the singly linked list with the operations of Lab Assignment 1. Hint: Using the same Stack class you implemented, change the array to an object of the singly linked list class. The functionality of push and pop is now based on the methods of the linked list classStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started