Вы находитесь на странице: 1из 4

1 IAS1223 Data Structures and Algorithms Lab 5 Stacks Session 2/2011/2012

Programming Exercise page 430, question number 4. Add the following operation to the class LinkedStackClass. void reverseStack(LinkedStackClass otherStack) This operation copies the elements of a stack in reverse order onto another stack. Consider the following statements: LinkedStackClass stack1; LinkedStackClass stack2; The statement: stack1.reverseStack(stack2); copies the elements of stack1 onto stack2 in reverse order. That is the top element of stack1 is the bottom element of stack2 and so on. The old contents of stack2 are destroyed and stack1 is unchanged. Write the definition of the method to implement the operation reverseStack. Also write a program to test the method reverseStack. Instructions 1. This is an individual assignment. 2. Submit the hardcopy and softcopy of the following: a. All your java source code files b. Test run output 3. Submission date is Wednesday 8 February 2012.

2
Type the following files:

StackUnderflowException.java

DataElement.java

IntElement.java

LinkedStackClass.java

StackException.java

StackOverflowException.java

StackProgram.java

//StackException.java public class StackException extends RuntimeException { public StackException() { } StackException(String msg) { super(msg); } } // StackUnderflowException.java public class StackUnderflowException extends StackException { public StackUnderflowException() { super("Stack Underflow"); } public StackUnderflowException(String msg) { super(msg); } } // StackOverflowException.java public class StackOverflowException extends StackException { public StackOverflowException() { super("Stack Overflow"); } public StackOverflowException(String msg) { super(msg); } } //DataElement.java public abstract class DataElement { public abstract boolean equals(DataElement otherElement); //Method to determine whether two objects contain the same data. //Postcondition: Returns true if this object contains the same data as the object otherElement; // otherwise, it returns false. public abstract int compareTo(DataElement otherElement); //Method to compare two objects. //Postcondition: Returns a value < 0 if this object is less than the object otherElement; // Returns 0 if this object is the same as the object otherElement. // Returns a value > 0 if this object is greater than the object otherElement. public abstract void makeCopy(DataElement otherElement); //Method to copy otherElement into this object. //Postcondition: The data of otherElement is copied into this object. public abstract DataElement getCopy(); //Method to return a copy of this object. //Postcondition: A copy of this object is created and a reference of the copy is returned. } //IntElement.java public class IntElement extends DataElement { protected int num; public IntElement() { num = 0;} //default constructor //constructor with a parameter //copy constructor

public IntElement(int x) { num = x; }

public IntElement(IntElement otherElement) { num = otherElement.num;} //Method to set the value of the instance variable num. //Postcondition: num = x;

3
public void setNum(int x) { num = x; } //Method to return the value of the instance variable num. //Postcondition: The value of num is returned. public int getNum() { return num; } } //LinkedStackClass.java public class LinkedStackClass { //Definition of the node private class StackNode { DataElement info; StackNode link; } private StackNode stackTop; //reference variable to the top element of the stack

//default constructor. Postcondition: stackTop = null public LinkedStackClass() { stackTop = null; } //Method to initialize the stack to an empty state. Postcondition: stackTop = null public void initializeStack() { stackTop = null; }// end initializeStack //Method to determine whether the stack is empty. //Postcondition: Returns true if the stack is empty; otherwise, returns false. public boolean isEmptyStack() { return(stackTop == null); } //Method to determine whether the stack is full. //Postcondition: Returns true if the stack is full; public boolean isFullStack() { return false; }

otherwise, returns false.

//Method to add newItem to the stack. //Postcondition: The stack is changed and newItem is added to the top of stack. public void push(DataElement newElement) { StackNode newNode; //reference variable to create the new node newNode = new StackNode(); //create the new node newNode.info = newElement.getCopy(); newNode.link = stackTop; //insert newNode before stackTop stackTop = newNode; //set stackTop to point to the top element } //end push //Method to return the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: If the stack is empty, the method throws StackUnderflowException; otherwise, a // reference to a copy of the top element of the stack is returned. public DataElement top() throws StackUnderflowException { if(stackTop == null) throw new StackUnderflowException(); return stackTop.info.getCopy(); }//end top //Method to remove the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: The stack is changed and the top element is removed from the stack. // If the stack is empty, the method throws StackUnderflowException public void pop()throws StackUnderflowException { if(stackTop == null) throw new StackUnderflowException(); stackTop = stackTop.link; //advance stackTop to the next node }//end pop

4
public void reverseStack(___________ stack) { } //Type your operation here

}//end linkedStackClass.java //StackProgram.java public class StackProgram { public static void main(String[] args) {

//Write your program here

} }

Вам также может понравиться