Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 17-2 Learning Objectives Nodes and Linked Lists Creating,

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Chapter 25 Lists, Stacks, Queues, and Priority Queues
TK1924 Program Design & Problem Solving Session 2011/2012
AP STUDY SESSION 2.
1
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 8 Operator Overloading, Friends, and References.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
The 5S numbers game..
1.
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists Chapter 4.
Chapter 24 Lists, Stacks, and Queues
Abstract Data Types and Algorithms
Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSE Lecture 12 – Linked Lists …
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Adding Up In Chunks.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
Essential Cell Biology
Clock will move after 1 minute
PSSA Preparation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
Chapter 17 Linked Data Structures. Learning Objectives Nodes and Linked Lists – Creating, searching Linked List Applications – Stacks, queues, sets, hash.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Copyright © 2002 Pearson Education, Inc. Slide 1.
COMP 53 – Week Eight Linked Lists.
Pointers and Linked Lists
Chapter 12 – Data Structures
Pointers and Linked Lists
Copy Constructor / Destructors Stacks and Queues
What’s New? Six homework programming assignments. New: five
Exercise 1 From Lec80b-Ponter.ppt.
Pointers and Linked Lists
Presentation transcript:

Chapter 17 Linked Data Structures

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating, searching Linked List Applications Stacks, queues Friend classes, alternatives Iterators Pointers as iterators Trees

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Introduction Linked list Constructed using pointers Grows and shrinks during run-time Trees also use pointers Pointers backbone of such structures Use dynamic variables Standard Template Library Has predefined versions of some structures

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Approaches Three ways to handle such data structures: 1. C-style approach: global functions and structs with everything public 2. Classes with private member variables and accessor and mutator functions 3. Friend classes Linked lists will use method 1 Stacks, queues will use method 2 Trees will use method 3

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Nodes and Linked Lists Linked list Simple example of "dynamic data structure" Composed of nodes Each "node" is variable of struct or class type thats dynamically created with new Nodes also contain pointers to other nodes Provide "links"

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 17.1 Nodes and Pointers

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Node Definition struct ListNode { string item; int count; ListNode *link; }; typedef ListNode* ListNodePtr; Order here is important! Listnode defined 1 st, since used in typedef Also notice "circularity"

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Head Pointer Box labeled "head" not a node: ListNodePtr head; A simple pointer to a node Set to point to 1 st node in list Head used to "maintain" start of list Also used as argument to functions

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Example Node Access (*head).count = 12; Sets count member of node pointed to by head equal to 12 Alternate operator, -> Called "arrow operator" Shorthand notation that combines * and. head->count = 12; Identical to above cin >> head->item Assigns entered string to item member

Copyright © 2006 Pearson Addison-Wesley. All rights reserved End Markers Use NULL for node pointer Considered "sentinel" for nodes Indicates no further "links" after this node Provides end marker similar to how we use partially-filled arrays

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 17.2 Accessing Node Data

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Linked List Lists as illustrated called linked lists First node called head Pointed to by pointer named head Last node special also Its member pointer variable is NULL Easy test for "end" of linked list

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Linked List Class Definition class IntNode { public: IntNode() { } IntNode(int theData, IntNOde* theLink) : data(theData), link(theLink) { } IntNode* getLink() const {return link;} int getData() const{return data;} void setData(int theData) {data = theData;} void setLink(IntNode* pointer) {link=pointer;} private: int data; IntNode *link; }; typedef IntNode* IntNodePtr;

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Linked List Class Notice all member function definitions are inline Small and simple enough Notice two-parameter constructor Allows creation of nodes with specific data value and specified link member Example: IntNodePtr p2 = new IntNode(42, p1);

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Create 1 st Node IntNodePtr head; Declares pointer variable head head = new IntNode; Dynamically allocates new node Our 1 st node in list, so assigned to head head->setData(3); head->setLink(NULL); Sets head node data Link set to NULL since its the only node!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 17.3 Adding a Node to the Head of a Linked List

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Lost Nodes Pitfall: Display 17.5 Lost Nodes

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 17.6 Inserting in the Middle of a Linked List (1 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 17.6 Inserting in the Middle of a Linked List (2 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display 17.7 Removing a Node

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Searching a Linked List Function with two arguments: IntNodePtr search(IntNodePtr head, int target); //Precondition: pointer head points to head of //linked list. Pointer in last node is NULL. //If list is empty, head is NULL //Returns pointer to 1 st node containing target //If not found, returns NULL Simple "traversal" of list Similar to array traversal

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Pseudocode for search Function while (here doesnt point to target node or last node) { Make here point to next node in list } if (here node points to target) return here; else return NULL;

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Algorithm for search Function while (here->getData() != target && here->getLink() != NULL) here = here->getLink(); if (here->getData() == target) return here; else return NULL; Must make "special" case for empty list Not done here

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Stacks Stack data structure: Retrieves data in reverse order of how stored LIFO – last-in/first-out Think of like "hole in ground" Stacks used for many tasks: Track C++ function calls Memory management Our use: Use linked lists to implement stacks

Copyright © 2006 Pearson Addison-Wesley. All rights reserved A StackGraphic: Display A Stack

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display Interface File for a Stack Template Class (1 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display Interface File for a Stack Template Class (2 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Stack Template Class Driver: Display Program Using the Stack Template Class (1 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Stack Template Class Driver: Display Program Using the Stack Template Class (2 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Stack Template Class Driver: Display Program Using the Stack Template Class (3 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Stack Push and Pop Adding data item to stack push Considered "pushing" data onto stack Recall: goes to "top" of stack Removing data item from stack pop Considered "popping" item off stack Recall: removed from "top" of stack

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Queues Another common data structure: Handles data in first-in/first-out manner (FIFO) Items inserted to end of list Items removed from front Representation of typical "line" forming Like bank teller lines, movie theatre lines, etc.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display Interface File for a Queue Template Class (1 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display Interface File for a Queue Template Class (2 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Display Interface File for a Queue Template Class (3 of 3)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Queue Template Class Driver: Display Program Using the Queue Template Class

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Friend Classes Recall constant use of getLink and setlink accessor and mutator functions Somewhat of a nuisance Similar to making data public?! Public makes available to ALL! Use friend class Make queue template class "friend" of node template class All private link members directly available in member functions of queue class!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Forward Declaration Class friendships typically require classes reference each other Presents problem How can "both" be declared at same time? Requires forward declaration Simple class heading given inside other: class Queue; //Forward Dec. Announces "class Queue will exist"

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Iterators Construct for cycling through data Like a "traversal" Allows "whatever" actions required on data Pointers typically used as iterators Seen in linked list implementation

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Pointers as Iterators Recall: linked list: "prototypical" data structure Pointer: "prototypical" example of iterator Pointer used as iterator by moving thru linked list node by node starting at head: Example: Node_Type *iterator; for (iterator = Head; iterator != NULL; iterator=iterator->Link) Do_Action

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Iterator Classes More versatile than pointer Typical overloaded operators: ++advances iterator to next item --retreats iterator to previous item ==Compares iterators !=Compare for not equal *Accesses one item Data structure class would have members: begin(): returns iterator to 1 st item in structure end(): returns iterator to test if at end

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Iterator Class Example Cycle through data structure named ds: for (i=ds.begin();i!=ds.end();i++) process *i //*i is current data item i is name of iterator

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Trees Introduction Trees can be complex data structures Only basics here: Constructing, manipulating Using nodes and pointers Recall linked list: nodes have only one pointer next node Trees have two, & sometimes more, pointers to other nodes

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Tree Structure: Display A Binary Tree (1 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Tree Structure: Display A Binary Tree (2 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Tree Properties Notice paths From top to any node No "cycles" – follow pointers, will reach "end" Notice here each node has two links Called binary tree Most common type of tree Root node Similar to linked lists head Leaf nodes Both link variables are NULL (no subtrees)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Trees and Recursion Note trees "recursive structure" Each tree has two subtrees Each subtree has two subtrees Etc., etc. Makes trees amenable to recursive algorithms For searching especially!

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Tree Processing Preorder Processing: 1. Process data in root node 2. Process left subtree 3. Process right subtree In-order Processing: 1. Process left subtree 2. Process data in root 3. Process right subtree Postorder Processing: 1. Process left subtree 2. Process right subtree 3. Process data in root

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Tree Storage Our example stored values in special way: Called binary search tree storage rule: 1. values in left subtree less than root value 2. values in right subtree greater than root 3. rule applies recursively to each subtree Trees using this storage mechanism: Called binary search tree (BST) Traversals: Inorder values "in order" Preorder "prefix" notation Postorder "postfix" notation

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Summary 1 Node is struct or class object One or more members is pointer Nodes connected by member pointers Produce structures that grow and shrink at runtime Linked list List of nodes where each node points to next End of linked list marked with NULL pointer

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Summary 2 Stack is LIFO data structure Queue is FIFO data structure Iterator construct allows cycling through data items in given data structure Tree data structures Nodes have two member pointers Each point to other nodes/subtrees Binary search tree Special storage rules allow rapid searches