Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 17 Linked Lists.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Linked Lists
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 14 Pointers and Linked Lists Slides by David B. Teague, Western Carolina University,
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Pointers and Dynamic Arrays
COMP 53 – Week Eight Linked Lists.
Pointers and Linked Lists
Pointers and Linked Lists
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Exercise 1 From Lec80b-Ponter.ppt.
Chapter 4 Linked Lists.
Pointers and Linked Lists
Chapter 16-2 Linked Structures
Pointers and Linked Lists
Linked Lists Chapter 4.
Chapter 16 Linked Structures
Pointers & Dynamic Data Structures
Presentation transcript:

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists

Slide Overview 13.1 Nodes and Linked Lists (up to, but not including the last section on page 749.) 13.2 Stacks and Queues

Copyright © 2008 Pearson Addison-Wesley. All rights reserved Nodes and Linked Lists

Slide end head Nodes and Linked Lists A linked list is a list that can grow and shrink while the program is running. A linked list is a list that can grow and shrink while the program is running. A linked list is constructed using pointers A linked list is constructed using pointers A linked list often consists of structs or classes that contain a pointer variable connecting them to other dynamic variables A linked list often consists of structs or classes that contain a pointer variable connecting them to other dynamic variables A linked list can be visualized as items, drawn as boxes, connected to other items by arrows A linked list can be visualized as items, drawn as boxes, connected to other items by arrows

The boxes in the previous drawing represent the nodes of a linked list Nodes contain the data item(s) and a pointer that can point to another node of the same type Nodes contain the data item(s) and a pointer that can point to another node of the same type The pointers point to the entire node, not an individual item that might be in the node The arrows in the drawing represent pointers Slide Nodes

Slide Nodes are implemented in C++ as structs or classes Example: A structure to store two data items and a pointer to another node of the same type, along with a type definition might be: struct ListNode { string item; int count; ListNode *link; }; typedef ListNode* ListNodePtr; Example: A structure to store two data items and a pointer to another node of the same type, along with a type definition might be: struct ListNode { string item; int count; ListNode *link; }; typedef ListNode* ListNodePtr; This circular definition is allowed in C++ Implementing Nodes

Slide The Head of a List The box labeled head in the previous diagram is not a node, but a pointer variable that points to a node Pointer variable head is declared as: ListNodePtr head;

Slide Accessing Items in a Node Using the previous diagr, this is one way to change the number in the first node from 10 to 12: (*head).count = 12; head is a pointer variable so *head is the node to which head points head is a pointer variable so *head is the node to which head points The parentheses are necessary because the dot operator. has higher precedence than the dereference operator * The parentheses are necessary because the dot operator. has higher precedence than the dereference operator *

The arrow operator -> combines the actions of the dereferencing operator * and the dot operator to specify a member of a struct or object pointed to by a pointer (*head).count = 12; (*head).count = 12; can be written as can be written as head->count = 12; head->count = 12; The arrow operator is more commonly used The arrow operator is more commonly used Slide The Arrow Operator

Slide NULL The defined constant NULL is used as… An end marker for a linked list An end marker for a linked list A program can step through a list of nodes by following the pointers, but when it finds a node containing NULL, it knows it has come to the end of the list The value of a pointer that has nothing to point to The value of a pointer that has nothing to point to The value of NULL is 0 Any pointer can be assigned the value NULL: double* there = NULL;

Slide To Use NULL A definition of NULL is found in several libraries, including and A definition of NULL is found in several libraries, including and A using directive is not needed for NULL

Slide Linked Lists The previous diagram depicts a linked list A linked list is a list of nodes in which each node has a member variable that is a pointer that points to the next node in the list The first node is called the head The first node is called the head The pointer variable head, points to the first node The pointer variable head, points to the first node The pointer named head is not the head of the list…it points to the head of the list The last node contains a pointer set to NULL The last node contains a pointer set to NULL

Slide Building a Linked List: The node Definition Let's begin with a simple node definition: struct Node { int data; Node *link; }; typedef Node* NodePtr;

Slide Building a Linked List: Declaring Pointer Variable head With the node defined and a type definition to make our code easier to understand, we can declare the pointer variable head: NodePtr head; head is a pointer variable that will point to the head node when the node is created head is a pointer variable that will point to the head node when the node is created

Slide Building a Linked List: Creating the First Node To create the first node, the operator new is used to create a new dynamic variable: head = new Node; Now head points to the first, and only, node in the list Now head points to the first, and only, node in the list

Slide Building a Linked List: Initializing the Node Now that head points to a node, we need to give values to the member variables of the node: head->data = 3; head->link = NULL; Since this node is the last node, the link is set to NULL Since this node is the last node, the link is set to NULL

Slide Function head_insert It would be better to create a function to insert nodes at the head of a list, such as: void head_insert(NodePtr& head, int the_number); void head_insert(NodePtr& head, int the_number); The first parameter is a NodePtr parameter that points to the first node in the linked list The second parameter is the number to store in the list

Function head_insert head_insert will create a new node for the number head_insert will create a new node for the number The number will be copied to the new node The new node will be inserted in the list as the new head node

Create a new dynamic variable pointed to by temp_ptr Place the data in the new node called *temp_ptr Make temp_ptr's link variable point to the head node Make the head pointer point to temp_ptr Slide Pseudocode for head_insert

The pseudocode for head_insert can be written in C++ using these lines in place of the lines of pseudocode: NodePtr temp_ptr; //create temporary pointer temp_ptr = new Node; // create the new node NodePtr temp_ptr; //create temporary pointer temp_ptr = new Node; // create the new node temp_ptr->data = the_number; //copy the temp_ptr->data = the_number; //copy the //number //number temp_ptr->link = head; //new node points to //first node head = temp_ptr; // head points to new // first node temp_ptr->link = head; //new node points to //first node head = temp_ptr; // head points to new // first node Slide Translating head_insert to C++

Slide An Empty List A list with nothing in it is called an empty list An empty linked list has no head node The head pointer of an empty list is NULL head = NULL; Any functions written to manipulate a linked list should check to see if it works on the empty list Any functions written to manipulate a linked list should check to see if it works on the empty list

You might be tempted to write head_insert using the head pointer to construct the new node: head = new Node; head->data = the_number; Now to attach the new node to the list The node that head used to point to is now lost! The node that head used to point to is now lost! Slide Losing Nodes

Slide Memory Leaks Nodes that are lost by assigning their pointers a new address are not accessible any longer The program has no way to refer to the nodes and cannot delete them to return their memory to the freestore Programs that lose nodes have a memory leak Significant memory leaks can cause system crashes Significant memory leaks can cause system crashes Various operating systems are accused of having memory leaks - particularly Windows Various operating systems are accused of having memory leaks - particularly Windows

Slide Searching a Linked List To design a function that will locate a particular node in a linked list: We want the function to return a pointer to the node so we can use the data if we find it, else return NULL We want the function to return a pointer to the node so we can use the data if we find it, else return NULL The linked list is one argument to the function The linked list is one argument to the function The data we wish to find is the other argument The data we wish to find is the other argument This declaration will work: NodePtr search(NodePtr head, int target); This declaration will work: NodePtr search(NodePtr head, int target);

Refining our function We will use a local pointer variable, named here, to move through the list checking for the target We will use a local pointer variable, named here, to move through the list checking for the target The only way to move around a linked list is to follow pointers We will start with here pointing to the first node and move the pointer from node to node following the pointer out of each node We will start with here pointing to the first node and move the pointer from node to node following the pointer out of each node Slide Function search

Slide Pseudocode for search Make pointer variable here point to the head node while (here does not point to a node containing target AND here does not point to the last node) { make here point to the next node } If (here points to a node containing the target) return here; else return NULL;

Slide Moving Through the List The pseudocode for search requires that pointer here step through the list How does here follow the pointers from node to node? How does here follow the pointers from node to node? When here points to a node, here->link is the address of the next node To make here point to the next node, make the assignment: here = here->link; To make here point to the next node, make the assignment: here = here->link;

Slide Check for last node A Refinement of search The search function can be refined in this way: here = head; while (here->data != target && here->link != NULL) { here = here->next; } if (here->data = = target) return here; else return NULL;

Our search algorithm has a problem If the list is empty, here equals NULL before the while loop so… If the list is empty, here equals NULL before the while loop so… here->data is undefined here->link is undefined The empty list requires a special case in our search function The empty list requires a special case in our search function A refined search function that handles an empty list is shown in the next slide. A refined search function that handles an empty list is shown in the next slide. Slide Searching an Empty List

Slide Pointers as Iterators An iterator is a construct that allows you to cycle through the data items in a data structure to perform an action on each item An iterator can be an object of an iterator class, an array index, or simply a pointer An iterator can be an object of an iterator class, an array index, or simply a pointer A general outline using a pointer as an iterator: Node_Type *iter; for (iter = Head; iter != NULL; iter = iter->Link) //perform the action on the node iter //points to Head is a pointer to the head node of the list Head is a pointer to the head node of the list

Slide Iterator Example Using the previous outline of an iterator we can display the contents of a linked list in this way: NodePtr iter; for (iter = Head; iter != NULL; iter = iter->Link) cout data); iter = iter->Link) cout data);

Inserting and Deleting in Locations Other Than the Head Obviously, it is a bit more difficult to insert an node at a point other than the head of a list. We won't cover this as you will see these more sophisticated insertion and deletion routines for linked lists in a data structures class. We will not cover the rest of Chapter 13 from the middle of page 749 on.

Slide Assignment With Pointers- A Caution If head1 and head2 are pointer variables and head1 points to the head node of a list: head2 = head1; causes head2 and head1 to point to the same list There is only one list! There is only one list! If you want head2 to point to a separate copy, you must copy the list, node by node, or overload the assignment operator appropriately

Many other data structures can be constructed using nodes and pointers Doubly-Linked Lists Each node has two links, one to the next node and one to the previous node Each node has two links, one to the next node and one to the previous node Allows easy traversal of the list in both directions Allows easy traversal of the list in both directions Often used in graphics programs - i.e. scanning algorithms Often used in graphics programs - i.e. scanning algorithms Variations on Linked Lists Slide

Doubly-Linked List struct Node { int data; Node *forward_link; Node *forward_link; Node *back_link; Node *back_link;};

A tree is a data structure that looks like an upside-down tree with the root at the top No cycles No cycles In a binary tree each node has at most two links Binary Tree Slide struct TreeNode { int data; TreeNode *left_link; TreeNode *right_link; };

The preceding examples created linked lists of structs. We can also create linked lists using classes. The logic to use a class is identical except the syntax of using and defining a class should be substituted in place of that for a struct Linked List of Classes Slide

Slide Section 13.1 Conclusion Can you Write type definitions for the nodes and pointers in a linked list? Call the node type NodeType and call the pointer type PointerType. The linked lists will be lists of letters. Write type definitions for the nodes and pointers in a linked list? Call the node type NodeType and call the pointer type PointerType. The linked lists will be lists of letters. Explain why inserting into an array can be less efficient than inserting into a linked list? Explain why inserting into an array can be less efficient than inserting into a linked list?

Copyright © 2008 Pearson Addison-Wesley. All rights reserved Stacks and Queues (skipped as these are covered in data structures)

Slide Chapter End