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

Slides:



Advertisements
Similar presentations
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Advertisements

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.
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.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p 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.
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.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
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.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
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.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke 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.
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,
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Using a Stack Chapter 6 introduces the stack data type.
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.
Using a Stack Chapter 6 introduces the stack data type.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
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.
Complete Binary Trees Chapter 9 introduces trees.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

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

For this presentation, nodes in a linked list are objects, as shown here. data_field link_field 10 data_field link_field 15 data_field link_field 7 null class node { public: typedef double value_type;... private value_type data_field; node *link_field; }; Declarations for Linked Lists

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

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

A program can keep track of the front node by using a pointer variable such as head_ptr in this example. Notice that head_ptr is not a node -- it is a pointer to a node. head_ptr data_field link_field 10 data_field link_field 15 data_field link_field 7 null

Declarations for Linked Lists A program can keep track of the front node by using a pointer variable such as head_ptr. Notice that head_ptr is not a node -- it is a pointer to a node. We represent the empty list by storing null in the head pointer. head_ptr null

void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front We want to add a new entry, 13, to the front of the linked list shown here null head_ptr entry 13

Inserting a Node at the Front 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 Front 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 Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node; Place the data in the new node's data_field. void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node; Place the data in the new node's data_field. 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 Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); The correct new node can be completely created in one step by calling an appropriate node constructor. void list_head_insert(node*& head_ptr, const node::value_type& entry);

Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); Make the old head pointer point to the new node. void list_head_insert(node*& head_ptr, const node::value_type& entry);

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

Inserting a Node at the Front insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; null head_ptr 13 When the function returns, the linked list has a new node at the front. 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(entry, head_ptr); head_ptr = insert_ptr; } Inserting a Node at the Front

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; } Does the function work correctly for the empty list ?

head_ptr entry 13 null Inserting a Node at the Front 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(entry, 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(entry, head_ptr); head_ptr = insert_ptr; } null

Inserting a Node at the Front 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(entry, head_ptr); head_ptr = insert_ptr; }

Inserting a Node at the Front head_ptr 13 null 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; } When the function returns, the linked list has one node.

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

Pseudocode for Inserting Nodes Nodes are often inserted at places other than the front of a linked list. 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: 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 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

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

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 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 orange pointer ? 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_field (although this name may be private to the node) What is the name of this orange pointer ? 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_field points to the head of a small 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 front 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 What might cause this statement to fail to compile? previous_ptr list_head_insert(previous_ptr->link_field, entry);

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 Use a node member function to get the link field if needed. previous_ptr list_head_insert(previous_ptr->link( ), 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); 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. Make the function call: list_head_insert(previous_ptr->link( ), entry);

Pseudocode for Inserting Nodes The process of adding a new node in the middle of a list can also be incorporated as a separate function. This function is called list_insert in the linked list toolkit of Section 5.2. The process of adding a new node in the middle of a list can also be incorporated as a separate function. This function is called list_insert in the linked list toolkit of Section 5.2.

Pseudocode for Removing Nodes Nodes often need to be removed from a linked list. 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. Well look at the pseudocode for removing a node from the front 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 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 remove_ptr. head_ptr = remove_ptr->link( ); remove_ptr

Removing the Head Node 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 Heres what the linked list looks like after the removal finishes null head_ptr

It is easy to insert a node at the front of a list. The linked list toolkit also provides a function for inserting a new node elsewhere It is easy to remove a node at the front of a list. 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

T HE E ND Presentation copyright 2010, 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.