CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
1 CSC211 Data Structures Lectures 4 & 5 Container Classes Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
1 CSC212 Data Structure - Section FG Lectures 4 & 5 Container Classes Instructor: Zhigang Zhu Department of Computer Science City College of New York.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
1 Linked Lists It’s a conspiracy!. 2 Linked list: a data structure used to represent an ordered list Consists of a sequence of nodes A node consists of.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC212 Data Structure - Section FG Lecture 11 Templates, Iterators and STL Instructor: Professor Zhigang Zhu Department of Computer Science City College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
1 CSC212 Data Structure Lecture 4 Container Classes Instructor: George Wolberg Department of Computer Science City College of New York.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
Prof. I. J. Chung Data Structure #4 Professor I. J. Chung.
CSC212 Data Structure - Section BC
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Collection Classes A Collection class is a data type that is capable of holding a group of items. In Java, Collection classes can be implemented as a class,
The Bag and Sequence Classes with Linked Lists
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Using a Queue Chapter 8 introduces the queue data type.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Presentation transcript:

CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York

Motivation p In a sequence using an array, inserting a new item needs to move others back ?? 18

Motivation p In a sequence using an array, inserting a new item needs to move others back ? 18

Motivation p In a sequence using an array, inserting a new item needs to move others back... p So the Big-O of the insert is O(n) ?

Motivation p How can we insert a new item without moving others ? ?? 18 We need a new data structure

Motivation p How can we insert a new item without moving others ? 18 ?? break an array into a linked chain...

Motivation p How can we insert a new item without moving others ? 18 ?? and then put the new item into the chain

Motivation p How can we insert a new item without moving others ? 18 ?? But the links (->) need some way to build up

p p Chapter 5 introduces the often- used data structure of linked lists. p p This presentation shows how to implement the most common operations on linked lists. Linked Lists in Action CHAPTER 5 Data Structures and Other Objects

  Each node in the linked list is a class, as shown here. data link 10 data link 15 data link 7 null class node { public: typedef int value_type;... private: value_type data; node *link; }; Declarations for Linked Lists

data link 7 p p The data portion of each node is a type called value_type, defined by a typedef. link null data link 15 Declarations for Linked Lists data 10 class node { public: typedef int value_type;... private: value_type data; node *link; };

p p Each node also contains a link field which is a pointer to another node. data 15 data 7 Declarations for Linked Lists data 10 link null link class node { public: typedef int value_type;... private: value_type data; node *link; };

Declarations for Linked Lists p p A program can keep track of the first node by using a pointer variable such as head_ptr in this example. p p Notice that head_ptr itself is not a node -- it is a pointer to a node. data link 10 data link 15 data link 7 null head_ptr node * head_ptr;

Declarations for Linked Lists p p A program can also keep track of the last node by using a pointer variable such as tail_ptr in this example. p p Notice that tail_ptr itself is not a node -- it is a pointer to a node. data link 10 data link 15 data link 7 null head_ptr node * head_ptr; node * tail_ptr; tail_ptr

Declarations for Linked Lists p p A program can keep track of the first and the last nodes by using pointer variables such as head_ptr, tail_ptr. p p Notice that neither head_ptr nor tail_ptr is a node -- it is a pointer to a node.   For an empty list, null is stored in both the head and the tail pointers. head_ptr null tail_ptr null node * head_ptr; node * tail_ptr; head_ptr = NULL; tail_ptr = NULL; // NULL can be used for any pointers!

The Complete node Class Definition p The node class is fundamental to linked lists p The private member variables p data: a value_type variable p link: a pointer to the next node p The member functions include: p A constructor p Set data and set link p Retrieve data and retrieve link

The Complete node Class Definition p The node class is fundamental to linked lists p The private member variables p data_field p link_field p The member functions include: p A constructor p Set data and set link p Retrieve data and retrieve link class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node( const value_type& init_data = value_type( ), node* init_link = NULL ) { data = init_data; link = init_link; } // Member functions to set the data and link fields: void set_data(const value_type& new_data) { data = new_data; } void set_link(node* new_link) { link = new_link; } // Constant member function to retrieve the current data: value_type data( ) const { return data; } // Two slightly different member functions to retrieve // the current link: const node* link( ) const { return link; } node* link( ) { return link;} private: value_type data; node* link; }; default argument given by the value_type default constructor Why TWO? p

A Small Quiz - p p Suppose a a program has built the linked list as shown, and head_ptr is a pointer to a node. p p What is the data type of *head_ptr? p p cout << (*head_ptr). data(); p p cout data(); data link 10 data link 15 data link 7 null head_ptr

Linked List Toolkit p Design Container Classes using Linked Lists p The use of a linked list is similar to our previous use of an array in a container class p But storing and retrieving needs more work since we do not have that handy indexing p => Linked List Toolbox p using node class

The Workings of four functions p This lecture will show four functions: p Compute the length of a linked list (code) p Insert a new node at the head (code) p Insert a node at any location (pseudo-code) p Delete a node from the head (pseudo-code) p Read Section 5.2 for other functions in the Toolbox p will be used in container classes bag and sequence

size_t list_length(const node* head_ptr); Length of a Linked List We simply want to compute the length of the linked list, for example the one shown here. Note that list_length is not a member function of the node class null head_ptr

size_t list_length(const node* head_ptr); Pseudo-code of list_length 1. Initialize the count to zero. 2. Make cursor point to each node, starting at the head. Each time cursor points to a new node, add 1 to count. 3. return count null head_ptr count 0

size_t list_length(const node* head_ptr); Pseudo-code of list_length 1. Initialize the count to zero. 2. Make cursor point to each node, starting at the head. Each time cursor points to a new node, add 1 to count. 3. return count null head_ptr count 1 cursor

size_t list_length(const node* head_ptr); Pseudo-code of list_length 1. Initialize the count to zero. 2. Make cursor point to each node, starting at the head. Each time cursor points to a new node, add 1 to count. 3. return count null head_ptr count 2 cursor

size_t list_length(const node* head_ptr); Pseudo-code of list_length 1. Initialize the count to zero. 2. Make cursor point to each node, starting at the head. Each time cursor points to a new node, add 1 to count. 3. return count null head_ptr count 3 cursor

size_t list_length(const node* head_ptr); Pseudo-code of list_length 1. Initialize the count to zero. 2. Make cursor point to each node, starting at the head. Each time cursor points to a new node, add 1 to count. 3. return count null head_ptr count 3 cursor

size_t list_length(const node* head_ptr) { const node *cursor; size_t count = 0; // step 1 for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) count++; // step 2 return count; // step 3 } Real code of list_length: List Traverse 1. Initialize the count to zero. 2. Each time cursor points to a new node, add 1 to count. 3. return count null head_ptr count 0 cursor

size_t list_length(const node* head_ptr) { const node *cursor; size_t count = 0; // step 1 for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) count++; // step 2 return count; // step 3 } Real code of list_length: List Traverse 1. Initialize the count to zero. 2. Each time cursor points to a new node, add 1 to count. 3. return count null head_ptr count 1 cursor

size_t list_length(const node* head_ptr) { const node *cursor; size_t count = 0; // step 1 for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) count++; // step 2 return count; // step 3 } Real code of list_length: List Traverse 1. Initialize the count to zero. 2. Each time cursor points to a new node, add 1 to count. 3. return count null head_ptr count 2 cursor

size_t list_length(const node* head_ptr) { const node *cursor; size_t count = 0; // step 1 for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) count++; // step 2 return count; // step 3 } Real code of list_length: List Traverse 1. Initialize the count to zero. 2. Each time cursor points to a new node, add 1 to count. 3. return count null head_ptr count 3 cursor

size_t list_length(const node* head_ptr) { const node *cursor; size_t count = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) count++; return count; // step 3 } Big-O of list_length Big-O: O (n) if length is n null head_ptr count 3 cursor

size_t list_length(const node* head_ptr) { const node *cursor; size_t count = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) count++; return count; } Is list_length works for an empty list? cursor = head_ptr = NULL count = 0 count 0 head_ptr null cursor null

The Workings of four functions p This lecture will show four functions: p Compute the length of a linked list (code) p Insert a new node at the head (code) p Insert a node at any location (pseudo-code) p Delete a node from the head (pseudo-code) p Read Section 5.2 for other functions in the Toolbox p will be used in container classes bag and sequence

void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a node at the Head We want to add a new entry, 13, to the head of the linked list shown here. Note that head_ptr is a reference node pointer null head_ptr entry 13

Inserting a Node at the Head ¶ ¶Create a new node, pointed to by a local variable insert_ptr null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head ¶ ¶insert_ptr = new node; null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head   insert_ptr = new node; · ·Place the data in the new node's data field null head_ptr entry 13 insert_ptr 13 void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head   insert_ptr = new node; · ·insert_ptr->data = entry; null head_ptr entry 13 insert_ptr 13 ? What expression appears on the left side of the assignment statement ? void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head   insert_ptr = new node; · ·insert_ptr->data = entry; null head_ptr entry 13 insert_ptr 13 But data is a private variable, so cannot be accessed by a non-member function void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head   insert_ptr = new node; · ·insert_ptr->data = entry; null head_ptr entry 13 insert_ptr 13 But data is a private variable, so cannot be accessed by a non-member function void list_head_insert(node*& head_ptr, const node::value_type& entry); X

Inserting a Node at the Head   insert_ptr = new node; · ·insert_ptr->set_data(entry); null head_ptr entry 13 insert_ptr 13 Instead, Set_data function is used since data_field is a private variable of the node class void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head null head_ptr entry 13 insert_ptr 13  insert_ptr = new node; ·insert_ptr->set_data(entry); ¸Connect the new node to the front of the list. void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head null head_ptr entry 13 insert_ptr 13 What will be the parameter ?  insert_ptr = new node; ·insert_ptr->set_data(entry); Ìinsert_ptr->set_link(head_ptr); ? void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head null head_ptr entry 13 insert_ptr 13 The new node is linked to the node that head_ptr is pointing to.  insert_ptr = new node; ·insert_ptr->set_data(entry); Ìinsert_ptr->set_link(head_ptr); void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head null head_ptr entry 13 insert_ptr 13  insert_ptr = new node; ·insert_ptr->set_data(entry); Ìinsert_ptr->set_link(head_ptr); ¹Make the head_ptr point to the new head of the linked list. void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head null head_ptr entry 13 insert_ptr 13  insert_ptr = new node; ·insert_ptr->set_data(entry); Ìinsert_ptr->set_link(head_ptr); Íhead_ptr = insert_ptr; void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Head   insert_ptr = new node; · ·insert_ptr->set_data(entry); Ì Ìinsert_ptr->set_link(head_ptr); Íhead_ptr = insert_ptr; null head_ptr 13 When the function returns, the linked list has a new node at the head, containing 13. void list_head_insert(node*& head_ptr, const node::value_type& entry);

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node; insert_ptr->set_data(entry); insert_ptr->set_link(head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Head Linked List: O(1) - cmp: Array: O(n) What is the Big-O of the head_insert function? What is the Big-O of the head_insert function?

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node; insert_ptr->set_data(entry); insert_ptr->set_link(head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Head Does the function work correctly for the empty list ?

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node; insert_ptr->set_data(entry); insert_ptr->set_link(head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Head head_ptr entry 13 null Does the function work correctly for the empty list ?

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node; insert_ptr->set_data(entry); insert_ptr->set_link(head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Front head_ptr entry 13 null insert_ptr 13

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node; insert_ptr->set_data(entry); insert_ptr->set_link(head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Head head_ptr entry 13 null insert_ptr 13 null

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node; insert_ptr->set_data(entry); insert_ptr->set_link(head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Head head_ptr entry 13 insert_ptr 13 null

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node; insert_ptr->set_data(entry); insert_ptr->set_link(head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Head head_ptr 13 null When the function returns, the linked list has one node, containing 13.

Caution! p p Always make sure that your linked list functions work correctly with an empty list. EMPTY LIST

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node; insert_ptr->set_data(entry); insert_ptr->set_link(head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Head Q: Can you give an implementation with ONLY a single statement?

void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Head YES, we can use the constructor with parameters!

void list_head_insert(node*& head_ptr, const node::value_type& entry) { head_ptr = new node(entry, head_ptr); } Inserting a Node at the Head and assign the return pointer of new directly to the head pointer !

p Break

p p Each node also contains a link field which is a pointer to another node. data 15 data 7 Declarations for Linked Lists data 10 link null link class node { public: typedef int value_type;... private: value_type data; node *link; };

The Complete node Class Definition p The node class is fundamental to linked lists p The private member variables p data_field p link_field p The member functions include: p A constructor p Set data and set link p Retrieve data and retrieve link class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node( const value_type& init_data = value_type( ), node* init_link = NULL ) { data = init_data; link = init_link; } // Member functions to set the data and link fields: void set_data(const value_type& new_data) { data = new_data; } void set_link(node* new_link) { link = new_link; } // Constant member function to retrieve the current data: value_type data( ) const { return data; } // Two slightly different member functions to retrieve // the current link: const node* link( ) const { return link; } node* link( ) { return link;} private: value_type data; node* link; }; default argument given by the value_type default constructor Why TWO? p

size_t list_length(const node* head_ptr) { const node *cursor; size_t count = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) count++; return count; // step 3 } Big-O of list_length Big-O: O (n) if length is n null head_ptr count 3 cursor

The Workings of four functions p This lecture will show four functions: p Compute the length of a linked list (code) p Insert a new node at the head (code) p Insert a node at any location (pseudo-code) p Delete a node from the head (pseudo-code) p Read Section 5.2 for other functions in the Toolbox p will be used in container classes bag and sequence

Pseudocode for Inserting Nodes p p Nodes are often inserted at places other than the front of a linked list. p p There is a general pseudocode that you can follow for any insertion function...

Pseudocode for Inserting Nodes ¶ ¶Determine whether the new node will be the first node in the linked list. If so, then there is only one step: list_head_insert(head_ptr, entry);

Pseudocode for Inserting Nodes ÊDetermine whether the new node will be the first node in the linked list. If so, then there is only one step: The function we already wrote The function we already wrote list_head_insert(head_ptr, entry);

Pseudocode for Inserting Nodes ¶ ¶Determine whether the new node will be the first node in the linked list. If so, then there is only one step: list_head_insert(head_ptr, entry); A pointer to the head of the list A pointer to the head of the list

Pseudocode for Inserting Nodes ¶ ¶Determine whether the new node will be the first node in the linked list. If so, then there is only one step: list_head_insert(head_ptr, entry); The data to put in the new node The data to put in the new node

Pseudocode for Inserting Nodes ·Otherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position.

Pseudocode for Inserting Nodes null head_ptr ·Otherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. In this example, the new node will be the second node In this example, the new node will be the second node previous_ptr

Pseudocode for Inserting Nodes null head_ptr ·Otherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position What is the name of this pointer ? Look at the pointer which is in the node *previous_ptr Look at the pointer which is in the node *previous_ptr previous_ptr

Pseudocode for Inserting Nodes null head_ptr ·Otherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position This pointer is called previous_ptr->link This pointer is called previous_ptr->link Always remember how can you access link previous_ptr

Pseudocode for Inserting Nodes null head_ptr ËOtherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position previous_ptr->link points to the head of a smaller linked list, with 10 and 7 previous_ptr->link points to the head of a smaller linked list, with 10 and 7 previous_ptr

Pseudocode for Inserting Nodes null head_ptr ·Otherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. The new node must be inserted at the head of this small linked list. The new node must be inserted at the head of this small linked list. 13 Write one C++ statement which will do the insertion. previous_ptr

Pseudocode for Inserting Nodes null head_ptr ·Otherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. 13 Write one C++ statement which will do the insertion. previous_ptr list_head_insert(previous_ptr->link, entry); private variable?!! X

Pseudocode for Inserting Nodes null head_ptr ·Otherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. 13 More precisely, you need to use member function link(), and have three lines of code previous_ptr node *sl_head_ptr; sl_head_ptr = previous_ptr->link(); list_head_insert(sl_head_ptr, entry); previous_ptr->set_link(sl_head_ptr); sl_head_ptr

Pseudocode for Inserting Nodes null head_ptr ·Otherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. 13 More precisely, you need to use member function link(), and have three lines of code previous_ptr node *sl_head_ptr; sl_head_ptr = previous_ptr->link(); list_head_insert(sl_head_ptr, entry); previous_ptr->set_link(sl_head_ptr); sl_head_ptr

Pseudocode for Inserting Nodes null head_ptr ·Otherwise (if the new node will not be first):  Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. 13 More precisely, you need to use member function link(), and have three lines of code previous_ptr node *sl_head_ptr; sl_head_ptr = previous_ptr->link(); list_head_insert(sl_head_ptr, entry); previous_ptr->set_link(sl_head_ptr); sl_head_ptr

Pseudocode for Inserting Nodes ¶ ¶Determine whether the new node will be the first node in the linked list. If so, then there is only one step: list_head_insert(head_ptr, entry); · Otherwise (if the new node will not be first):  Set a pointer named previous_ptr to point to the node which is just before the new node's position. pDo the following : node *sl_head_ptr; sl_head_ptr = previous_ptr->link(); list_head_insert(sl_head_ptr, entry); previous_ptr->set_link(sl_head_ptr);

Pseudocode for Inserting Nodes  The process of adding a new node in the middle of a list ( only the step after previous_ptr has been set ) can also be incorporated as a separate function. This function is called list_insert in the linked list toolkit of Section 5.2. p Challenge yourself: p The textbook actually gives you a different implementation (p 235, 4 lines of code)  Can you implement list_insert with just one line of code? p Don’t use list_head_insert p See Self-Test Ex 16

The Workings of four functions p This lecture will show four functions: p Compute the length of a linked list (code) p Insert a new node at the head (code) p Insert a node at any location (pseudo-code) p Delete a node from the head (pseudo-code) p Read Section 5.2 for other functions in the Toolbox p will be used in container classes bag and sequence

Pseudocode for Removing Nodes p p Nodes often need to be removed from a linked list. p p As with insertion, there is a technique for removing a node from the front of a list, and a technique for removing a node from elsewhere. p p We’ll look at the pseudocode for removing a node from the head of a linked list.

Removing the Head Node null head_ptr 13  Start by setting up a temporary pointer named remove_ptr to the head node. remove_ptr

Removing the Head Node null head_ptr 13  Set up.  Set up remove_ptr. ·head_ptr = remove_ptr->link(); remove_ptr Draw the change that this statement will make to the linked list.

Removing the Head Node null head_ptr 13  Set up.  Set up remove_ptr. Ëhead_ptr = remove_ptr->link(); remove_ptr

Removing the Head Node  Set up.  Set up remove_ptr. Ëhead_ptr = remove_ptr->link; ¸delete remove_ptr; // Return the node's memory to heap null head_ptr 13 remove_ptr

Removing the Head Node Here’s what the linked list looks like after the removal finishes null head_ptr

p p It is easy to insert a node at the front of a list. p p The linked list toolkit also provides a function for inserting a new node elsewhere p p It is easy to remove a node at the front of a list. p p The linked list toolkit also provides a function for removing a node elsewhere--you should read about this function and the other functions of the toolkit. Summary

Key points you need to know p Linked List Toolkit uses the node class which has p set and retrieve functions p The functions in the Toolkit are not member functions of the node class p length, insert(2), remove(2), search, locate, copy,... p compare their Big-Os with similar functions for an array p They can be used in various container classes, such as bag, sequence, etc. Toolkit Code

Homework... p Self-Test Exercises (node) p 1-12 p Read after class p Linked List ToolKit (Section 5.2) p Do Self-Test Ex p Read before the next lecture p Section

T HE E ND Presentation copyright 1997, Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.