CS142 Introduction to Computer Science II - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CS142 Introduction to Computer Science II

Description:

Thus iter a_list.end() is not a meaningful test. ... iter; The postfix increment operator saves the previous value and then performs the increment. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 20
Provided by: janicets
Category:

less

Transcript and Presenter's Notes

Title: CS142 Introduction to Computer Science II


1
CS142Introduction toComputer Science II
  • ADT List
  • 3-11-2008

2
Announcements CS Colloquia
  • Tues, Mar 11 Wensheng Wu (Databases)
  • Meet with students 215 245, COSI/VR
  • Talk Schema matching, 300, SC342
  • Thurs, Mar 13 Kaiqi Xiong (Networks)
  • Meet with students 215 245, COSI/VR
  • Talk _at_ 300, SC342
  • Fri, Mar 14 Joachim Stahl (Vision)
  • Talk Image Segmentation, 12 noon, SC356
  • Meet with students 300 400, COSI/VR

3
CS142
  • Review of new and delete operators
  • Linked lists
  • Singly linked lists
  • Doubly linked lists
  • Circular linked lists
  • HW5 due Tues. 3/11

4
A List Node
  • A node contains
  • A data item
  • One or more links
  • A link is a pointer to a list node
  • The node class is usually defined inside another
    class
  • It is a hidden inner class
  • The details of a node should be kept private

5
List Nodes for Singly-Linked Lists
6
List Nodes for Singly-Linked Lists
  • struct Node
  • Item_Type data
  • Node next
  • // Constructor
  • Node(const Item_Type data_item,
  • Node next_ptr NULL)
  • data(data_item), next(next_ptr)

7
Removing a node from a single-linked list
Node ptr tom-gtnext tom-gtnext
tom-gtnext-gtnext delete ptr
8
Inserting a Node into a Single Linked List
Node bob new Node("Bob") bob-gtnext
harry-gtnext // step 1 harry-gtnext bob // step
2
9
Doubly-Linked Lists
  • Limitations of a singly-linked list include
  • Can insert only after a referenced node
  • Removing node requires pointer to previous node
  • Can traverse list only in the forward direction
  • We can remove these limitations
  • Add a pointer in each node to the previous node
    doubly-linked list

10
Doubly-Linked Lists, The Diagrams
11
Inserting into a Double-Linked List
DNode sharon new DNode("Sharon") // Link new
DNode to its neighbors sharon-gtnext sam //
Step 1 sharon-gtprev sam-gtprev // Step 2
12
Inserting into a Double-Linked List
// Link old predecessor of sam to new
predecessor. sam-gtprev-gtnext sharon // Step
3 // Link to new predecessor. sam-gtprev sharon
// Step 4
13
Removal from a Double-Linked List
harry-gtprev-gtnext harry-gtnext // Step
1 harry-gtnext-gtprev harry-gtprev // Step
2 delete harry
14
Circular Lists
  • Circular doubly-linked list
  • Link last node to the first node, and
  • Link first node to the last
  • Advantages
  • Can easily keep going past ends
  • Can visit all elements from any starting point
  • Can never fall off the end of a list
  • Disadvantage code must avoid infinite loop!
  • Can also build singly-linked circular lists
  • Traverse in forward direction only

15
Implementing a Circular List
16
The iterator
  • Suppose we want to access each element of a list
    in a loop
  • for (Item_Type index 0 index lt a_list.size()
    index)
  • // Do something with next_element, the element
  • // at position index
  • Item_Type next_element a_listindex // not
    valid
  • The subscripting operator (operator) is not
    defined for the list class.
  • Instead an iterator is used to access elements in
    a list.

17
The iterator (2)
18
Using an Iterator
  • We use an iterator like a pointer.
  • // Access each list element and process it
  • for (listltTgtiterator iter a_list.begin()
  • iter ! a_list.end() iter)
  • // Do something with next element (iter)
  • Item_Type next_element iter
  • . . .

19
Notes on using iterators in loops
  • We test for the end of the loop with the
    expression
  • iter ! a_list.end()
  • The order of individual iterator values is not
    meaningful.Thus iter lt a_list.end() is not a
    meaningful test.
  • We increment the iterator using the expression
  • iter
  • The postfix increment operator saves the
    previous value and then performs the increment.
    Since this previous value is not used, it is
    more efficient to use the prefix increment
    operator.
Write a Comment
User Comments (0)
About PowerShow.com