Docsity

Linked Lists Part I - CSCI-1200 Computer Science II, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI-1200 Computer Science II — Fall 2007
Lecture 10 — Linked Lists, Part I
Review from Lecture 9
•Returning const references to member variables provides access to important variables without the cost of
copying and without the danger of allowing them to be changed.
•Lists vs. vectors
•Iterators (vs. subscripting)
•Special properties of vector iterators
•The erase function for vectors and lists
•Sieve of Eratosthenes
Today’s Class: Linked Lists, Part I
•Motivation: implementation of the std::list container class.
•Introductory example on linked lists.
•Basic linked list operations:
–Stepping through a list
–Push back
–Insert
–Remove
•Common mistakes
10.1 Motivation
•Thus far our discussion of how list<T> is implemented has been only intuitive: it is a “chain” of objects.
•Now we will look at the mechanism — linked lists.
•Learning this mechanism is good background for higher-level courses where the design of novel data structures
is important.
10.2 Objects with Pointers / Linking Objects
•The two fundamental mechanisms of linked lists are:
–creating objects with pointers as one of the member variables, and
–making these pointers point to other objects of the same type.
•These mechanisms are illustrated in the following program:
#include <iostream>
using namespace std;
template <class T>
class Node {
public:
T value;
Node* ptr;
};
pf3
pf4

Partial preview of the text

Download Linked Lists Part I - CSCI-1200 Computer Science II and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Fall 2007

Lecture 10 — Linked Lists, Part I

Review from Lecture 9

  • Returning const references to member variables provides access to important variables without the cost of copying and without the danger of allowing them to be changed.
  • Lists vs. vectors
  • Iterators (vs. subscripting)
  • Special properties of vector iterators
  • The erase function for vectors and lists
  • Sieve of Eratosthenes

Today’s Class: Linked Lists, Part I

  • Motivation: implementation of the std::list container class.
  • Introductory example on linked lists.
  • Basic linked list operations:
    • Stepping through a list
    • Push back
    • Insert
    • Remove
  • Common mistakes

10.1 Motivation

  • Thus far our discussion of how list is implemented has been only intuitive: it is a “chain” of objects.
  • Now we will look at the mechanism — linked lists.
  • Learning this mechanism is good background for higher-level courses where the design of novel data structures is important.

10.2 Objects with Pointers / Linking Objects

  • The two fundamental mechanisms of linked lists are:
    • creating objects with pointers as one of the member variables, and
    • making these pointers point to other objects of the same type.
  • These mechanisms are illustrated in the following program:

#include using namespace std;

template class Node { public: T value; Node* ptr; };

void main() { Node* ll; // ll is a pointer to a (non-existent) Node ll = new Node; // Create a Node and assign its memory address to ll ll->value = 6; // This is the same as (*ll).value = 6; ll->ptr = NULL; // NULL == 0, which indicates a "null" pointer

Node* q = new Node; q->value = 8; q->ptr = NULL;

// set ll’s ptr member variable to // point to the same thing as variable q ll->ptr = q;

cout << "1st value: " << ll->value << "\n" << "2nd value: " << ll->ptr->value << endl; }

6

ll

value ptr

value ptr

q

NULL

8

10.3 Definition: A Linked List

  • The definition is recursive: A linked list is either:
    • Empty, or
    • Contains a node storing a value and a pointer to a linked list.
  • The first node in the linked list is called the head node and the pointer to this node is called the head pointer. The pointer’s value will be stored in a variable called head.

10.4 Visualizing Linked Lists

NULL

value

ptr

value

ptr

value

ptr

value

ptr

head

  • The head pointer variable is drawn with its own box. It is an individual variable. It is important to have a separate pointer to the first node, since the “first” node may change.
  • The objects (nodes) that have been dynamically allocated and stored in the linked lists are shown as boxes, with arrows drawn to represent pointers. - Note that this is a conceptual view only. The memory locations could be anywhere, and the actual values of the memory addresses aren’t usually meaningful.
  • The last node MUST have NULL for its pointer value — you will have all sorts of trouble if you don’t ensure this!
  • You should make a habit of drawing pictures of linked lists to figure out how to do the operations.

10.5 Basic Mechanisms: Stepping Through the List

  • We’d like to write a function to determine if a particular value, stored in x, is also in the list.
  • You can think of this as a precursor to the find function. Our function isn’t yet returning an iterator, however.
  • We can access the entire contents of the list, one step at a time, by starting just from the head pointer.
    • We will need a separate, local pointer variable to point to nodes in the list as we access them.
    • We will need a loop to step through the linked list (using the pointer variable) and a check on each value.

10.10 Basic Mechanisms: Removing a Node

  • There are two parts to this: finding the node to be removed and doing the remove operation.
  • The remove operation itself requires a pointer to the node before the node to be removed.
  • Removing the first node is an important special case.

10.11 Exercise: Remove a Node

Suppose p points to a node that should be removed from a linked list, q points to the node before p, and head points to the first node in the linked list. Write code to remove p, making sure that if p points to the first node that head points to what was the second node and now is the first after p is removed.

10.12 Exercise: List Copy

Write a recursive function to copy all nodes in a linked list to form an new linked list of nodes with identical structure and values. Here’s the function prototype:

template void CopyAll(Node* old_head, Node*& new_head) {

10.13 Basic Linked Lists Mechanisms: Common Mistakes

Here is a summary of common mistakes. Read these carefully, and read them again when you have problem that you need to solve.

  • Allocating a new node to step through the linked list; only a pointer variable is needed.
  • Confusing the. and the -> operators.
  • Not setting the pointer from the last node to NULL.
  • Not considering special cases of inserting / removing at the beginning or the end of the linked list.
  • Applying the delete operator to a node (calling the operator on a pointer to the node) before it is removed. Delete should be done after all pointer manipulations are completed.
  • Pointer manipulations that are out of order. These can ruin the structure of the linked list.

10.14 Looking Ahead to Lecture 19 — Our Own List Class

  • We will alter the structure of our linked list. Nodes will be templated and have two pointers, one going “forward” to the successor in the linked list and one going “backward” to the predecessor in the linked list. We will have a pointer to the beginning and the end of the list.

template class Node { public: Node() : next_(NULL), prev_(NULL) {} Node(const T& v) : value_(v), next_(NULL), prev_(NULL) {} T value_; Node* next_; Node* prev_; };

  • We’ll reimplement the mechanisms discussed today and we will define list iterators as a class inside a class.