{private SinglyLinkedNode top Node = null;Data Structures StacksThe pop method willemove ihe last clenca hat was added totle stackpubile I popo• li seack is enpty tlauw at EuptyStackException....


JAVA Stacks<br>Data Scructures IStack<br>Implementarion<br>Create a class called ArrayBasedStack. Declare the follkawing<br>variahles:<br>* data references an array scoring elements in the ist<br>mpOStack. an inr value represenring the location nf the stack<br>Top in the array<br>• INITIAL, (CAPACITY the defalt capaciey of the srack<br>puhlic clans AayRasedSeack <R<br>private ) dara<br>private inr topOStack<br>prieate static nal int INITIAI CAPACITY-:<br>true<br>If you had used a linked list implementation of the stack, how<br>would your stack differ from the<br>array implementation?<br>Add a method called peek that will return the top of the stack<br>but will not remove the element from the<br>stack.<br>public E peek0 {<br>Data Structures Stacks<br>Add a constructor that will initalie the stack with a uIser<br>defined inirial caparirs The top of the stack<br>will be assigned a value of-1 to indicate that the srack is<br>currently cmpte<br>public ArrsylasedStackint capcity)(<br>data - (FI) new Objeclcapocinyk<br>Data Structures Stack 7<br>Another costructor take no paraneters and sets the capacty<br>of the data ay to the defaudi valae<br>public ArrayBlasedStacko<br>this INITIAI_CAPACITY);<br>• If the stack is empty, throw an exception.<br>if (isEmpty0) (<br>throw new EmptyŠtackException0;<br>• Otherwise, return the element at the top of the stack.<br>Auld a metbod that wil capad the array when nosary The<br>method is very similar to that in the<br>Array BaselList accivicy Tu roduce the amout of code to line,<br>yo Eoald also use the copyOf nehod in<br>java utilArrNs.<br>rirate vuid expankint amount)<br>Ell emp = EIB ew Objeetldata length - armount;<br>for Gnt i- xi c- upOStack i<br>templil - datali<br>return dataf.<br>Test your class as follows in the interactions pane. Answer the<br>following questions and verify your<br>answers using the viewer:<br>• At what point is the internal array expanded, if at all?<br>• What are the contents of the stack before the push operation<br>is performed on object { private SinglyLinkedNode top Node = null; Data Structures Stacks The pop method willemove ihe last clenca hat was added to tle stack pubile I popo • li seack is enpty tlauw at EuptyStackException. Iuport the EmpeyStackExccption class which is in tle javautd package if lisEmpaydo Data Suructures Stacks Duta Stractures Stack d Stack undkrlow throw new ErptyStackEeptioan. • lf there are ekments. get the ckment at the end af che sack (index is topOtStack) aad decrement the indea of the tup af the stack. Set the previous tog of che Szack su aul E returOy - datai dataltopOtScack) - null; • Decrernene the index of the op n the seack and rerurn che previous cop. top(tStack, Data Structures I Stack 8 public void push(E newElement) { // create a node that holds the new element // set the next node reference of the new node to the current top Il set the top node to reference the new element public boolean isEmpty0 { i cmpty if the topNode is null return Run the tollowiag in iateractions witha viewer open on the vark object. Anvwer the follewing questions and then use the viewer co verity your answers. - What wilthe srack look likke after ievolcing rhe pash operarion public E pop0 { ii throw an exception if stack is empty /i store data from current top node (type E) Il set the top node to reference the next node in the stack Il return the stored data on String objects "ist. "and", and • What would a nop operation rerurn ar this poinr? Whar woald a second pop operation rerum? ArrayBasedStack stack - new AmayliasedStack(gk tack inllonprasetacah Enie stack puslt'isa'k stack islipryo public E peck0 { li throw an exception if stack is empty // return the data from the top node false stark pash and'k stack panht" sed'k Stack pop0 stack pop0 2nd Sack pusht"4ih", stack pop0 Submit both the ArrayBasedStack class and the LinkedStack class implementations stack pop0 stack isEmptyo "/>
Extracted text: JAVA Stacks Data Scructures IStack Implementarion Create a class called ArrayBasedStack. Declare the follkawing variahles: * data references an array scoring elements in the ist mpOStack. an inr value represenring the location nf the stack Top in the array • INITIAL, (CAPACITY the defalt capaciey of the srack puhlic clans AayRasedSeack { private SinglyLinkedNode top Node = null; Data Structures Stacks The pop method willemove ihe last clenca hat was added to tle stack pubile I popo • li seack is enpty tlauw at EuptyStackException. Iuport the EmpeyStackExccption class which is in tle javautd package if lisEmpaydo Data Suructures Stacks Duta Stractures Stack d Stack undkrlow throw new ErptyStackEeptioan. • lf there are ekments. get the ckment at the end af che sack (index is topOtStack) aad decrement the indea of the tup af the stack. Set the previous tog of che Szack su aul E returOy - datai dataltopOtScack) - null; • Decrernene the index of the op n the seack and rerurn che previous cop. top(tStack, Data Structures I Stack 8 public void push(E newElement) { // create a node that holds the new element // set the next node reference of the new node to the current top Il set the top node to reference the new element public boolean isEmpty0 { i cmpty if the topNode is null return Run the tollowiag in iateractions witha viewer open on the vark object. Anvwer the follewing questions and then use the viewer co verity your answers. - What wilthe srack look likke after ievolcing rhe pash operarion public E pop0 { ii throw an exception if stack is empty /i store data from current top node (type E) Il set the top node to reference the next node in the stack Il return the stored data on String objects "ist. "and", and • What would a nop operation rerurn ar this poinr? Whar woald a second pop operation rerum? ArrayBasedStack stack - new AmayliasedStack(gk tack inllonprasetacah Enie stack puslt'isa'k stack islipryo public E peck0 { li throw an exception if stack is empty // return the data from the top node false stark pash and'k stack panht" sed'k Stack pop0 stack pop0 2nd Sack pusht"4ih", stack pop0 Submit both the ArrayBasedStack class and the LinkedStack class implementations stack pop0 stack isEmptyo
Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here