Other List Structures

Post on 03-Jan-2016

33 views 0 download

description

Other List Structures. Reading: Chapter 8 Homework: p.403, #1, 2, 3; p. 448, #1, 3, 8, 10, 15. Design Alternatives. Consider alternative designs for the actual implementation of the list For example, array singly linked list doubly linked list circular linked list - PowerPoint PPT Presentation

transcript

Other List Structures

Reading: Chapter 8Homework: p.403, #1, 2, 3;

p. 448, #1, 3, 8, 10, 15

Design Alternatives

• Consider alternative designs for the actual implementation of the list

• For example,– array– singly linked list– doubly linked list– circular linked list

• How would the choice be made?• Might it change in a later version?

Array-Based List

item 1 item 2 item 3 item 4 item 5

Singly Linked List

item 1 item 2 item 3 item 4 item 5

null

Doubly Linked List

item 1 item 2 item 3 item 4 item 5

null

null

Circular Linked List

item 1

item 2 item 3

item 4item 5

Head and Tail Pointers

item 1 item 2 item 3 item 4 item 5

null

Lists Datatype

• Interface should allow– creation of empty list– tests for empty– reinitialization to empty– access to front and back– insertion and removal– operators for moving iterator to

beginning and end of list (begin() and end() );

STL List Constructors

list<T> lst;Creates an empty list for elements of type

T.

list<T> lst(n, e);Creates list of n copies of e.

list<T> lst(beg, end);Creates list with copies from range

beg..end.

lst.~list<T>();Destroys all elems and frees memory.

STL List: Front/Back Push/Pop

item = lst.front(); First element.item = lst.back(); Last element.

lst.push_front(item);

Adds item to front of lst.

lst.pop_front();Removes first element of lst.

lst.push_back(item);

Adds item to end of lst.

lst.pop_back();Removes last element of lst.

STL List: Entire List Methods

i = lst.size(); Number of elements.b = lst.empty(); True if empty. Use instead of lst.size()==0,

which may traverse entire list in some implementations.

lst = lst2; Assigns lst2 to lst.lst.clear(); Removes all elements.lst.assign(n, e); Replaces existing elements with n copies of e.

lst.sort(); Sorts list (stable) with <.lst.unique(); Assumes lst is sorted. Removes subsequent

consecutive equal values.

lst.reverse(); Reverses lst.lst.merge(lst2); Assumes lst and lst2 are sorted. Merges lst2

into lst to remain sorted.

Lists and Iterators

• Stack object had member data defined to specify access points

• Ok for stack since it allows only a single access point

• Each container class is equipped with an associated iterator class.

• Iterators maintain pointer to “current” element

IteratorIterator

A pointer-like objectA pointer-like object used to cycle through used to cycle through all the elements stored in a containerall the elements stored in a container

// Built-in type// Built-in type vector<int>::iterator p;vector<int>::iterator p; // Built-in values// Built-in values p = v.begin(); p = v.begin(); p != v.end()p != v.end()

List Iterator Datatype

• Interface provides– Operators for moving iterator to

beginning of list ( begin()) and end (end() )

– Operators for moving through the list (++ and --)

– Operator for returning the current item on the list (*)

– Generic algorithms (Find, Remove, etc.)

Traversing List using Iterator

# include <list>

list<int> L;L.push_back(0);L.push_front(1);L.push_back(2);

L.sort();

list<int>::iterator itr;

itr = L.begin(); while (itr != L.end()) { cout << *itr << " "; itr++; } // The values that are printed are 0 1 2

Adding Elements to a List

iter2 =

lst.assign(beg, end);

Sets list to copies from range beg..end.

iter2 =

lst.insert(iter, e); Inserts copy of e at iter position, returns its position.

lst.insert(iter, n, e); Inserts n copies of e starting at iter position.

lst.insert(iter, beg, end);

Inserts copies in range beg..end, starting at iter.

Erasing Elements from a List

iter2 =

lst.erase(iter); Removes element at iter, returns position of next.

iter2 =

lst.erase(beg, end);

Removes beg...end, returns position of next.