Exercise 1 From 31-32 Lec80b-Ponter.ppt.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Chapter 17 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
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.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
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 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 14 Pointers and Linked Lists Slides by David B. Teague, Western Carolina University,
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
CSCE 210 Data Structures and Algorithms
Programming Circular Linked List.
COMP 53 – Week Eight Linked Lists.
Pointers and Linked Lists
Chapter 12 – Data Structures
Pointers and Linked Lists
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists Chapter 6 Section 6.4 – 6.6
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
What’s New? Six homework programming assignments. New: five
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
COMP 2710 Software Construction Inheritance
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Chapter 4 Linked Lists.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
COMP 2710 Software Construction Pointers
COMP 2710 Software Construction File I/O
CSCE 210 Data Structures and Algorithms
CSCI 3333 Data Structures Linked Lists.
Chapter 4 Linked Lists
Sequences 9/18/ :20 PM C201: Linked List.
Linked Data Structures
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Pointers and Linked Lists
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Linked Lists in C and C++
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Introduction to C++ Linear Linked Lists
Linked Lists.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Exercise 1 From 31-32 Lec80b-Ponter.ppt

Call-by-value Pointers Graphic: Display 10 Call-by-value Pointers Graphic: Display 10.5 The Function Call sneaky(p);

COMP 2710 Software Construction Linked List Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu Nodes and Linked Lists Creating, searching Linked List Applications Stacks, queues, sets, hash tables Friend classes, alternatives These slides are adapted from notes by Dr. Walter Savitch (UCSD)

Introduction Linked list Trees also use pointers Constructed using pointers Grows and shrinks during run-time Doubly Linked List : A variation with pointers in both directions Trees also use pointers Pointers backbone of such structures Use dynamic variables Standard Template Library Has predefined versions of some structures

Approaches Three ways to handle such data structures: C-style approach: global functions and structs with everything public Classes with private member variables and accessor and mutator functions Friend classes Linked lists will use method 1 Stacks, queues, sets, and hash tables will use method 2 Trees will use method 3

Nodes and Linked Lists Linked list Simple example of "dynamic data structure" Composed of nodes Each "node" is variable of struct or class type that’s dynamically created with new Nodes also contain pointers to other nodes Provide "links"

Display 17.1 Nodes and Pointers

Node Definition struct ListNode { string item; int count; ListNode *link; }; typedef ListNode* ListNodePtr; Order here is important! Listnode defined 1st, since used in typedef Also notice "circularity"

Head Pointer "head" is not a node: ListNodePtr head; A simple pointer to a node Set to point to 1st node in list Head used to "maintain" start of list Also used as argument to functions struct ListHead { string listname; int max; int min; ListNode *head; }; Question: What is missing in struct ListHead? How to maintain metadata of the list? (what metadata?)

Example Node Access (*head).count = 12; Alternate operator, -> 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

How to mark the end of a list? 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

Linked List Lists as illustrated called linked lists First node called head Pointed to by pointer named head Last node special also It’s member pointer variable is NULL Easy test for "end" of linked list struct ListNode { string item; int count; ListNode *link; }; struct ListHead { string listname; int max; int min; ListNode *head; };

Exercise 2 Task 1: Define a structure named node, where there are two items – (1) data whose type is int and (2) a pointer pointing to the next node Task 2: Using typedef to define a new type (e.g., nodePtr) of pointer pointing to node (i.e., node*) Task 3: Create a pointer (e.g., node_ptr) using the above new data type Task 4: Allocate memory resource for the new pointer Task 5: Assign value (e.g., 10) to the node pointed by node_ptr Spring’15: slides 14-16 - See also Lec08c2-Lnked List Exercise 1.ppt

Print a list of nodes struct node { int data; node *next; }; typedef node* nodePtr; void printList(nodePtr root); What two cases should we consider? Empty List Non-empty list void printList(nodePtr root) { nodePtr cur; if (root == NULL) cout << "This is an empty list\n"; cur = root; while (cur != NULL) { cout << cur->data << endl; cur = cur->next; }

Insert a node to the head of the list struct node { int data; node *next; }; typedef node* nodePtr; void insertNode(nodePtr& root, int info); //Another possible prototype void insertNode(nodePtr& root, nodePtr newNodePtr); How many cases should we consider? What are these cases? struct node { int data; node *next; }; typedef node* nodePtr; void insertNode(nodePtr& root, int info); void insertNode(nodePtr& root, int info) { nodePtr cur; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur->data = info; cur->next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Insert cur as the root of the list cur->next = root; }

Summary Node is struct or class object Linked list 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 In a doubly linked lists there are pointers in both directions End of linked list marked with NULL pointer

COMP 2710 Software Construction Linked List - References Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu Nodes and Linked Lists Creating, searching Linked List Applications Stacks, queues, sets, hash tables Friend classes, alternatives These slides are adapted from notes by Dr. Walter Savitch (UCSD)

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 © 2010 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 © 2010 Pearson Addison-Wesley. All rights reserved.

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

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

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

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

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

Display 17.7 Removing a Node Copyright © 2010 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 1st node containing target //If not found, returns NULL Simple "traversal" of list Similar to array traversal Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Pseudocode for search Function while (here doesn’t 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 © 2010 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 © 2010 Pearson Addison-Wesley. All rights reserved.