l03-stack_qs

l03-stack_qs - CS112: Data Structures Lecture 03 Stacks and...

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

View Full Document Right Arrow Icon
CS112: Slides for Prof. Steinberg ʼ s lecture 1 Lecture 4 CS112: Data Structures CS112: Data Structures Lecture 03 Stacks and Queues
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS112: Slides for Prof. Steinberg ʼ s lecture 2 Lecture 4 Review: Generic Lists Review: Generic Lists Problem: suppose you want to have a list of Strings and a list of ints and … Class declarations and methods are almost identical Solution in java 1.5: “generics” type
Background image of page 2
CS112: Slides for Prof. Steinberg ʼ s lecture 3 Lecture 4 Generic List Generic List public class Node<E> { private E data; private Node<E> next; public Node<E>(E dat, Node<E> nxt){ data = dat; next = nxt; } public E getHead(Node<E> head){ E headData = head.data; return headData;} … Type parameter, eg String Declare an instance variable of type E, e.g. String
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS112: Slides for Prof. Steinberg ʼ s lecture 4 Lecture 4 Generic List Generic List In some method: Node<String> n1 = new Node<String>; String name = n1.getHead( ); Actual argument corresponding to type parameter E
Background image of page 4
CS112: Slides for Prof. Steinberg ʼ s lecture 5 Lecture 4 Circular Lists Circular Lists Problem: Cost to Access Tail Solution: point to tail, not head Problem: access to head. Solution: people Anne Bob null Dan people Anne Bob null Dan people Anne Bob Dan
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS112: Slides for Prof. Steinberg ʼ s lecture 6 Lecture 4 Circular List Circular List First node is people.next Variable place points at last node when place == people people Anne Bob Dan
Background image of page 6
CS112: Slides for Prof. Steinberg ʼ s lecture 7 Lecture 4 Insert at Head Insert at Head if(people == null){ people = new Node(newName, null); people.next = people; } else { Node newNode= new Node(newName, people.next); people.next = newNode; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
CS112: Slides for Prof. Steinberg ʼ s lecture 8 Lecture 4 Delete Head Delete Head For circular lists if (people == null ){ } else if (people == people. next ){ people = null ; } else { people. next = people. next . next ; }
Background image of page 8
CS112: Slides for Prof. Steinberg ʼ s lecture 9 Lecture 4 Other CLL Methods Other CLL Methods See resources => Java examples => fancy lists
Background image of page 9

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

View Full DocumentRight Arrow Icon
CS112: Slides for Prof. Steinberg ʼ s lecture 10 Lecture 4 DLL Methods DLL Methods See resources => Java examples => fancy lists
Background image of page 10
CS112: Slides for Prof. Steinberg ʼ s lecture 11 Lecture 4 Dummy Headers Dummy Headers Problem: delete head is different that delete elsewhere in list Change pointer to list as a whole vs change the next field of some node Solution: Keep an extra “dummy” node at the head of the list
Background image of page 11

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

View Full DocumentRight Arrow Icon
CS112: Slides for Prof. Steinberg ʼ s lecture 12 Lecture 4 Iterators Iterators Abstract data type: a container E.g. array or linked list Can do mostly the same things with them, main difference is cost Problem: one of the things I want to do is go through the data items one by one
Background image of page 12
CS112: Slides for Prof. Steinberg ʼ s lecture 13 Lecture 4 Processing Data Items Processing Data Items
Background image of page 13

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

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

This document was uploaded on 11/01/2011 for the course 198 112 at Rutgers.

Page1 / 51

l03-stack_qs - CS112: Data Structures Lecture 03 Stacks and...

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

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