Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any...

36
Linked Lists Objects->Connected->by->Pointers

Transcript of Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any...

Page 1: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

Linked ListsObjects->Connected->by->Pointers

Page 2: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Page 3: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

List OperationsCreate itself (initialize)Insert an itemDelete an itemPrint itselfKnow the number of items it

contains

Page 4: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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)

Page 5: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Page 6: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

How to Code a Node

class node{ public:

typedef double value_type;…

private:value_type penquin;node *next;

};

Page 7: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

What Else Does a Node Need?Data is private Accessor MethodMutator Method

Page 8: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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!

Page 9: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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;}

Page 10: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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;};

Page 11: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

Class Functions versus Non-Member Functions

main

{

node *head;

}

class myList { … private: node *head;

};

Page 12: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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?

Page 13: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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?

Page 14: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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;

Page 15: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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;

}

Page 16: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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”);

Page 17: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Page 18: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

From a Node to a List: What about Joe’s Next pointer?

Joe

newOne

head_ptr

Page 19: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Page 20: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Page 21: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Page 22: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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);

Page 23: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Page 24: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Page 25: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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?

Page 26: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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

Page 27: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

Removing the node at the head of a list

Null pointerAt the end of the list

head_ptr

Is this OK?delete head_ptr;

Page 28: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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 …

Page 29: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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…

Page 30: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

Removing the node at the head of a list

Null pointerAt the end of the list

head_ptr

Can I delete doomed now?

doomed

Page 31: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

Member Function Code

Node *doomed = list;list = list->get_next();delete doomed;

Page 32: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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?

Page 33: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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?

???

Page 34: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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?

Page 35: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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?

Page 36: Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.

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