360 likes | 456 Vues
Linked Lists. Objects->Connected->by->Pointers. 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)
E N D
Linked Lists Objects->Connected->by->Pointers
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
List Operations • Create itself (initialize) • Insert an item • Delete an item • Print itself • Know the number of items it contains
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)
Anatomy of a Node Null pointer At the end of the list Pointer to the head of the list Tux Penquin data Pointer to next node Node
How to Code a Node class node { public: typedef double value_type; … private: value_typepenquin; node *next; };
What Else Does a Node Need? • Data is private • Accessor Method • Mutator Method
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!
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 main main { 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 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?
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?
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 Function Node * myList::AddNode(value_typemyData) { Node *newOne= new Node; … } void AddNode(node*& head, value_typemyData) { Node *newOne= new Node; … }
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”);
From a Node to a List: Member Add Function versus Non-Member Add Function Node * myList::AddNode(value_typemyData) { Node newOne = new Node; newOne->set_data(“Joe”); return newOne; } void AddNode(node*& head, value_typemyData) { 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? newOne Joe head_ptr
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
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
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 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);
From a Node to a List: Member Add Function versus Non-Member Add Function Node * myList::AddNode(value_typemyData) { Node newOne = new Node; newOne->set_data(“Joe”); newOne->set_next(head); return newOne; } void AddNode(node*& head, value_typemyData) { 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 pointer At 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); newOne Joe What if you initialize head_ptr as NULL? head_ptr ???
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
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;
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 …
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…
Removing the node at the head of a list Null pointer At the end of the list head_ptr doomed Can I delete doomed now?
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 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?
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?
Does This Work For Removing ANY Node? Null pointer At the end of the list head_ptr What if I remember where the previous node is…and then I redirect that nodes’ next pointer instead? previous
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?