CSE Cheet Sheet 3

CSE Cheet Sheet 3 - 1) Trace the output of the following...

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

View Full Document Right Arrow Icon
1) Trace the output of the following Linked List code, which uses an Iterator. import java.util.*; public class LinkedListTester { public static void main ( String [] args ) { LinkedList< String > list = new LinkedList< String > () ; ListIterator< String > iterator = list.listIterator () ; iterator.add ( "Blue" ) ; System .out.println ( list.toString ()) ; iterator.add ( "Red" ) ; System .out.println ( list.toString ()) ; iterator.add ( "Green" ) ; System .out.println ( list.toString ()) ; iterator = list.listIterator () ; iterator.next () ; iterator.add ( "Orange" ) ; System .out.println ( list.toString ()) ; iterator = list.listIterator () ; iterator.next () ; iterator.remove () ; System .out.println ( list.toString ()) ; }} Output: [Blue]/[Blue, Red]/[Blue, Red, Green] [Blue, Orange, Red, Green]/[Orange, Red, Green] Linked Lists consist of Nodes with links to other Nodes in a chain-like fashion. 2) Queue methods: To add to the Queue: myqueue.offer(itemToAdd) To see the item at the head of the Queue: myqueue.peek( ) To remove an item from the Queue: myqueue.remove( ) Remember a Queue is FIFO (First In First Out), like a line at the bank, the person at the head of the line has been waiting the longest and is next in line for service. 3) Trace the output of the following Stack code. import java.util.*; public class StackTester { public static void main ( String [] args ) { Stack< String > mystack = new Stack< String > () ; mystack.push ( "Blue" ) ; System .out.println ( mystack.toString ()) ; mystack.push ( "Red" ) ; System .out.println ( mystack.toString ()) ; mystack.push ( "Green" ) ; System .out.println ( mystack.toString ()) ; System .out.println ( mystack.pop ()) ; System .out.println ( mystack.toString ()) ; mystack.push ( "Orange" ) ; mystack.push ( "Yellow" ) ; System .out.println ( mystack.toString ()) ; System .out.println ( mystack.peek ()) ; System .out.println ( mystack.toString ()) ; mystack.pop () ; mystack.pop () ; mystack.pop () ; System .out.println ( mystack.toString ()) ; }} Output:[Blue]/[Blue, Red]/[Blue, Red, Green]/Green
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/11/2008 for the course CSE 205 taught by Professor Matasu during the Spring '08 term at ASU.

Page1 / 3

CSE Cheet Sheet 3 - 1) Trace the output of the following...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online