In: Computer Science
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;
}
}
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.