Download presentation
Presentation is loading. Please wait.
Published byMoris Mills Modified over 9 years ago
1
Linked Lists Objects->Connected->by->Pointers
2
What is a Linked List? List: a collection Linked: any individual item points to another item to connect them Linked List: ◦ A collection of objects (data + functions) ◦ Data includes items of the same data type(s) ◦ A way of implementing a container class
3
List Operations Create itself (initialize) Insert an item Delete an item Print itself Know the number of items it contains
4
Linked List Container Class Implementation 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)
5
Anatomy of a Node Pointer to the head of the list Tux Penquin data Pointer to next node Node Null pointer At the end of the list
6
How to Code a Node class node { public: typedef double value_type; … private: value_type penquin; node *next; };
7
What Else Does a Node Need? Data is private Accessor Method Mutator Method
8
From a Node to a List Create 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!
9
From a Node to a List Non-Member functions in a Linked List Toolkit Begin with a “head” pointer to a node This pointer must be declared in main main { node *head; }
10
From a Node to a List Member 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; };
11
Class Functions versus Non- Member Functions main { node *head; } class myList { … private: node *head; };
12
From a Node to a List: How To Initialize a Node to Add to a List So 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?
13
From a Node to a List: Where To Add a Node The first node should be at the head of the list Where 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?
14
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;
15
From a Node to a List: Member Add Function versus Non-Member Add Function Node * myList::AddNode(value_type myData) { Node *newOne = new Node; … } void AddNode(node*& head, value_type myData) { Node *newOne = new Node; … }
16
From a Node to a 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 newOne->set_data(“Joe”);
17
From a Node to a List: Member Add Function versus Non-Member Add Function Node * 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
18
From a Node to a List: What about Joe’s Next pointer? Joe newOne head_ptr
19
From a Node to a List: Adding to the Head of a List that Isn’t Empty Null pointer At the end of the list head_ptr newOne
20
From a Node to a List: Adding to the Head of a List that Isn’t Empty Null pointer At the end of the list head_ptr newOne
21
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
22
From a Node to a 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) 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);
23
From a Node to a List: Member Add Function versus Non-Member Add Function Node * 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
24
From a Node to a List: Adding to the Head of a List that Isn’t Empty Null pointer At the end of the list head_ptr newOne
25
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?
26
Removing a Node From a List Nodes often need to be removed from a linked list From front of the list From another location in the list ◦ Two operations are needed Search for node to remove Remove node
27
Removing the node at the head of a list Null pointer At the end of the list head_ptr Is this OK? delete head_ptr;
28
Removing the node at the head of a list Null pointer At the end of the list head_ptr doomed What if I create a new pointer and point it to the head of the list …
29
Removing the node at the head of a list Null pointer At the end of the list head_ptr doomed …and then point the head pointer to the next node…
30
Removing the node at the head of a list Null pointer At the end of the list head_ptr Can I delete doomed now? doomed
31
Member Function Code Node *doomed = list; list = list->get_next(); delete doomed;
32
Does This Work For Removing ANY Node? This method works because we know where the head node is What 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?
33
Does This Work For Removing ANY Node? Null pointer At the end of the list head_ptr doomed Does this work? And what about the penguin in the green hat? ???
34
Does This Work For Removing ANY Node? Null pointer At 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?
35
Does This Work For Removing ANY Node? Null pointer At the end of the list head_ptr previous …but what if I need to delete the last node?
36
Does This Work For Removing ANY Node? Redirect Which Pointer?Redirect Where? Deleting 1 st NodeHead pointer to point toHead pointer’s->next Deleting Middle NodePrevious pointer to point toPrevious pointer’s -> next Deleting Last NodePrevious pointer to point toNULL
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.