11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

100 Chap. 4 Lists, Stacks, <strong>and</strong> QueuesInsert 23:1312 20 8 313 12 20 8 30 1 2 3 4 50 1 2 3 4(a)(b)523013 12 20 8 31 2 3 4 5(c)Figure 4.3 Inserting an element <strong>at</strong> the head of an array-based list requires shiftingall existing elements in the array by one position toward the tail. (a) A listcontaining five elements before inserting an element with value 23. (b) The listafter shifting all existing elements one position to the right. (c) The list after 23has been inserted in array position 0. Shading indic<strong>at</strong>es the unused part of thearray.maintain this property. Inserting or removing elements <strong>at</strong> the tail of the list is easy,so the append oper<strong>at</strong>ion takes Θ(1) time. But if we wish to insert an element <strong>at</strong>the head of the list, all elements currently in the list must shift one position towardthe tail to make room, as illustr<strong>at</strong>ed by Figure 4.3. This process takes Θ(n) timeif there are n elements already in the list. If we wish to insert <strong>at</strong> position i withina list of n elements, then n − i elements must shift toward the tail. Removing anelement from the head of the list is similar in th<strong>at</strong> all remaining elements must shifttoward the head by one position to fill in the gap. To remove the element <strong>at</strong> positioni, n − i − 1 elements must shift toward the head. In the average case, insertion orremoval requires moving half of the elements, which is Θ(n).Most of the other member functions for Class AList simply access the currentlist element or move the current position. Such oper<strong>at</strong>ions all require Θ(1) time.Aside from insert <strong>and</strong> remove, the only other oper<strong>at</strong>ions th<strong>at</strong> might requiremore than constant time are the constructor, the destructor, <strong>and</strong> clear. Thesethree member functions each make use of the system free-store oper<strong>at</strong>ion new. Asdiscussed further in Section 4.1.2, system free-store oper<strong>at</strong>ions can be expensive.4.1.2 Linked ListsThe second traditional approach to implementing lists makes use of pointers <strong>and</strong> isusually called a linked list. The linked list uses dynamic memory alloc<strong>at</strong>ion, th<strong>at</strong>is, it alloc<strong>at</strong>es memory for new list elements as needed.A linked list is made up of a series of objects, called the nodes of the list.Because a list node is a distinct object (as opposed to simply a cell in an array), it isgood practice to make a separ<strong>at</strong>e list node class. An additional benefit to cre<strong>at</strong>ing a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!