Linked Lists Part I - Understanding the Basics, Lab Reports of Data Structures and Algorithms

An introduction to linked lists, a fundamental data structure used in computer science. The motivation behind linked lists, the mechanisms of creating objects with pointers and linking them, and basic linked list operations such as stepping through a list, pushing back, inserting, and removing nodes. The document also includes common mistakes to avoid when working with linked lists.

Typology: Lab Reports

Pre 2010

Uploaded on 08/09/2009

koofers-user-r2s
koofers-user-r2s 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI-1200 Computer Science II Spring 2006
Lecture 20 Linked Lists Part I
Review from Lecture 19 and Lab
Building our own version of vector,stack &queue
Templates
Classes that allocate dynamic memory really must provide the BIG 3:
Copy constructor
Assignment operator
Destructor
Dynamic resizing
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
20.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.
20.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 - Understanding the Basics and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Spring 2006

Lecture 20 Linked Lists — Part I

Review from Lecture 19 and Lab

  • Building our own version of vector, stack & queue
  • Templates
  • Classes that allocate dynamic memory really must provide the BIG 3:
    • Copy constructor
    • Assignment operator
    • Destructor
  • Dynamic resizing

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

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

20.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;

ll->ptr = q; // ll’s ptr member variable now has the same value // as the pointer variable q

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

  • The following picture illustrates the structure of memory at the end of the program.

8

ll

value ptr

value ptr

q 6

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

20.4 Visualizing Linked Lists

value ptr

value ptr

value ptr

value ptr

head

  • The head pointer variable is drawn with its own box. It is an individual variable.
  • 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 a 0 (0 == 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.

Node * q = new Node; // create a new node q -> value = x; // store x in this node q -> next = p -> next; // make its successor be the current successor of p p -> next = q; // make p’s successor be this new node

  • Can you draw a picture to illustrate what is happening here?
  • This code will not work if you want to insert the value stored in x in a new node at the front of the linked list.

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

20.11 Exercise

Suppose p points to 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.

20.12 Basic Mechanisms: Common Mistakes

Here is 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 0 (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.

20.13 Looking Ahead to Lecture 21 — Our Own List Class

  • We will alter the structure ouf 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_(0), prev_(0) {} Node(const T& v) : value_(v), next_(0), prev_(0) {} 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.