Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a generic class that creates an instance of a stack data structure. This i

ID: 3569102 • Letter: W

Question

Write a generic class that creates an instance of a stack data structure. This is a last in first out (LIFO) data structure. This means information put into it at the beginning can only be accessed at the end. Here's a picture:

Write a generic class that creates an instance of a stack data structure. This is a last in first out (LIFO) data structure. This means information put into it at the beginning can only be accessed at the end. Here's a picture: First download the Lab22Files Put the driver in your project DO NOT ALTER THE DRIVER! Write a class file called GenericStack that can be compose of generic types. Write an interior class called StackNode This means it is a private class within Stack Attributes of this interior class will be Data ? this is the data of the generic type Next ? a link to the next node Create the following constructors for this interior type A default constructor that sets everything to null A constructor where it has parameters for the data and the next link Back to GenericStack Attributes of this class are Head ? an instance of the DoubleListNode that always keeps track of the top of the list Create the following constructors Default ? sets both head and current to null Create the following Methods Push ? this takes in generic data and then creates a new instance of the stack node and then pushes it on as the new head Pop ? this returns the generic value of the current head and then removes it from the list by resetting the head to the head?s next link canPop ? this returns true if the head is not null printStack ? this iterates through the stack printing the data values of each item in the stack addArrayList ? this takes in an array list with the same generic type as the class and then adds it to the stack. This is a good way to reverse these kinds of lists

Explanation / Answer

Here you go :)

//GenericStack class

import java.util.ArrayList;

public class GenericStack<T>
{
   class StackNode
   {
       private T data;
       private StackNode link;
      
       public StackNode()
       {
           this.data=null;
           this.link=null;
       }
   }
  
   private StackNode head,current;
  
   public GenericStack()
   {
       this.head=null;
       this.current=null;
   }
  
   public void push(T data)
   {
       StackNode n=new StackNode();
       n.data=data;
       n.link=this.head;
       this.head=n;
       this.current=this.head;
   }
  
   public T pop()
   {
       T t=this.head.data;
       this.head=this.head.link;
       this.current=this.head;
       return t;
   }
   public void printStack()
   {
       GenericStack<T> dum=this;
       while(dum.current!=null)
       {
           System.out.println(dum.current.data);
           dum.current=dum.current.link;
       }
   }
  
   public boolean canPop()
   {
       if(this.head!=null)return true;
       return false;
   }
  
   public void addArrayList(ArrayList<T> al)
   {
       for(int i=0;i<al.size();i++)
       {
           this.push(al.get(i));
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote