// CS210 - Algorithms and Data Structures I // Department of Computer Science // National University of Ireland, Maynooth // // Solutions to Laboratory Sheet 6 // Tom Naughton // November 2000 // //--------------------------------------------------------- //http://www.cs.may.ie/~tnaughton/cs210/ //========================================================= Solution to Problem 6.1: Almost identical code to that given in lectures. Except (i) use Object instead of int, and (ii) check if the Object is null within the push() method. Assume stack data structure is: Object a[]; void push(Object e) { /* Push object if it is not null and if a[] is not full. */ if (e == null) { throw new RuntimeException("null passed to push(e)."); } else if isFull() { throw new RuntimeException("Cannot push(e). Stack is full."); } else { topOfStack++; a[topOfStack] = e; } } Object pop() { /* Pop an element if a[] is not empty. */ if isEmpty() { throw new RuntimeException("Cannot pop(). Stack is empty."); } else { topOfStack--; return a[(topOfStack + 1)]; } } boolean isEmpty() { /* Return true if a[] is empty. */ return (topOfStack == -1); }