Code of main class:
public class LBmain {
public static void main(String[]args)
{
LinkedStack list = new LinkedStack<>();
System.out.println(\"Let\'s make a List!\");
System.out.println(\"Push 3 times.\");
System.out.println(\"Check the size.\");
System.out.println(\"Peek the top element.\");
System.out.println(\"Pop three times.\");
System.out.println (\"The size now should be zero!\" + \"\ \");
list.Push(1);
list.Push(2);
list.Push(3);
System.out.println(list.toString());
list.Size();
list.Peek();
list.Pop();
list.Pop();
list.Pop();
list.Size();
}
public class LinkedStack implements Stack {
private int count;
private LinearNode top;
//-----------------------------------------------------------------
// Creates an empty stack using the default capacity.
//-----------------------------------------------------------------
public LinkedStack()
{
count = 0;
top = null;
}
@Override
public boolean IsEmpty()
{
if(top == null)
{
System.out.println(\"Stack is empty\");
}
return top == null;
}
@Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;}
@Override
public T Pop()
{
T result;
System.out.println(\"Lets pop the top element!\");
if (count == 0)
{
System.out.println(\"Pop operation failed. \"+ \"The stack is empty.\");
}
result = top.getElement();
top = top.getNext();
count--;
System.out.println(\"The element that we have poped is :\" + result);
return result;
}
Override
public String toString()
{
String result = \"\ \";
LinearNode current = top;
while (current != null)
{
result += current.getElement() + \"\ \";
current = current.getNext();
}
return result + \"\";
}
@Override
public T Peek() {
System.out.println(\"Lets peek the top element!\");
if(count == 0)
{
System.out.println(\"Peek failed stack is empty\");
}
System.out.println(\"The element that we have peeked is: \" + top.getElement());
return top.getElement();
}
@Override
public int Size() {
System.out.println(\"The size of the list now is: \" + count);
return count;
}
}
main.Peek();
main.Pop();
main.Pop();
main.Size();
main.toString();
}
}
@Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;
}
}
public class LinearNode {
private LinearNode next; //se guarda la referencia del Nodo
private T element; //Lista vacia
public LinearNode()
{
next = null;
element = null;
}
//-----------------------------------------------------------------
// Creates a node storing the specified element.
//-----------------------------------------------------------------
public LinearNode (T elem)
{
next = null;
element = elem;
}
//-----------------------------------------------------------------
// Returns the node that follows this one.
//-----------------------------------------------------------------
public LinearNode getNext()
{
return next;
}
//-----------------------------------------------------------------
// Sets the node that follows this one.
.
Code of main classpublic class LBmain { public static void m.pdf
1. Code of main class:
public class LBmain {
public static void main(String[]args)
{
LinkedStack list = new LinkedStack<>();
System.out.println("Let's make a List!");
System.out.println("Push 3 times.");
System.out.println("Check the size.");
System.out.println("Peek the top element.");
System.out.println("Pop three times.");
System.out.println ("The size now should be zero!" + " ");
list.Push(1);
list.Push(2);
list.Push(3);
System.out.println(list.toString());
list.Size();
list.Peek();
list.Pop();
list.Pop();
list.Pop();
list.Size();
}
public class LinkedStack implements Stack {
private int count;
private LinearNode top;
//-----------------------------------------------------------------
// Creates an empty stack using the default capacity.
//-----------------------------------------------------------------
public LinkedStack()
{
count = 0;
top = null;
}
@Override
public boolean IsEmpty()
2. {
if(top == null)
{
System.out.println("Stack is empty");
}
return top == null;
}
@Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;}
@Override
public T Pop()
{
T result;
System.out.println("Lets pop the top element!");
if (count == 0)
{
System.out.println("Pop operation failed. "+ "The stack is empty.");
}
result = top.getElement();
top = top.getNext();
count--;
System.out.println("The element that we have poped is :" + result);
return result;
}
Override
public String toString()
{
String result = " ";
LinearNode current = top;
while (current != null)
{
3. result += current.getElement() + " ";
current = current.getNext();
}
return result + "";
}
@Override
public T Peek() {
System.out.println("Lets peek the top element!");
if(count == 0)
{
System.out.println("Peek failed stack is empty");
}
System.out.println("The element that we have peeked is: " + top.getElement());
return top.getElement();
}
@Override
public int Size() {
System.out.println("The size of the list now is: " + count);
return count;
}
}
main.Peek();
main.Pop();
main.Pop();
main.Size();
main.toString();
}
}
@Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;
}
4. }
public class LinearNode {
private LinearNode next; //se guarda la referencia del Nodo
private T element; //Lista vacia
public LinearNode()
{
next = null;
element = null;
}
//-----------------------------------------------------------------
// Creates a node storing the specified element.
//-----------------------------------------------------------------
public LinearNode (T elem)
{
next = null;
element = elem;
}
//-----------------------------------------------------------------
// Returns the node that follows this one.
//-----------------------------------------------------------------
public LinearNode getNext()
{
return next;
}
//-----------------------------------------------------------------
// Sets the node that follows this one.
//-----------------------------------------------------------------
public void setNext (LinearNode node)
{
next = node;
}
//-----------------------------------------------------------------
// Returns the element stored in this node.
//-----------------------------------------------------------------
public T getElement()//asigna valor
{
5. return element;
}
public void setElement(T elem)
{
element = elem;
}
Solution
Code of main class:
public class LBmain {
public static void main(String[]args)
{
LinkedStack list = new LinkedStack<>();
System.out.println("Let's make a List!");
System.out.println("Push 3 times.");
System.out.println("Check the size.");
System.out.println("Peek the top element.");
System.out.println("Pop three times.");
System.out.println ("The size now should be zero!" + " ");
list.Push(1);
list.Push(2);
list.Push(3);
System.out.println(list.toString());
list.Size();
list.Peek();
list.Pop();
list.Pop();
list.Pop();
list.Size();
}
public class LinkedStack implements Stack {
private int count;
private LinearNode top;
//-----------------------------------------------------------------
// Creates an empty stack using the default capacity.
6. //-----------------------------------------------------------------
public LinkedStack()
{
count = 0;
top = null;
}
@Override
public boolean IsEmpty()
{
if(top == null)
{
System.out.println("Stack is empty");
}
return top == null;
}
@Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;}
@Override
public T Pop()
{
T result;
System.out.println("Lets pop the top element!");
if (count == 0)
{
System.out.println("Pop operation failed. "+ "The stack is empty.");
}
result = top.getElement();
top = top.getNext();
count--;
System.out.println("The element that we have poped is :" + result);
return result;
7. }
Override
public String toString()
{
String result = " ";
LinearNode current = top;
while (current != null)
{
result += current.getElement() + " ";
current = current.getNext();
}
return result + "";
}
@Override
public T Peek() {
System.out.println("Lets peek the top element!");
if(count == 0)
{
System.out.println("Peek failed stack is empty");
}
System.out.println("The element that we have peeked is: " + top.getElement());
return top.getElement();
}
@Override
public int Size() {
System.out.println("The size of the list now is: " + count);
return count;
}
}
main.Peek();
main.Pop();
main.Pop();
main.Size();
main.toString();
}
}
8. @Override
public void Push(T element)
{
LinearNode current = new LinearNode<>(element);
current.setNext(top);
top = current;
count++;
}
}
public class LinearNode {
private LinearNode next; //se guarda la referencia del Nodo
private T element; //Lista vacia
public LinearNode()
{
next = null;
element = null;
}
//-----------------------------------------------------------------
// Creates a node storing the specified element.
//-----------------------------------------------------------------
public LinearNode (T elem)
{
next = null;
element = elem;
}
//-----------------------------------------------------------------
// Returns the node that follows this one.
//-----------------------------------------------------------------
public LinearNode getNext()
{
return next;
}
//-----------------------------------------------------------------
// Sets the node that follows this one.
//-----------------------------------------------------------------
public void setNext (LinearNode node)