10. Linked list

10. Linked list - Arrays vs Linked lists So far, an array...

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

View Full Document Right Arrow Icon
Arrays vs Linked lists ¾ So far, an array is the only choice we know for keeping a set of records in memory. Carol Lau” 2345678 S H Yip” 24567890 Fanny Wong” 25678901 . . . 0 1 99 gflist ¾ The characteristics of arrays are: ¾ Random access ¾ Size is fixed once created ¾ Difficult to insert/delete 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Arrays vs Linked lists y An alternative way for keeping a set of records is to use a linked list. A linked list is a sequence of dynamic ructures Each structure contains the data of a record structures. Each structure contains the data of a record and the address of next structure . The address of the first structure is kept in a pointer variable (i.e., address variable). flist arol Lau” HY ip” Fanny gflist Carol Lau 2345678 / S H Yip 24567890 y Wong” 25678901 . . . t d Pointer to fi t t d first node the first node last node NULL 2
Background image of page 2
A more complicated example #include <iostream> using namespace std; int main() { struct Node { string info; Node * next}; Node * ptr1 (0), * ptr2 (0); ptr1 = new Node; ptr2 = new Node; tr1 info = "P"; ptr1->info = P; ptr2->info = "M"; ptr1->next = ptr2; ptr2->next = 0; return 0; } 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
A more complicated example #include <iostream> using namespace std; int main() ptr1 { struct Node { string info; Node * next}; Node * ptr1 (0), *ptr2 (0), 0 ptr1 = new Node; ptr2 = new Node; tr1 info = "P"; 0 ptr2 ptr1->info = P; ptr2->info = "M"; ptr1->next = ptr2; ptr2->next = 0; return 0; } 4
Background image of page 4
A more complicated example #include <iostream> using namespace std; int main() ptr1 info { struct Node { string info; Node * next}; Node * ptr1 (0), *ptr2 (0), next ptr1 = new Node; ptr2 = new Node; tr1 info = "P"; 0 ptr2 ptr1->info = P; ptr2->info = "M"; ptr1->next = ptr2; ptr2->next = 0; return 0; } 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
A more complicated example #include <iostream> using namespace std; int main() ptr1 info { struct Node { string info; Node * next}; Node * ptr1 (0), *ptr2 (0), next ptr1 = new Node; ptr2 = new Node; tr1 info = "P"; ptr2 info next ptr1->info = P; ptr2->info = "M"; ptr1->next = ptr2; ptr2->next = 0; return 0; } 6
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/26/2011 for the course BENG 1002 taught by Professor Dr.x during the Winter '10 term at HKU.

Page1 / 26

10. Linked list - Arrays vs Linked lists So far, an array...

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

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