following a Le the viewer to verify your anrers Mack pushl JAVA Stacks Dara Screctures IStack Implementarion Create a class ealled ArravilasedStack. Declare the folkowing variahles * data references an array scoring elements in the ist mpOStack. an inr value represenring the location of the stack true If you had used a linked list implementation of the stack, how would your stack differ from the array implementation? Add a method called peek that will return the top of the stack but will not remove the element from the TANmp ia the array • INITIAL, CAPACITY: the defalt capaciry of the stack pahlic elass AmaylasedStack { private SinglyLinkedNode top Node = null; Data Structures Stacks Duta Structures Stack e ASack undrlow thruw nev EeptyStackEeption. •If there are ekments, get the ckent at the end of che sack index is top(Stack) and decrement Lhe indea of the Lup af the stack. Set the previous tog uf cde Data Structures I Stack 8 public void push(E newElement) { I/ create a node that holds the new element Stack sa nul EreturnObj - latal ; dataſtopOtscack) - aul; • Decrernene the index of rhe op the seack and reeurn ehe previous cop. ropOtStack , Il set the next node reference of the new node to the current top l set the top node to reference the new clement public boolean isEmpty0 { l empty if the topNode is null return : Run the tollowing in interactions with a viewer open on the rark objert. Anvwer the follesing questions and then use the viewer to verity your answers. - What wil the srack look like after involcing the pash operarion on String objects "ist, "and, and "znd? • What would a nop operarion return ar this poinr? Whar woald 1 second pop operation rerum? ArrayBasedStack stack - new AmayliasedStacjk stack isliapry 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 / return the stored data Enie stack pusl'isa'k public E peek0 { Il throw an exception if stack is empty // return the data from the top node stack isEnapry0 false Tase szark pash'and'k Stack pasht" sed"h stack pop0 stack pop0 and Sack pushl"4ih stack pop0 Submit both the ArrayBasedStack class and the LinkedStack class implementations stack pop0 stack isEmptyo

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
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 <R
private ) dara
private inr topOStack
prieate static nal int INITIAI CAPACITY-:
true
If you had used a linked list implementation of the stack, how
would your stack differ from the
array implementation?
Add a method called peek that will return the top of the stack
but will not remove the element from the
stack.
public E peek0 {
Data Structures Stacks
Add a constructor that will initalie the stack with a uIser
defined inirial caparirs The top of the stack
will be assigned a value of-1 to indicate that the srack is
currently cmpte
public ArrsylasedStackint capcity)(
data - (FI) new Objeclcapocinyk
Data Structures Stack 7
Another costructor take no paraneters and sets the capacty
of the data ay to the defaudi valae
public ArrayBlasedStacko
this INITIAI_CAPACITY);
• If the stack is empty, throw an exception.
if (isEmpty0) (
throw new EmptyŠtackException0;
• Otherwise, return the element at the top of the stack.
Auld a metbod that wil capad the array when nosary The
method is very similar to that in the
Array BaselList accivicy Tu roduce the amout of code to line,
yo Eoald also use the copyOf nehod in
java utilArrNs.
rirate vuid expankint amount)
Ell emp = EIB ew Objeetldata length - armount;
for Gnt i- xi c- upOStack i
templil - datali
return dataf.
Test your class as follows in the interactions pane. Answer the
following questions and verify your
answers using the viewer:
• At what point is the internal array expanded, if at all?
• What are the contents of the stack before the push operation
is performed on object "o"?
ArrayBasedStack stack - new ArrayBasedStack(3);
stack.isEmpty0
data - temp
Add a method called push that will add an element to the top of
the stack
pablic void punbiE newElement
true
stack.push("b");
stack.push("e");
stack.peek0
Data Structurvs Stacks
Data Seructures ISeack t
•f chu array is tull, incavae the kngela of the arrazy by the
balkwing amoune appreximarely 6s
né its currene izel
if (toptStaclk -- datalengh
epandldara lengeh ' .
stack.pop0
e
• Incresse the top of the stack to point to the new element and
stack.peek0
inserr the sew eleer into the stack
topOfhtack
dataltopSrack) - newilement
Create a new ArrayBasedseack in interactions and oprn a viewer
stack.pop0
b
an the srark ohject. Anwer the
following queuionsand use che viewer to verify your answers
• What will be the vahse of tanOHStack atter eachline of cade is
stack.push("o");
stack.pop0
esecuted
•1 low many rime will the innernal zrray expand? Whar will ins
size be after each espansion
AayBasedStack stack - new Avay RasedStacih
srack pahly
stack push
tack push
stack.isEmpty0
true
l
stack push
The isEmpty melod will retern whcther the stack has
clements Tith top of the stack is less than o then
there are no clesocnts cently in the stack:
public boolean islnpeyo
stack.pop0
Exception in evaluation thread java.util.EmptyStackException
Deliverables:
Implement a stack using a linked implementation. First,
implement the SinglyL.inkedNode class
from the activity on singly linked list. Use the following hints to
create your stack and use the same
operations in the interactions pane from the array-based version
to test your stack's functionality.
import java.util.EmptyStackException;
public class LinkedStack<E>{
private SinglyLinkedNode<E> 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
Transcribed Image 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 <R private ) dara private inr topOStack prieate static nal int INITIAI CAPACITY-: true If you had used a linked list implementation of the stack, how would your stack differ from the array implementation? Add a method called peek that will return the top of the stack but will not remove the element from the stack. public E peek0 { Data Structures Stacks Add a constructor that will initalie the stack with a uIser defined inirial caparirs The top of the stack will be assigned a value of-1 to indicate that the srack is currently cmpte public ArrsylasedStackint capcity)( data - (FI) new Objeclcapocinyk Data Structures Stack 7 Another costructor take no paraneters and sets the capacty of the data ay to the defaudi valae public ArrayBlasedStacko this INITIAI_CAPACITY); • If the stack is empty, throw an exception. if (isEmpty0) ( throw new EmptyŠtackException0; • Otherwise, return the element at the top of the stack. Auld a metbod that wil capad the array when nosary The method is very similar to that in the Array BaselList accivicy Tu roduce the amout of code to line, yo Eoald also use the copyOf nehod in java utilArrNs. rirate vuid expankint amount) Ell emp = EIB ew Objeetldata length - armount; for Gnt i- xi c- upOStack i templil - datali return dataf. Test your class as follows in the interactions pane. Answer the following questions and verify your answers using the viewer: • At what point is the internal array expanded, if at all? • What are the contents of the stack before the push operation is performed on object "o"? ArrayBasedStack stack - new ArrayBasedStack(3); stack.isEmpty0 data - temp Add a method called push that will add an element to the top of the stack pablic void punbiE newElement true stack.push("b"); stack.push("e"); stack.peek0 Data Structurvs Stacks Data Seructures ISeack t •f chu array is tull, incavae the kngela of the arrazy by the balkwing amoune appreximarely 6s né its currene izel if (toptStaclk -- datalengh epandldara lengeh ' . stack.pop0 e • Incresse the top of the stack to point to the new element and stack.peek0 inserr the sew eleer into the stack topOfhtack dataltopSrack) - newilement Create a new ArrayBasedseack in interactions and oprn a viewer stack.pop0 b an the srark ohject. Anwer the following queuionsand use che viewer to verify your answers • What will be the vahse of tanOHStack atter eachline of cade is stack.push("o"); stack.pop0 esecuted •1 low many rime will the innernal zrray expand? Whar will ins size be after each espansion AayBasedStack stack - new Avay RasedStacih srack pahly stack push tack push stack.isEmpty0 true l stack push The isEmpty melod will retern whcther the stack has clements Tith top of the stack is less than o then there are no clesocnts cently in the stack: public boolean islnpeyo stack.pop0 Exception in evaluation thread java.util.EmptyStackException Deliverables: Implement a stack using a linked implementation. First, implement the SinglyL.inkedNode class from the activity on singly linked list. Use the following hints to create your stack and use the same operations in the interactions pane from the array-based version to test your stack's functionality. import java.util.EmptyStackException; public class LinkedStack<E>{ private SinglyLinkedNode<E> 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
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education