The stack is a classic data structure that is encountered in computer science. It implements a FILO / LIFO mechanism using its push and pop methods. Although linked lists and stacks are often used for radically different purposes, it is possible to implement the stack using a linked list. At this time you are asked to implement class Stack using class Inklst. You are being provided with Inklst.jar, a Java archive containing all the classes needed for the Inklst and its supporting classes, including BaseLnklst.class, Lnklst.class, Node.class and Data.class[1]. Use whatever OO techniques you deem appropriate. Your implementation of Stack must contain the following:

  • Stack() - the constructor for the class
  • int popAll() - removes all of the elements of the stack returning the number of elements removed.
  • void push( final Data ) - adds the argument to the stack by pushing it onto the top
  • Data pop() - removes the item on the top of the stack and returns a reference to that item. The reference is null if the stack is empty.
  • int createFromFile(String ) - accepts as input the name of a file containing binary information of type Data 2) and then reads that file one record at a time, popping the data onto the stack object. The method returns the number of records read and deposited into the stack. If a problem occurs, return the failure status of BaselnkLst.
  • int length() - returns the size of the stack
  • boolean isEmpty() - returns TRUE if the stack has no elements; FALSE, otherwise.

This is to be a multi-file project. Stack.java will contain the actual implementation. You will also write a driver program that continuously re-prompts the user to enter the name of the file containing the binary data[3]. The driver then instantiates an object of type Stack and invokes its createFromFile method passing it the character string containing the file name. Let the sample output which follows guide your design. As always, follow the good rules of program construction. Comment your code as necessary and be efficient so as not to waste memory or cpu cycles. Use reserved words such as final to insure that the functions cannot change parameters that should be immutable. Always have appropriate exception handling.

Enter the file you want read - data1000.bin
Preparing to read data1000.bin
In total 1000 records were pushed onto the stack.
Do you wish to continue with another file (Y/N)? - y
Enter the file you want read - data 2000.bin
data2000.bin could not be opened.
In total 1000 records were pushed onto the stack.
Do you wish to continue with another file (Y/N)? - y
Enter the file you want read - data 20000.bin
Preparing to read data 20000.bin
In total 21000 records were pushed onto the stack.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.