java- stack using a SingelyLinked list coding propblem

Anonymous
timer Asked: Nov 14th, 2016

Question description

what is the problem in my code? It's not working with the tester that my instructor wrote.. i think it's related to generics since i don't fully understand them and i'm sure it's in my LLStack class.

public class Node <T>{

T value;

Node<T> next;

public Node(){

value=null ;

next=null;

}

public Node(T v){

value=v;

next=null;

}

public Node(T v, Node<T> nextNode ){

value=v;

next=nextNode;

}

}


public interface Stack<T>

{

int size();

boolean empty();

void push (T element);

T pop() throws StackEmptyException;

T peek() throws StackEmptyException;

void makeEmpty();

}

public class StackEmptyException extends Exception

{

public StackEmptyException(String msg)

{

super(msg);

}

}


public class LLStack<T> extends Node<T> implements Stack<T> {

Node<T> head;

int count;

public LLStack(){

head = null;

count = 0;

}

public int size(){

return count;

}

public boolean empty(){ // not sure

return size() == 0;

}

public void push (T element){

Node n = new Node(element);

if (empty()){

n = head;

}else {

n.next = head;

head = n;

}

count ++;

}

public T pop() throws StackEmptyException{

T val = head.value;

if (!empty()) {

head = head.next;

count --;

return val;

}

else {

throw new StackEmptyException("Nothing to pop!");

}

}

public T peek() throws StackEmptyException{

T val = head.value;

if (!empty()){

return val;

}else

throw new StackEmptyException("Nothing to peak!, Stack is empty");

}

public void makeEmpty(){

count =0;

head = null;

}

}




public class StackTester

{

public static void testOne (Stack<Integer> s)

{

try

{

if (s.size() != 0 || !s.empty())

System.out.println("1: Failed size or empty.");

s.push(1);

s.push(2);

if (s.size() != 2 || s.empty())

System.out.println("2: Failed size or empty.");

if (!s.pop().equals(2))

System.out.println("3: Failed pop");

if (!s.peek().equals(1))

System.out.println("4: Failed peek");

if (!s.pop().equals(1))

System.out.println("5: Failed pop");

if (s.size() != 0 || !s.empty() )

System.out.println("6: Failed size or empty.");

}

catch (StackEmptyException e)

{

System.out.println(e);

}

}

public static void testTwo (Stack<Integer> s)

{

try

{

for (int i = 0; i < 100; i++)

{

s.push(i);

}

if (s.size() != 100)

System.out.println("7: Failed size.");

for (int i = 99; i >= 0; i--)

{

if (!s.pop().equals(i))

{

System.out.println("Failed pop for: " + i);

break;

}

}

}

catch (StackEmptyException e)

{

System.out.println("Failed testTwo.");

System.out.println(e);

}

}

public static void testThree (Stack<Integer> s)

{

try

{

s.makeEmpty();

s.pop();

System.out.println("Failed empty stack test.");

}

catch (StackEmptyException e)

{

/* If we get here, we

* passed the previous test.

*/

}

}

public static void main (String args[])

{

LLStack<Integer> s1 = new LLStack<Integer>();

testOne(s1);

//testTwo(s2);

//testThree(s3);

}

}

Tutor Answer

(Top Tutor) Studypool Tutor
School: University of Maryland
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags
Study Guides

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors