Tutorial_8_Sol-for-viewing-printing

# Tutorial_8_Sol-for-viewing-printing -...

This preview shows pages 1–2. Sign up to view the full content.

CC2203 Data Structures and Algorithms Sem 2, 2009-2010 Tutorial 8 Solution Exercise 1 answer: (a) [10] [10, 18] [10, 18, 14] Peek 14 [10, 18, 14] Pop 14 [10, 18] [10, 18, 21] Pop 21 [10, 18] Pop 18 [10] Peek 10 [10] Pop 10 [] Pop Empty Stack! [] Pop Empty Stack! [] (b) import java.util.*; public class Stack extends Vector { public Object peek() { if (size() == 0 ) return "Empty Stack!"; return elementAt(size() - 1); } public Object pop() { Object object = peek(); if (!object.equals("Empty Stack!"))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: removeElementAt(size() - 1); return object; } public void push(Object object) { addElement(object); } } CC2203 Data Structures and Algorithms Sem 2, 2009-2010 (c) public static void reverse(Stack s) { Stack temp_s1 = new Stack(); Stack temp_s2 = new Stack(); while (!s.peek().equals("Empty Stack!")) temp_s1.push(s.pop()); while (!temp_s1.peek().equals("Empty Stack!")) temp_s2.push(temp_s1.pop()); while (!temp_s2.peek().equals("Empty Stack!")) s.push(temp_s2.pop()); }...
View Full Document

## This note was uploaded on 08/18/2011 for the course COMP 3868 taught by Professor Keithchan during the Summer '97 term at Hong Kong Polytechnic University.

### Page1 / 2

Tutorial_8_Sol-for-viewing-printing -...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online