03-lists

03-lists - Spring 2009 CS216: Program and Data...

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

View Full Document Right Arrow Icon
1 CS216: Program and Data Representation University of Virginia Computer Science Spring 2009 Aaron Bloomfield Yet More …C++ and Lists… Linked Lists
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 CS216 Roadmap Data Representation Program Representation Bits 01001010 Addresses, Numbers, Characters 0x42381a, 3.14, ‘x’ Objects “Hello” Arrays [‘H’,’i’,\0] Java code High-level language C++ code Low-level language Virtual Machine language JVML Assembly x86 Real World Problems Real World Physics
Background image of page 2
Abstract Data Types: Lists Linked Lists
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Abstract Data Types (ADT) • Set of objects with a set of operations that can be performed on the object • A C++ class is a way to implement an ADT –Hides implementation details from the programs that use them
Background image of page 4
5 Lists ADT • A 0 , A 1 , A 2 , A 3 ,… –size N –empty list – list of size 0 –A 0 is the first element –A i follows A i-1 –position of element A i is i
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 List ADT Operations printList – print the list makeEmpty – make the list empty find – find the position of an item in the list insert – insert an item to the list remove – remove an item from the list findKth – find the element at position K next – return the position of the successor previous – return the position of the predecessor
Background image of page 6
7 Example 34, 12, 52, 16, 12 A 0 , A 1 , A 2 , A 3 , A 4 • find(52) – returns 2 //position of 52 • insert(x,2) – 34, 12, x, 52, 16, 12 • remove(52) • find(1)
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 • Fixed capacity • printList – linear time • findKth – constant time • insert and delete - varies Implementations of Lists: Arrays A 0 A 2 A 1 A n-2 A n-1 0 1 2 n-2 n-1 pos
Background image of page 8
9 List Implementation: Linked List • Nodes – element – link (pointer!) to node containing successor – printList, find, findKth – linear time – remove – change one pointer – insert • get a new node ( new ) • adjust 2 pointers A 0 A 1 A 2 A 3
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Linked List insert() • Special cases –inserting to the front –inserting at the end –removing last node
Background image of page 10
11 List Iterators • Why are they needed? –position is needed for some list operations • find() • insert() • remove() –ListItr • maintains notion of position by maintaining a pointer to a particular ListNode
Background image of page 11

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

View Full DocumentRight Arrow Icon
C++ Templates Function and class templates C++ for Java Programmers, Chapter 7
Background image of page 12
13 Templates • Java –Uses inheritance to write type- independent code –Or generics • C++ –Uses templates • function templates • class templates
Background image of page 13

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

View Full DocumentRight Arrow Icon
14 Function Templates (Ch. 7) • Not an actual function – A pattern for a function. Expanded by compiler as needed // Return the maximum item in array a. // Comparable objects must provide operator< template <typename Comparable > const Comparable Comparable > & a ) { int maxIndex = 0; for( int i = 1; i < a.size( ); i++ ) if( a[ maxIndex ] < a[ i ] ) maxIndex = i; return a[ maxIndex ]; }
Background image of page 14
15 Example int main() { vector< int > v1(37); vector< double > v2(40); vector< string > v3(80); vector< IntCell
Background image of page 15

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

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

Page1 / 60

03-lists - Spring 2009 CS216: Program and Data...

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

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