# Lists_ linked lists_ and their implementations

Document Sample

```					Lists, linked lists, and their
implementations

Ken Nguyen
Spring 2002
Introduction
• A vector has two disadvantages:
– It is not efficient when the number of elements to be
stored varies widely during a program execution.
– It is expensive to add or remove items anywhere except
at the end (possibly the beginning)
• List: A list is a kind of sequence that supports
bidirectional iterators and allows constant time insert and
erase operations anywhere within the sequence, with
storage management handled automatically. Unlike vectors
many algorithms only need sequential access anyway.
Introduction cont …
• Linked list: elements are stored in a chain, with
each element containing a pointer to the next
element, (and a pointer to the previous element),
in the list. The last element contains a NULL
pointer.
elements.
• Linked list is usually implemented using node-
base. (array-base linked list are rare).
Basic ideas
template <class Object>
Front of list
struct Node {
a        b       c   d
Object element
Node * next;               Figure 17.1

};

Last node has the next pointer point to NULL.
Basic ideas cont . . .

Inserting a node into a list:
Node *tmp = new Node;              a                 b

tmp->element = x;               current
x

tmp->next = current->next;                tmp       Fig. 17.2
current->next = tmp;
Basic ideas cont…
a         x   a

current
Fig. 17.3
Deleting a node:
Node * deleteNode = current->next;
curent->next = current->next->next;
delete deleteNode;
• A header node holds no data but serves to
satisfy the requirement that every node has

a    b           c

Fig. 17.4

Fig. 17.5 an empty list
A list

Node    Node   Node    Node
A list

Node    Node   Node   Node
Node

Node   Node    Node     Node
Node

Node           Node   Node   Node

struct Node{
Object element;
Node *prev;
Node *next;
};
Node

Node   Node   Node   Node
newNode = new Node; newNode->element = x;
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode;
a         3                 b
2

1   x         4

Fig. 17.20
what has to be changed is the insert
function.
Sample Implementations
•   list.h
•   list.cpp
STL List Class
list<T> v;                Default constructor O(1)
list<T> v(aList);         Copy constructor O(n)
v = aList;                Assignment O(n)
v.swap(aList);            Swap values with another list O(1)
v.remove(value);          Remove all occurrences of value O(n)
v.remove_if(predicate);   Remove all values that match the condition
O(n)
list<T>::iterator itr;    Declare a new iterator O(1)
v.begin();                Starting iterator O(1)
v.end();                  Ending iterator O(1)
v.rbegin();               Starting backward moving iterator O(1)
v.rend()                  Ending backward moving itarator O(1)
v.merge(aList);           Merge with another ordered list O(n)
v.sort();                 Place elements in accending order O(nlogn)
STL List Examples
#include <list>
list<int> list_one;
list_one.push_front(5);
list_one.push_back(10);
int a = list_one.front();
...
list_one.remove(4);
list_one.remove_if(even); //assume even is a functor
list<int>::iterator start = list_one.begin();
list<int>::iteartor stop = list_one.end();
list_one.erase(start,stop);
STL List Examples cont . . .
list<int>::reverse_iterator current = list_one.rbegin();
while (current != list_one.rend())
cout << *current++ << “        ”;

list_one.sort();
list_one.sort(intCompare);
//assume intCompare is a functor

int num = 0;
count(list_one.begin(), list_one.end(), 5, num);
// using generic count function on list

copy(list_one.begin(), list_one.end(), list_two.begin());
//copy list_one into list_two starting at the first element.
//list_one = {1, 2, 3}, list_two = {6,7,8,9} => list_two = {1, 2, 3, 9}

list<int>::iterator five = find(list_one.begin(), list_one.end(), 5);
Sequences and containers
bitset      <bitset>
deque       <deque>
list        <list>
queue       <queue>
stack       <stack>
Vector      <vector>
STL Sequence Container
operations
expression         return   assertion/note
type     pre/post-condition
X(n, t)                     post: size() == n.
X a(n, t);                  constructs a sequence with n
copies of t.
X(i, j)                     post: size() == distance
X a(i, j);                  between i and j.
constructs a sequence equal
to the range [i,j).
Container operations cont. . .
a.insert(p,t)    iterator    inserts a copy of t before p.
a.insert(p,n,t) result is   inserts n copies of t
not used    before p.
a.insert(p,i,j) result is    inserts copies of elements in
not used    [i,j) before p.
a.erase(q)      result is   erases the element pointed to
not used    by q.
a.erase(q1,q2 result is     erases the elements in the
)               not used    range [q1,q2).
Partial Summary of
STL List Class operations
Iterators:
•   iterator begin();                        O(1)
•   const_iterator begin() const;            O(1)
•   iterator end();                          O(1)
•   const_iterator end() const;              O(1)
•   reverse_iterator rbegin();               O(1)
•   const_reverse_iterator rbegin() const;   O(1)
•   reverse_iterator rend();                 O(1)
•   const_reverse_iterator rend() const;     O(1)
Partial Summary cont . . .
Capacity:
• bool empty() const;                O(1)
• size_type size() const;            O(n)
Element access:
•   reference front();               O(1)
•   const_reference front() const;   O(1)
•   reference back();                O(1)
•   const_reference back() const;    O(1)
Partial Summary cont . . .
Modifiers:
• void push_front(const T& x);                    O(1)
• void pop_front();                               O(1)
• void push_back(const T& x);                     O(1)
• void pop_back();                                O(1)
• iterator insert(iterator position, const T& x = T());
O(1)
• void erase(iterator position);                  O(1)
• void erase(iterator position, iterator last);   O(1)
• void swap(list<T,Allocator>&);                  O(1)
Partial Summary cont . . .
• void remove(const T& value);
O(n)
• void merge(list<T,Allocator>& x); O(n)
• void reverse();                   O(n)
• void sort();                O(nlogn)
Summary
• Linked list and its designs.
• List in STL.
• Sequential containers and their operations.

```
DOCUMENT INFO
Categories:
Tags:
Stats:
 views: 3 posted: 3/4/2012 language: pages: 26