Linked ListsObjects->Connected->by->Pointers
What is a Linked List?List: a collectionLinked: any individual item points
to another item to connect themLinked List:
◦A collection of objects (data + functions)
◦Data includes items of the same data type(s)
◦A way of implementing a container class
List OperationsCreate itself (initialize)Insert an itemDelete an itemPrint itselfKnow the number of items it
contains
Linked List Container ClassImplementation of the container
class is based on the concept of a node
Node: holds two important components◦The item that the user wants (private
data and public functions)◦A pointer to the next node in the list
(next)
Anatomy of a Node
Pointer to the head of the list
Tux Penquin data
Pointer to next node
Node
Null pointerAt the end of the list
How to Code a Node
class node{ public:
typedef double value_type;…
private:value_type penquin;node *next;
};
What Else Does a Node Need?Data is private Accessor MethodMutator Method
From a Node to a ListCreate a pointer to a node object
◦Remember the pointer to a dynamic array? Create an add function to add a new
node and link it to a list (and other useful functions)◦Textbook creates these functions as non-
member functions in the linked list toolkit◦You can also create another class to hold all
of these useful functions Bonus: node is more easily accessible if it is a
member of the same class as the functions that manipulate it!
From a Node to a ListNon-Member functions in a Linked List Toolkit
Begin with a “head” pointer to a node
This pointer must be declared in mainmain {
node *head;}
From a Node to a ListMember Functions in a Linked List Class
Begin with a pointer to a node This pointer must be declared in the
private section of your linked list class:
class myList { … private: node *head;};
Class Functions versus Non-Member Functions
main
{
node *head;
}
class myList { … private: node *head;
};
From a Node to a List: How To Initialize a Node to Add to a ListSo far, we have a pointer for a node object,
but where does it point?Remember how to make a dynamic array grow?Create a function to add nodes to the list
◦1. Declare a node object◦2. Use the new operator to initialize it in the
heap◦3. Initialize the data to a valid state (put some
data in it)◦4. Get the original “head” pointer to point to it
Which end of the list do we add a new node?
From a Node to a List: Where To Add a NodeThe first node should be at the
head of the listWhere should the second node
go?◦Head of the list◦Tail of the list
How do we know where the head of the list is? Do we know where the tail is?
From a Node to a List: The Add Function◦ 1. Declare a node
object◦ 2. Use the new
operator to initialize it in the heap
Node *newOne = new Node;
From a Node to a List: Member Add Function versus Non-Member Add FunctionNode * myList::AddNode(value_type myData)
{
Node *newOne = new Node;
…
}
void AddNode(node*& head, value_type myData)
{
Node *newOne = new Node;
…
}
From a Node to a List: 1. Declare a node
object2. Use the new
operator to initialize it in the heap
3. Initialize the data to a valid state (put some data in it)
4. Get the original “head” pointer to point to it
newOne->set_data(“Joe”);
From a Node to a List: Member Add Function versus Non-Member Add FunctionNode * myList::AddNode(value_type myData)
{
Node newOne = new Node;
newOne->set_data(“Joe”);
return newOne;
}
void AddNode(node*& head, value_type myData)
{
Node newOne = new Node;
newOne->set_data(“Joe”);
head = newOne;
}
3. Get the original “head” pointer to point to it
From a Node to a List: What about Joe’s Next pointer?
Joe
newOne
head_ptr
From a Node to a List: Adding to the Head of a List that Isn’t Empty
Null pointerAt the end of the list
head_ptr
newOne
From a Node to a List: Adding to the Head of a List that Isn’t Empty
Null pointerAt the end of the list
head_ptr
newOne
From a Node to a List: One MORE Thing…
◦1. Declare a node object◦2. Use the new operator to initialize
it in the heap◦3. Initialize the data to a valid state
(put some data in it)◦3b. Set the node’s next pointer to
point to the head of the list◦4. Get the original “head” pointer to
point to it
From a Node to a List: 1. Declare a node
object2. Use the new
operator to initialize it in the heap
3. Initialize the data to a valid state (put some data in it)
3b. Set the node’s next pointer to point to the head of the list
4. Get the original “head” pointer to point to it
newOne->set_next(head);
From a Node to a List: Member Add Function versus Non-Member Add FunctionNode * myList::AddNode(value_type myData)
{
Node newOne = new Node;
newOne->set_data(“Joe”);
newOne->set_next(head);
return newOne;
}
void AddNode(node*& head, value_type myData)
{
Node newOne = new Node;
newOne->set_data(“Joe”);
newOne->set_next(head);
head = newOne;
}
3a. Set the node’s next pointer to point to the head of the list
From a Node to a List: Adding to the Head of a List that Isn’t Empty
Null pointerAt the end of the list
head_ptr
newOne
From a Node to a List: Does it Work for an Empty List?What will the new node’s next
pointer point to if the list is empty?
newOne->set_next(head);
Joe
newOne
head_ptr???
What if you initialize head_ptr as NULL?
Removing a Node From a ListNodes often need to be removed
from a linked listFrom front of the listFrom another location in the list
◦Two operations are needed Search for node to remove Remove node
Removing the node at the head of a list
Null pointerAt the end of the list
head_ptr
Is this OK?delete head_ptr;
Removing the node at the head of a list
Null pointerAt the end of the list
head_ptr
doomedWhat if I create a new pointer and point it to the head of the list …
Removing the node at the head of a list
Null pointerAt the end of the list
head_ptr
doomed
…and then point the head pointer to the next node…
Removing the node at the head of a list
Null pointerAt the end of the list
head_ptr
Can I delete doomed now?
doomed
Member Function Code
Node *doomed = list;list = list->get_next();delete doomed;
Does This Work For Removing ANY Node?This method works because we
know where the head node isWhat of the node is in the middle
or the end? Do we know where it is?◦Our find function can locate the node◦But what about all of the nodes in
front of that node?
Does This Work For Removing ANY Node?
Null pointerAt the end of the list
head_ptr
doomed
Does this work? And what about the penguin in the green hat?
???
Does This Work For Removing ANY Node?
Null pointerAt the end of the list
head_ptr
previous
What if I remember where the previous node is…and then I redirect that nodes’ next pointer instead?
Does This Work For Removing ANY Node?
Null pointerAt the end of the list
head_ptr
previous …but what if I
need to delete the last node?
Does This Work For Removing ANY Node?
Redirect Which Pointer?
Redirect Where?
Deleting 1st Node Head pointer to point to
Head pointer’s->next
Deleting Middle Node
Previous pointer to point to
Previous pointer’s -> next
Deleting Last Node Previous pointer to point to
NULL