Other List Structures

17
Other List Structures Reading: Chapter 8 Homework: p.403, #1, 2, 3; p. 448, #1, 3, 8, 10, 15

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 of Other List Structures

Page 1: Other List Structures

Other List Structures

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

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

Page 2: Other List Structures

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?

Page 3: Other List Structures

Array-Based List

item 1 item 2 item 3 item 4 item 5

Page 4: Other List Structures

Singly Linked List

item 1 item 2 item 3 item 4 item 5

null

Page 5: Other List Structures

Doubly Linked List

item 1 item 2 item 3 item 4 item 5

null

null

Page 6: Other List Structures

Circular Linked List

item 1

item 2 item 3

item 4item 5

Page 7: Other List Structures

Head and Tail Pointers

item 1 item 2 item 3 item 4 item 5

null

Page 8: Other List Structures

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() );

Page 9: Other List Structures

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.

Page 10: Other List Structures

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.

Page 11: Other List Structures

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.

Page 12: Other List Structures

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

Page 13: Other List Structures

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()

Page 14: Other List Structures

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.)

Page 15: Other List Structures

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

Page 16: Other List Structures

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.

Page 17: Other List Structures

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.