Chapter Goals

Linked Lists

Linked Lists

Figure 1 A Linked List

Linked Lists

Figure 2 Adding a Node to a Linked List

Linked Lists

Figure 3 Removing a Node from a Linked List

Linked Lists

Linked Lists

std::list

Linked Lists

Iterators

Linked Lists

Linked Lists

Insertion

Linked Lists

Traversal

Linked Lists

Removal

Linked Lists – list1.cpp

Implementing Linked Lists

Implementing Linked Lists

class Node { public: Node(string s); private: string data; Node* previous; Node* next; friend class List; friend class Iterator; };

Implementing Linked Lists

Implementing Linked Lists

Implementing Linked Lists

Implementing Iterators

Implementing Iterators

Figure 4 Advancing an Iterator

Implementing Iterators

void Iterator::next() { assert(position != NULL) position = position->next; }

Implementing Iterators

Implementing Iterators

Implementing Insertion and Removal

push_back()

Adds the new value to the end of the list

Implementing Insertion and Removal

Figure 5 Appending a Node to the End of a Linked List

void List::push_back(string data) { Node* new_node = new Node(data); if (last == NULL) // List is empty { first = new_node; last = new_node; } else { new_node->previous = last; last->next = new_node; last = new_node; } }

Implementing Insertion and Removal

Inserting into the middle of a list

Implementing Insertion and Removal

Special cases:

Implementing Insertion and Removal

Figure 6 Inserting a Node into a Linked List

Implementing Insertion and Removal

if (remove == first) first = after; else before->next = after; if (remove == last) last = before; else after->previous = before;

Implementing Insertion and Removal

Figure 7 Removing a Node from a Linked List

Implementing Insertion and Removal – list2.cpp

The Efficiency of List and Vector Operations

We will analyze and compare the efficiency of fundamental operations on linked lists and vectors

These operations are:

The Efficiency of List and Vector Operations

To find the kth element of a linked list:

The Efficiency of List and Vector Operations

vector has the following attributes:

Figure 8 Internal Data Fields Maintained by Vector

The Efficiency of List and Vector Operations

The Efficiency of List and Vector Operations

Modifying the Middle

To add an element in the middle of a list (the cost of finding the location is considered separately):

The Efficiency of List and Vector Operations

To insert an element at position k of a vector (if relative order matters):

Figure 9 Inserting and Removing Vector Elements

The Efficiency of List and Vector Operations

The Efficiency of List and Vector Operations

The cost of 1280 push_back() operations:

The Efficiency of List and Vector Operations

The Efficiency of List and Vector Operations

Table 1 Execution Times for Container Operations
Operation Vector Linked List
Add/remove element at end O(1)+ O(1)
Add/remove element in the middle O(n) O(1)
Get kth element O(1) O(k)

If a pointer to the last element is maintained

Queues and Stacks

Queue

Figure 14 Stack and Queue Behavior

Queues and Stacks

STL queue

Queues and Stacks

Stacks

Queues and Stacks

STL stack

Stacks and Queues – fifolifo.cpp

Compares and contrasts the 2 containers

Stacks and Queues

Reverse Polish Notation (RPN) (postfix) calculator

Stacks and Queues – calc.cpp

Random Fact

Polish Notation

Random Fact (cont.)

Polish Notation

Random Fact (cont.)

Polish Notation

Standard Notation Łukasiewicz Notation RPN
3 + 4 + 3 4 3 4 +
3 + 4 × 5 + 3 * 4 5 3 4 5 * +
3 × (4 + 5) * 3 + 4 5 3 4 5 + *
(3 + 4) × 5 * + 3 4 5 3 4 + 5 *
3 + 4 + 5 + + 3 4 5 3 4 + 5 +

Random Fact (cont.)

Polish Notation

In 1972 Hewlett-Packard introduced the HP35

RPN users tend to be fanatical proponents of the system.