ch04 - Ch 4 Linked Lists Motivation Pointers in C+ Dynamic...

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

View Full Document Right Arrow Icon
Ch 4 – Linked Lists Motivation Pointers in C++ Dynamic Size Arrays Linked List Nodes Connecting Nodes Linked List Operations Print List Create List Insert List Multiple Insertions Insert at End Sorted Insert List Empty? List Full? Searching A List Delete From List Delete Code Implementation Example Using Head and Tail Pointers Using Doubly Linked Lists Insertion into Doubly Linked List Deletion from Doubly Linked List
Background image of page 1

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

View Full DocumentRight Arrow Icon
Motivation • Arrays fast to access but fixed size. • Linked list invented for dynamic data of variable length. • Code is more complex and often slower. • Programmers must make space/time trade-off when selecting data structure.
Background image of page 2
Pointers in C++ float x; variable contains float value float *y; variable contains address of a float variable y = &x; initializes y to have address of x *y = 4.2 stores 4.2 in variable x y = new float; allocates space for new variable on heap delete y gives space back to system *y = 1.7; illegal after delete called, can make program crash
Background image of page 3

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

View Full DocumentRight Arrow Icon
Dynamic Size Arrays int *data; integer pointer int size = 42 variable contains address of a float variable data = new int[size] allocates array of integers on heap data[7] = 19 use array normal way *(data + 7) = 91 pointer arithmetic used to access array. (faster but ugly) delete []data; gives space back to system (failure to delete causes memory leak)
Background image of page 4
Linked List Nodes • We create dynamic size data structure by chaining together chunks of memory. • A linked list node contains N data elements and 1 or 2 pointers to other data in the list struct Node { int ID; float GPA; Node * next; } data fields pointer to another Node record
Background image of page 5

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

View Full DocumentRight Arrow Icon
Linked List Nodes • We draw nodes as boxes and connect nodes with arrows from pointer fields. head ID GPA next ID GPA next null pointer
Background image of page 6
Connecting Nodes • pointer to Node record • allocate space on heap head 123456 3.7 next Node * ptr ptr = new Node; ptr->ID = 123456; ptr->GPA = 3.7; ptr->next = NULL; set data fields
Background image of page 7

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

View Full DocumentRight Arrow Icon
Connecting Nodes head 123456 3.7 next Node * ptr2 ptr = new Node; ptr2->ID = 567890; ptr2->GPA = 2.5; ptr2->next = ptr; create another node and add to chain 567890 2.5 next
Background image of page 8
Linked List Operation Linked list do not allow random access like arrays so we must write functions to store and retrieve data. A typical linked list ADT has the following operations:
Background image of page 9

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

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

This note was uploaded on 12/01/2011 for the course CSCE 2014 taught by Professor Gauch during the Spring '11 term at Arkansas.

Page1 / 31

ch04 - Ch 4 Linked Lists Motivation Pointers in C+ Dynamic...

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

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