L04_lsq

# L04_lsq - COMP152 Spring 2010 D.Y. Yeung ADTs: Lists,...

This preview shows pages 1–12. Sign up to view the full content.

ADTs: Lists, Stacks and Queues Nyhoff: 6-8,11 COMP152 Spring 2010 D.Y. Yeung

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

View Full Document
Outline COMP152 2 Lists An array-based implementation of lists Linked lists with pointer implementation Stacks Operations and implementations Applications: decimal to binary conversion, parenthesis matching, infix to postfix conversion, postfix computation, expression tree, etc. Queues Operations and implementations Bin-sort and radix sort
Lists in Daily Life COMP152 3 Shopping list Job to-do list List of assignments for a course Dean's list Can you name some others?

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

View Full Document
Properties of Lists COMP152 4 Can have a single element Can have no elements Can be lists of lists We will look at the list as an abstract data type (ADT) Homogeneous Finite length Sequential elements
Basic Operations COMP152 5 Construct an empty list Determine whether or not the list is empty Insert an element into the list Delete an element from the list Traverse (iterate through) the list to Modify Output Search for a specific element Copy or save Rearrange

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

View Full Document
Designing a List Class COMP152 6 Should contain at least the following function members Constructor empty(): whether or not the list is empty insert(): insert an element into the list delete(): delete an element from the list display(): display all elements in the list Implementation involves Defining data members Defining function members from design phase
Array-Based Implementation of Lists COMP152 7 An array is a viable choice for storing list elements Elements are sequential It is a commonly available data type Algorithm development is easy Normally sequential ordering of list elements matches with that of array elements a[0] a[1] a[2] a[n-1] a[n] a[CAPACITY-1] Array: List: a 1 , a 2 , a 3 , , a n

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

View Full Document
Implementing Operations COMP152 8 Constructor Static array allocated at compile time Empty Check if size == 0 Traverse Use a loop from the 0 th element to size – 1
Implementing Operations COMP152 9 Insert Shift elements to right of insertion point Delete Shift elements back Also adjust size up or down 23 25 34 48 61 79 82 89 91 99 ? ? 23 25 34 48 56 61 79 82 89 91 99 ? 23 25 34 48 56 61 79 82 89 91 99 ? 23 34 48 56 61 79 82 89 91 99 ? ?

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

View Full Document
List Class with Static Array COMP152 10 Must deal with the issue of type declaration and the size CAPACITY Use typedef mechanism to define type For specific implementation of our class we simply fill in the desired type for Some_Specific_Type typedef Some_Specific_Type ElementType ElementType array[CAPACITY];
List Class Example (Static Array Implementation) COMP152 11 Declaration file ( List.h ) Note use of typedef mechanism outside the class This typedef means that it is a list of int Definition and implementation ( List.cpp ) Note considerable steps required for insert() and erase() functions Program to test the class ( listtester.cpp )

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 08/25/2010 for the course COMP COMP152 taught by Professor D.y.yeung during the Spring '10 term at HKUST.

### Page1 / 82

L04_lsq - COMP152 Spring 2010 D.Y. Yeung ADTs: Lists,...

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

View Full Document
Ask a homework question - tutors are online