(Revising Listing 19.1) Revise the GenericStack class in Listing 19.1 to implement it using an array rather than an ArrayList. You should check the array size before adding a new element to the stack. If the array is full, create a new array that doubles the current array size and copy the elements front the current array to the new array.
Revising Listing 19.1
Program Plan:
- Define the class named “GenericStack<E>”.
- Declare appropriate variables to program.
- Define constructors “GenericStack()” which assigns initial size of stack and “GenericStack(initialCapaciy)” which assigns size of the stack.
- Define a method “push()” which pass “E o” as parameter.
- Using “if” condition, check the value of “N” greater than “elements.length”.
- Assign size of new array.
- Copy old array into new array.
- Return array elements.
- Using “if” condition, check the value of “N” greater than “elements.length”.
- Define a method “pop()” which pop out stack elements.
- Return array elements.
- Define a method “peek()” which returns top of the stack.
- Define a method “isEmpty()” which returns “0”.
- Define a method “getSize()” which returns size of the stack.
- Define the main method.
- Declare the GenericStack object “obj”, using the object insert elements into stack.
- Using “while” loop, print the elements on screen.
The following JAVA code is to revise the “GenericStack” class which implements array rather than an “ArrayList”.
Explanation of Solution
Program:
//Class definition
class GenericStack<E>
{
/*Declaration of variables*/
public final static int INITIAL_SIZE = 16;
private E[] elements;
private int N;
/*Construct a stack with the default initial capacity */
public GenericStack()
{
//Assign initial size
this(INITIAL_SIZE);
}
/*Construct a stack with the specified initial capacity */
public GenericStack(int initialCapacity)
{
//Assign size
elements = (E[])new Object[initialCapacity];
}
/*Push a new element into the top of the stack */
public E push(E o)
{
//Condition
if (N >= elements.length)
{
//Assign array size into variable "t"
E[] t = (E[])new Object[elements.length * 2];
//Copy array elements
System.arraycopy(elements, 0, t, 0, elements.length);
//Assign "t" into "elements"
elements = t;
}
//Return statement
return elements[N++] = o;
}
/*Return and remove the top element from the stack*/
public E pop()
{
//Return statement
return elements[--N];
}
/*Return the top element from the stack */
public E peek()
{
//Return statement
return elements[N - 1];
}
/*Function definition to check empty*/
public boolean isEmpty()
{
//Return statement
return N == 0;
}
/*Return the number of elements in the stack */
public int getSize()
{
//Return statement
return N;
}
//Main method
public static void main(String[] args)
{
//Assign object to "GenericStack"
GenericStack<String> obj = new GenericStack<>();
//Push elements into stack
obj.push("London");
obj.push("Paris");
obj.push("Berlin");
//Print elements
System.out.print("Stack 1: ");
//Loop
while (!obj.isEmpty())
{
//Print statement
System.out.print(obj.pop() + " ");
}
//Print statement
System.out.println("\n");
}
}
Stack 1: Berlin Paris London
Want to see more full solutions like this?
Chapter 19 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)
Introduction To Programming Using Visual Basic (11th Edition)
Mechanics of Materials (10th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
Starting Out with Python (4th Edition)
- (Please Help. My professor did not teach us any of this and will not answer my emails) Design a class named Queue for storing integers. Like a stack, a queue holds elements. In a stack, the elements are retreived in a last-in-first-out fashion. In a queue, the elements are retrieved in a first-in-first-out fashion. The class contains: An int[] data field named elements that stores the int values in the queue A data field named size that stores the number of elements in the queue A constructor that creates a Queue object with defult capacity 8 The method enqueue(int v) that adds v into the queue The method empty () that returns true if the queue is empty The method getSize() that returns the size of the queuearrow_forwarda) Can you develop Java code.Create a recursive function to print all permutations of a string. For example, the permutation of the string "abc" is [abc, acb, bac, bca, cab, cba]. b) Revise the genericStack class to use an array instead of an ArrayList. Before adding a new element, make sure the array size is correct. If the current array is full, construct a new array twice the size, copy the elements, and add the new array. dont use aiarrow_forwardWhat happens when the ref or out keyword is used with an array parameter?arrow_forward
- Help pleasearrow_forwardIdentify the difference between STACK and ARRAY.arrow_forward(Java) In this task, a skip list data structure should be implemented. You can follow the followinginstructions:- Implement the class NodeSkipList with two components, namely key node and arrayof successors. You can also add a constructor, but you do not need to add anymethod.- In the class SkipList implement a constructor SkipList(long maxNodes). The parameter maxNodes determines the maximal number of nodes that can be added to askip list. Using this parameter we can determine the maximal height of a node, thatis, the maximal length of the array of successors. As the maximal height of a nodeit is usually taken the logarithm of the parameter. Further, it is useful to constructboth sentinels of maximal height.- In the class SkipList it is useful to write a method which simulates coin flip andreturns the number of all tosses until the first head comes up. This number representsthe height of a node to be inserted.- In the class SkipList implement the following methods:(i) insert, which…arrow_forward
- [Java Code] The question SHOULD have MULTIPLE CLASSES like child, parent, main. Thank you in advance =Darrow_forwardTopic: Singly Linked ListImplement the following functions in C++ program. Read the question carefully. (See attached photo for reference) int count(int num) This will return the count of the instances of the element num in the list. In the linked list in removeAll method, having the method count(10) will return 3 as there are three 10's in the linked list. Additionally, you are to implement these methods: bool move(int pos1, int pos2) This method will move the element at position pos1 to the position specified as pos2 and will return true if the move is successful. This will return false if either the specified positions is invalid. In the example stated, operating the method move(1, 3) will move the 1st element and shall now become the 3rd element. The linked list shall now look like: 30 -> 40 -> 10 -> 50 int removeTail() This method will remove the last element in your linked list and return the said element. If the list is empty, return 0. You can use the…arrow_forwardHow do you do this? JAVAarrow_forward
- Don't use vector array .using c++ languagearrow_forwardName:- MIT Note:- Please type this java code and also need an output for this given Java program.arrow_forward(Online Address Book revisited) Programming Exercise 5 in Chapter 11 could handle a maximum of only 500 entries. Using linked lists, redo the program to handle as many entries as required. Add the following operations to your program: Add or delete a new entry to the address book. Allow the user to save the data in the address book.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education