lecture3a - Lecture3:PointerbasedListADT...

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

View Full Document Right Arrow Icon
Lecture 3 : Pointer-based List ADT  Implementation Part i. ECE 2574 Chao Huang Virginia Tech Fall 2009
Background image of page 1

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

View Full DocumentRight Arrow Icon
 C. Huang, Virginia Tech 2 Preliminaries Options for implementing an ADT List » Array-based implementation Array has a fixed size Data must be shifted during insertions and deletions » Pointer-based implementation (linked list) Linked list is able to grow in size as needed Does not require the shifting of items during insertions  and deletions
Background image of page 2
 C. Huang, Virginia Tech 3 Preliminaries Linked list insertion A linked list of integers Linked list insertion
Background image of page 3

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

View Full DocumentRight Arrow Icon
 C. Huang, Virginia Tech 4 Pointers A pointer contains the location, or address in memory, of a  memory cell » A statically allocated pointer declaration int *p; » A dynamically allocated pointer variable p = new int; » Initially undefined, but not  NULL As we check for  NULL  pointers when traversing lists, we  must be careful to correctly initialize pointers. 
Background image of page 4
 C. Huang, Virginia Tech 5 Statically Allocating Arrays Statically memory allocation » Its size is fixed » The size must be known at compiler time » The array exists only until the end of the scope in  which it was defined #define MSIZE 4; Main() { int size=4; ... . .. int array[]; int array[4]; int array[size]; int array[MSIZE]; ... . .. }
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 Dynamically Allocating Arrays Dynamically memory allocation » Its size need not be fixed » The size can be determined at run time » The array exists until it is explicitly freed by the  program #define MSIZE 4; Main() { int size=4; ... . .. int array=new int[4]; int *array=new int[4]; long *array=new int[4]; int *array=new int[size]; ... . .. } main()
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.

Page1 / 26

lecture3a - Lecture3:PointerbasedListADT...

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