Question

In: Computer Science

IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...

IN JAVA:

Repeat Exercise 28, but add the methods to the LinkedStack class.

Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class.

- String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable toString method.

- intsize()—returnsacountofhowmanyitemsarecurrentlyonthestack.Do not add any instance variables to the LinkStacked class in order to implement this method.

- void popSome(int count)—removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack .

- booleanswapStart()—if there are less than two elements on the stack returns false; otherwise it reverses the order of the top two elements on the stack and returns true.

- T poptop( )—the “classic” pop operation, if the stack is empty it throws StackUnderflowException; otherwise it both removes and returns the top element of the stack.

package ch02.stacks;

import support.LLNode;

public class LinkedStack implements StackInterface
{
protected LLNode top; // reference to the top of this stack

public LinkedStack()
{
top = null;
}

public void push(T element)
// Places element at the top of this stack.
{
LLNode newNode = new LLNode(element);
newNode.setLink(top);
top = newNode;
}

public void pop()
// Throws StackUnderflowException if this stack is empty,
// otherwise removes top element from this stack.
{
if (isEmpty())
throw new StackUnderflowException("Pop attempted on an empty stack.");
else
top = top.getLink();
}

public T top()
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element of this stack.
{
if (isEmpty())
throw new StackUnderflowException("Top attempted on an empty stack.");
else
return top.getInfo();
}

public boolean isEmpty()
// Returns true if this stack is empty, otherwise returns false.
{
return (top == null);
}

public boolean isFull()
// Returns false - a linked stack is never full
{
return false;
}

}

Solutions

Expert Solution

package ch02.stacks;

import support.LLNode;

public class LinkedStack implements StackInterface
{
protected LLNode top; // reference to the top of this stack

public LinkedStack()
{
top = null;
}

public void push(T element)
// Places element at the top of this stack.
{
LLNode newNode = new LLNode(element);
newNode.setLink(top);
top = newNode;
}

public void pop()
// Throws StackUnderflowException if this stack is empty,
// otherwise removes top element from this stack.
{
if (isEmpty())
throw new StackUnderflowException("Pop attempted on an empty stack.");
else
top = top.getLink();
}

public T top()
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element of this stack.
{
if (isEmpty())
throw new StackUnderflowException("Top attempted on an empty stack.");
else
return top.getInfo();
}

public boolean isEmpty()
// Returns true if this stack is empty, otherwise returns false.
{
return (top == null);
}

public boolean isFull()
// Returns false - a linked stack is never full
{
return false;
}

public T poptop()
//if the stack is empty it throws StackUnderflowException;
//otherwise it both removes and returns the top element of the stack
{
   if(isEmpty())
       throw new StackUnderflowException("Pop attempted on an empty stack.");
   T info = top.getInfo();
   top = top.getLink();
   return info;
}

public int size()
//returns a count of how many items are currently on the stack
{
   LLNode tmp = top;
   int count=0;
   while(tmp != null)
   {
       count++;
       tmp = tmp.getLink();
   }
   return count;
}

public boolean booleanswapStart()
//if there are less than two elements on the stack returns false;
//otherwise it reverses the order of the top two elements on the stack and returns true
{
   if(size() < 2)
       return false;
      
   T p = top.getInfo();
   top = top.getLink();
  
   T q = top.getInfo();
   top = top.getLink();

   LLNode newNode = new LLNode(p);
   newNode.setLink(top);
   top = newNode;
  
   newNode = new LLNode(q);
   newNode.setLink(top);
   top = newNode;

   return true;
}

public void popSome(int count)
//removes the top count elements from the stack;
//throws StackUnderflowException if there are less than count elements on the stack .
{
   if(size()<count)
       throw new StackUnderflowException("Less than count elements on the stack.");
   for(int i=0; i<count; i++)
   {
       top = top.getLink();
   }
}

public String toString()
//creates and returns a string that correctly represents the current stack.
{
   String s = "";
   LLNode tmp = top;
   while(tmp != null)
   {
       s = s + tmp.getInfo() + " ";
       tmp = tmp.getLink();
   }
   return s;
}

}

//Driver class
class Driver
{
   //main method
   public static void main (String[] args) throws StackUnderflowException
   {
       //create a stack of Integers
       LinkedStack<Integer> stack = new LinkedStack<Integer>();
      
       stack.push(10);
       stack.push(20);
       stack.push(30);
       stack.push(40);
       stack.push(50);
      
       //print the stack after push operation
       System.out.println (stack);
      
       boolean fg = stack.booleanswapStart();
      
       //print the stack after booleanswapStart operation
       System.out.println (stack);
      
       //print the size of the stack
       System.out.println ("Size of the Stack = " + stack.size());
      
       stack.popSome(2);
      
       //print the stack after popSome operation
       System.out.println (stack);
      
       System.out.println ("Popped " + stack.poptop());
       System.out.println ("Popped " + stack.poptop());
       System.out.println ("Popped " + stack.poptop());
      
       //print the size of the stack
       System.out.println ("Size of the Stack = " + stack.size());
      
   }
}

Note: I have added the methods to the LinkedStacked class and write a test driver class. But since you have not add the full code I have not able to run it.  Whether you face any problem or need any modification then share with me in the comment section, I'll happy to help you.


Related Solutions

Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
The todo section in java please LinkedStack class (provided as a skeleton) is to implement TextbookStackInterface...
The todo section in java please LinkedStack class (provided as a skeleton) is to implement TextbookStackInterface using chain of nodes to manage the data (see the textbook implementation). The instance variable topNode is defined as chain head reference. The empty stack should have this variable set to null. Skeleton of LinkedStack class is provided. Please note that in addition to all the methods defined in the TextbookStackInterface there are two methods: displayStack and remove that you also need to implement:...
Add The following methods to the LinkedQueue class and create a test driver for each to...
Add The following methods to the LinkedQueue class and create a test driver for each to show they work correctly. In order to practice your linked list coding skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously defined public methods of the class. a. String toString () creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging the class...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
Implement the following functions in C: LinkedStack* InitStack(); int IsEmptyStack(LinkedStack* LStack); LinkedStack* LinkedPush(LinkedStack* LStack, Eletype value);...
Implement the following functions in C: LinkedStack* InitStack(); int IsEmptyStack(LinkedStack* LStack); LinkedStack* LinkedPush(LinkedStack* LStack, Eletype value); // Return the head of stack. Eletype LinkedPop(LinkedStack* LStack); // Return the value of popped element. Eletype FrontStack(LinkedStack* LStack); // Return the value of the element on the top of stack. InitStack() returns an empty stack and IsEmptyStack(LinkedStack* LStack) determines if the stack is empty or not.
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT