Help needed with stack methods....

Hello all,

I am new to the programming world and I am having some trouble with filling the push(), pop(), and empty() methods for the following code. Any help would be much appreciated.

Thanks - Hark

public class IntStack2

{

private IntNode top;

// --

// Constructor -- initializes top and creates array

// --

public IntStack2()

{

top = null;

}

// --

// Adds new element to top of stack.

// --

public void push(int val)

{

}

// --

// Removes and returns top element in stack.

// Assumes that the stack contains at least one element.

// --

public int pop()

{

}

// --

// Returns true if stack is empty, false otherwise.

// --

public boolean empty()

{

}

//*************************************************************

// An inner class that represents a stack element.

//*************************************************************

private class IntNode

{

private int val;

private IntNode next;

//

// Sets up the node

//

public IntNode(int val, IntNode next)

{

this.val = val;

this.next = next;

}

}

}

[1294 byte] By [Harka] at [2007-11-27 3:28:54]
# 1

try to understand the following code, it is very easy and simple, and you will be able to implement any Stack yet:

(this code from Java How to Program book by Deitel & Deitel)

// class to represent one node in a list

class ListNode

{

// package access members; List can access these directly

Object data;

ListNode nextNode;

// constructor creates a ListNode that refers to object

ListNode( Object object )

{

this( object, null );

} // end ListNode one-argument constructor

// constructor creates ListNode that refers to

// Object and to next ListNode

ListNode( Object object, ListNode node )

{

data = object;

nextNode = node;

} // end ListNode two-argument constructor

// return reference to data in node

Object getObject()

{

return data; // return Object in this node

} // end method getObject

// return reference to next node in list

ListNode getNext()

{

return nextNode; // get next node

} // end method getNext

} // end class ListNode

// class List definition

class List

{

private ListNode firstNode;

private ListNode lastNode;

private String name; // string like "list" used in printing

// constructor creates empty List with "list" as the name

public List()

{

this( "list" );

} // end List no-argument constructor

// constructor creates an empty List with a name

public List( String listName )

{

name = listName;

firstNode = lastNode = null;

} // end List one-argument constructor

// insert Object at front of List

public void insertAtFront( Object insertItem )

{

if ( isEmpty() ) // firstNode and lastNode refer to same object

firstNode = lastNode = new ListNode( insertItem );

else // firstNode refers to new node

firstNode = new ListNode( insertItem, firstNode );

} // end method insertAtFront

// insert Object at end of List

public void insertAtBack( Object insertItem )

{

if ( isEmpty() ) // firstNode and lastNode refer to same Object

firstNode = lastNode = new ListNode( insertItem );

else // lastNode's nextNode refers to new node

lastNode = lastNode.nextNode = new ListNode( insertItem );

} // end method insertAtBack

// remove first node from List

public Object removeFromFront() throws EmptyListException

{

if ( isEmpty() ) // throw exception if List is empty

throw new EmptyListException( name );

Object removedItem = firstNode.data; // retrieve data being removed

// update references firstNode and lastNode

if ( firstNode == lastNode )

firstNode = lastNode = null;

else

firstNode = firstNode.nextNode;

return removedItem; // return removed node data

} // end method removeFromFront

// remove last node from List

public Object removeFromBack() throws EmptyListException

{

if ( isEmpty() ) // throw exception if List is empty

throw new EmptyListException( name );

Object removedItem = lastNode.data; // retrieve data being removed

// update references firstNode and lastNode

if ( firstNode == lastNode )

firstNode = lastNode = null;

else // locate new last node

{

ListNode current = firstNode;

// loop while current node does not refer to lastNode

while ( current.nextNode != lastNode )

current = current.nextNode;

lastNode = current; // current is new lastNode

current.nextNode = null;

} // end else

return removedItem; // return removed node data

} // end method removeFromBack

// determine whether list is empty

public boolean isEmpty()

{

return firstNode == null; // return true if List is empty

} // end method isEmpty

// output List contents

public void print()

{

if ( isEmpty() )

{

System.out.printf( "Empty %s\n", name );

return;

} // end if

System.out.printf( "The %s is: ", name );

ListNode current = firstNode;

// while not at end of list, output current node's data

while ( current != null )

{

System.out.printf( "%s ", current.data );

current = current.nextNode;

} // end while

System.out.println( "\n" );

} // end method print

} // end class List

class StackInheritance extends List

{

// no-argument constructor

public StackInheritance()

{

super( "stack" );

} // end StackInheritance no-argument constructor

// add object to stack

public void push( Object object )

{

insertAtFront( object );

} // end method push

// remove object from stack

public Object pop() throws EmptyListException

{

return removeFromFront();

} // end method pop

} // end class StackInheritance

Good Luck.

Ahmad Elsafty

NourElsaftya at 2007-7-12 8:31:47 > top of Java-index,Java Essentials,New To Java...
# 2

Ok this is what I've done and the test class should count down from 22 to 0 by 2's (i.e. 22, 20, 18, ... 4, 2, 0) BUT, it only counts the njumber 22 endlessly. any more help, I would be greatful. Thank in advance.

public class IntStack2

{

private IntNode top;

// --

// Constructor -- initializes top and creates array

// --

public IntStack2()

{

top = null;

}

// --

// Adds new element to top of stack.

// --

public void push(int val)

{

top = new IntNode(val, top);

}

// --

// Removes and returns top element in stack.

// Assumes that the stack contains at least one element.

// --

public int pop()

{

int removedItem = top.val;

return removedItem;

}

// -

// Returns true if stack is empty, false otherwise.

// --

public boolean empty()

{

return top == null;

}

//*************************************************************

// An inner class that represents a stack element.

//*************************************************************

private class IntNode

{

private int val;

private IntNode next;

//

// Sets up the node

//

public IntNode(int val, IntNode next)

{

this.val = val;

this.next = next;

}

}//end inner class

}//end outter class

public class StackTest2

{

// --

// Pushes even integers up to 22 on a stack, then

// pops and prints them.

// --

public static void main(String[] args)

{

IntStack2 stack = new IntStack2();

//push some stuff on the stack

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

stack.push(i*2);

//pop and print

//should print 22,20,18,...,2,0

while (!stack.empty())

System.out.println(stack.pop());

}

}

Harka at 2007-7-12 8:31:47 > top of Java-index,Java Essentials,New To Java...
# 3

From your description, I clearly expected that your pop() methods just returns the most top element and does not pop - remove - it.

so your empty() method infinitly returns false.

this is your pop() method:

public int pop()

{

int removedItem = top.val;

return removedItem;

}

do you see?

Thanks.

Ahmad Elsafty

NourElsaftya at 2007-7-12 8:31:47 > top of Java-index,Java Essentials,New To Java...
# 4

Did you read carefully the method removeFromFront() of class List:

// remove first node from List

public Object removeFromFront() throws EmptyListException

{

if ( isEmpty() ) // throw exception if List is empty

throw new EmptyListException( name );

Object removedItem = firstNode.data; // retrieve data being removed

// update references firstNode and lastNode

if ( firstNode == lastNode )

firstNode = lastNode = null;

else

firstNode = firstNode.nextNode;

return removedItem; // return removed node data

} // end method removeFromFront

read it.

NourElsaftya at 2007-7-12 8:31:47 > top of Java-index,Java Essentials,New To Java...
# 5
YOU ARE MY HERO!!!I added top = top.next;and it works now. Thank you very much for your help! And you are right I didn't read it close enough. Attention to detail is something I need to work on! Hark
Harka at 2007-7-12 8:31:47 > top of Java-index,Java Essentials,New To Java...