© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 16 – Shopping List Application: Introducing.

Slides:



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

Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Copyright © 2003 Pearson Education, Inc. Slide 1.
Author: Julia Richards and R. Scott Hawley
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
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
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 24 Lists, Stacks, and Queues
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Semantic Analysis and Symbol Tables
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
Pointers and Arrays Chapter 12
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
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 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
C++ Programming:. Program Design Including
Copy Constructor / Destructors Stacks and Queues
Linked Lists Chapter 6 Section 6.4 – 6.6
CS Data Structures Chapter 8 Lists Mehmet H Gunes
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists.
Pointers and Linked Lists
Basic C++ What’s a declaration? What’s a definition?
Indirection.
Presentation transcript:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 16 – Shopping List Application: Introducing Pointers, References and Dynamic Data Structures Outline 16.1 Test-Driving the Shopping List Application 16.2 Introducing Pointers 16.3 Pointer Operators 16.4 Passing Arguments to Functions by Reference 16.5 Designing the Shopping List Application 16.6 Constructing the Shopping List Application 16.7 Implementing a Linked List 16.8 Wrap-Up Files ShoppingList.cpp <- Applicaton ShoppingItem.h, ShoppingItem.cpp <- Object List.h, List.cpp <- Linked List

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Create and initialize pointers and references. –Store the address of a variable in a pointer. –Create a linked list of objects. –Access class members using a pointer. –Use dynamic memory management to create and delete objects.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.1Test Driving the Shopping List Application

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.1Test Driving the Shopping List Application (Cont.) Figure 16.1 Running the completed Shopping List application. Figure 16.2 Adding a new item to the shopping list. Figure 16.3 Viewing the shopping list contents.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.1Test Driving the Shopping List Application (Cont.) Figure 16.4 Adding more items to the shopping list. Figure 16.5 Viewing the updated shopping list contents. Items displayed in reverse order of input

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Introduction to Pointers Figure 16.6 Directly and indirectly referencing a variable.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Introduction to Pointers (Cont.) Figure 16.8 Representation of y and yPointer in memory.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Working with Pointers Reference the address of operator ( & ) –Returns the address of its operand Dereference value pointed by operator ( * ) –Also known as a unary indirection operatorunary indirection operator –Returns the value pointed by the name of the object to which the operand points The Reference and Dereference operators have complementary (or opposite) meanings. A variable referenced with & can be dereferenced with *.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Reference (the address of) Operator ( & ) The first statement assigns the value 25 to andy (a variable whose address in memory we have assumed to be 1776). The second statement copied to fred the content of variable andy (which is 25). This is a standard assignment operation, as you have done so many times before. Finally, the third statement copies to ted not the value contained in andy but a reference to it (i.e., its address, 1776). The reason is that in this third assignment operation we have preceded the identifier andy with the reference operator (&), so we were no longer referring to the value of andy but to its reference (its address in memory). The variable that stores the reference to another variable (like ted in the previous example) is what we call a pointer. Source: 1 andy = 25; 2 fred = andy; 3 ted = &andy;

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Dereference value pointed by Operator ( * ) Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by". 1 andy = 25; 2 ted = &andy; 3 4 beth = ted; // beth equal to ted ( 1776 ) 5 beth = *ted; // beth equal to value pointed by ted ( 25 ) Source:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declaring a Variable of Type Pointer The declaration of pointers follows this format: Each points to a different data type. ̶ The data to which they point to do not occupy the same amount of memory (the data). Each pointer occupies the same amount of space in memory (the address). 1 int * number; 2 char * character; 3 float * greatnumber Source: the asterisk (*) only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator that we have seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declaring a Variable of Type Pointer (Cont.) 1 // my first pointer 2 #include 3 using namespace std; 4 5 int main () 6 { 7 int firstvalue, secondvalue; 8 int * mypointer; 9 10 mypointer = &firstvalue; 11 *mypointer = 10; 12 mypointer = &secondvalue; 13 *mypointer = 20; 14 cout << "firstvalue is " << firstvalue << endl; 15 cout << "secondvalue is " << secondvalue << endl; 16 return 0; 17 } Declaring pointer variable mypointer of type integer. Dereference value pointed by operator

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Pointer Initialization Pointers can be initialized to 0, NULL or an address ̶ Symbolic constant NULL defined in as equal to 0 ̶ 0 is the only integer that can be directly assigned to a pointer Initialize pointers to avoid pointing to unknown or uninitialized areas of memory 1 int number; 2 int *tommy = &number; The behavior of this code is equivalent to : 1 int number; 2 int *tommy; 3 tommy = &number; Reference the address of operator Declaring pointer variable tommy of type int

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Working with References What is a Reference? –It's an alias for an object - another name by which it can be called. The implementation is frequently identical to that of pointers. But don't think of references as pointers - a reference is the object. –Strange phrases like "a reference IS the object" are used quite frequently in the C++ community. Such claims are only useful to hide the fact that C++ pointers & references are so similar that having both in the language is an unnecessary complication. In other contexts, the claims are simply false. Source:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Then Why References? Both C and C++ provide pointers as a way of referring to objects indirectly. C++ added references as an alternative mechanism for doing what is essentially the same job. C++ reference types enable overloaded operators to look like built-in operators, as well as act like them.overloaded operators Source:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declaring and Initializing a Variable of Type Reference A reference declaration is nearly identical to a pointer declaration, except that a reference declaration uses the & operator instead of the * operator. For example, given: 1 int i = 3; then: 2 int *pi = &i; Declares pi as an object of type "pointer to int" whose initial value is the address of object i, while: 3 int &ri = i; Declares ri as an object of type "reference to int " referring to i. The ampersand (&), as used in line 3 is not the reference operator (although both use the same sign: &). Remember, they are two different functions of one sign. Source: Reference the address of operator Declaring Reference variable ri of type integer.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Declaring and Initializing a Variable of Type Reference Nomenclature –Initializing a reference to refer to an object is often described as "binding the reference to the object." –many programmers pronounce " int * " as "int star," they usually pronounce " int & " as "int ref." Source:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. The Difference between Pointer and Reference Variables You can use a reference as if it were a value : myRef.myData, versus myPtr->myData, which is equivalent to ( *myPtr).myData (in this sense my Ref behaves like ( *myPtr )). A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization. –In Fact, a reference must be initialized to point to an object - otherwise, the code won't compile. –After the initialization, you can't make a pointer, point to any number of objects. A pointer can point to NULL while reference can never point to NULL –In fact, a wide class of bugs comes from accessing dangling references - references to objects which were already destroyed. Source:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. The Difference between Pointer and Reference Variables You can't take the address of a reference (it is bound to the object) like you can with pointers (forming a pointer to a pointer). We will see an example of this when we study linked lists. There's no "reference arithmetics" –but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5. Source:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. When to use a Reference or Pointer Pointers can do almost everything that references can do, but they can lead to expressions that don't look right. On the other hand, references have some restrictions that make them less convenient than pointers for implementing algorithms and data structures. As a general rule, –Use pointers to implement algorithms and data structures. –Use references in function parameters and return types to define attractive interfaces. We look at Passing Arguments to Functions by Reference or Pointer in the next section. Source:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference or Pointer There two (2) ways to perform a pass-by-reference 1.pass-by-reference with reference (&) argument 2.pass-by-reference with pointer (*) argument Review A parameter is the thing used to define a method or function while an argument is the thing you use to call a method or function. Parameter: void mySubroutine (int N){ … } N is a parameter Argument: int X; X = 10; mySubroutine(X); X is an argument

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference To indicate that a function passes parameters by reference, follow the variable type with an ampersand ( & ) in the function prototype Figure 16.9 Declaring a function that declares a reference parameter. Declaring a function that declares an int reference parameter Figure Passing an argument by reference using a reference argument. Using a variable name to pass-by-reference using a reference argument

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference (Cont.) Passing variables by reference allows the called function to directly modify the original variable in the caller. In this example the variable input. Figure Defining a function that declares a reference parameter. Incrementing the reference parameter

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference (Cont.) Figure Output demonstrating the value of input before and after passing a value by reference with a reference argument.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Pointer To pass a function parameter as a pointer, follow the parameters type in the function prototype with an asterisk ( * ) The asterisk (*), as used here is not the dereference operator (although both use the same sign: *). Remember, they are two different functions of one sign. Figure Declaring a function that declares a pointer parameter. Declaring a function that declares an int pointer parameter

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Pointer (Cont.) Figure Passing a variable by reference using a pointer argument. Using the address of operator to pass-by- reference using a pointer

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Pointer (Cont.) Always use parenthesis to make order of operation clear. The asterisk (*), as used in line 53 is not the dereference operator (although both use the same sign: *). Remember, they are two different functions of one sign. Figure Defining a function that declares a pointer argument. Increment the value Declaring Pointer variable countPointer of type integer. Dereference value pointed by operator

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Pointer (Cont.) Figure Output showing input before and after passing it by reference using reference and pointer arguments. Recap Using a reference as a function parameter provides an attractive interface, while using a pointer parameter explicitly shows that a link exists between the parameter and the argument.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.4Passing Arguments to Functions by Reference (Cont.) Array names are actually pointers pointing to element zero in the array –arrayName is equivalent to &arrayName[ 0 ] –This is why arrays are always pass-by-reference as a pointer.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Display a menu containing options for adding a new item to the list, displaying the shopping list and exiting the application Prompt the user for and input the menu selection If the user chose to add a new item to the list Prompt the user for and input the name and quantity of the item Create a new ShoppingItem object Add the new ShoppingItem to the linked List of ShoppingItems If the user chose to display the shopping list For each item in the list Display the items name and quantity If the user chose to exit Delete each ShoppingItem and exit the application Figure Pseudocode for the ShoppingList application. 16.5Designing the Shopping List Application

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.5Designing the Shopping List Application (Cont.) Figure ShoppingItem class definition. ShoppingItem constructor Get and set functions Member function that displays ShoppingItem information Defining variables that represent an items name and quantity Link data member Pointer variable next of type ShoppingItem Declares a shoppingItem pointer parameter Next Object in a linked list Declares a string reference parameter Objects name Quantity

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.5Designing the Shopping List Application (Cont.) A self-referential class contains a pointer to an object of the same class Not setting the link in the last node of a linked data structure to NULL (0) is a (possibly fatal) logic error. Figure Two self-referential class objects linked together. Self-referential class objects linked together to form a list Pointer to the first self- referential class object in the list

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.5Designing the Shopping List Application (Cont.) Linked list –Collection of linked nodes –Length is dynamic Can increase and decrease as necessary –Singly-linked list has one link per node Figure Graphical representation of a linked list.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.6Constructing the Shopping List Application Figure Including the ShoppingItem and List class definitions. Including the ShoppingItem.h and List.h header files ShoppingList C++ application Figure Defining variables to store user input. Defining variables to store user input

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.6Constructing the Shopping List Application (Cont.) Invoking the List class default constructor Figure Creating a List object. Creating a List object C++ is case sensitive so shoppingList the list is not the same as ShoppingList the application

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Menu Selection Case 1 Figure 16.2 Adding a new item to the shopping list.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Menu Selection Case 1 Figure Prompting the user for and inputting the items name and quantity. The ignore function removes one character from the input stream Prompting the user for and inputting the items name and quantity Figure Adding a new ShoppingItem object to ShoppingList. Add a new ShoppingItem to the linked list

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Menu Selection Case 2 Figure Displaying the name and quantity for each ShoppingItem in the linked list. Displaying the contents of the shopping list Figure 16.3 Viewing the shopping list contents.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Menu Selection Case 3 Applications should delete any dynamic memory allocated during run-time. Figure Deleting each ShoppingItem object from the ShoppingList. Deleting all ShoppingItem objects Figure Terminating the Shopping List application.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure List class definition. 16.7Implementing a Linked List List constructor Declaring set and get functions for the pointer to the first item in the list Declaring functions to manipulate the linked list of ShoppingItems Defining a pointer to the first item in the linked list List header file

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Keyword this –Inside a member function, you can explicitly refer to the object of that class on which the function was called with the keyword this Arrow operator ( -> ) –Shorthand notation for use with pointers –myObject->myData is equivalent to ( *myObject ).myData Figure List constructor definition. Initializing the firstItem variable as a null pointer List C++ object

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure Graphical representation of inserting an item at the front of a linked list. 16.7Implementing a Linked List (Cont.)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Dynamic memory allocation –Technique of obtaining memory during execution time –Allocated memory can be released so that it can be reused later new operator –Takes as an argument the type of object being created –Returns a pointer to newly created object Figure Creating a ShoppingItem object in the addItem function. Creating a ShoppingItem using the new operator

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Assign NULL (zero) to the link member of a new node –Uninitialized pointers often lead to dangerous runtime errors Figure Updating pointers when adding an item to the front of a linked list. Updating the new ShoppingItems nextItem variable and the Lists firstItem variable

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure Defining the displayList function. Creating a pointer to the first item in the linked list Displaying a header

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure Displaying each ShoppingItems name and quantity. Displaying the name and quantity for each ShoppingItem

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Deallocating memory –Not releasing dynamically allocated memory when it is no longer needed can cause the system to run out of memory prematurely delete operator –Runs the destructor and deallocates the memory allocated by the new operator –Returns memory to the system

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure Defining the deleteList function. Setting currentItem so that it points to the first item in the linked list Repeat until reaching the end of the list Variable currentItem is a pointer to type ShoppingItem

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Destructors –Functions that run just before an objects memory is released Figure Deleting each ShoppingItem in the linked list. Store a temporary copy of the pointer to the next ShoppingItem Delete the ShoppingItem to which currentItem points Move to the next item in the List Temporary variable nextItem is a pointer to type ShoppingItem

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure Shopping List application after entering several items.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16.7Implementing a Linked List (Cont.) Figure Terminating the Shopping List application.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.h (1 of 2) List constructor

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.h (2 of 2) Get and set functions for the pointer to the first item in the list Declaring functions to manipulate the linked list of ShoppingItem s Pointer to the first item in the linked list

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (1 of 5) Initialize the firstItem as a null pointer

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (2 of 5)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (3 of 5) Update the firstItem to point to the new ShoppingItem Update the appropriate pointers to add the new item to the list

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (4 of 5) Create a pointer to the first item in the linked list Display a header Display the name and quantity for each ShoppingItem

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. List.cpp (5 of 5) Create a pointer to the first item in the linked list Delete each ShoppingItem in the linked list

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.h (1 of 2) ShoppingItem constructor Get and set functions

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.h (2 of 2) Member function that displays ShoppingItem information Defining variables that represent an item name and quantity Link data member

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.cpp (1 of 4)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.cpp (2 of 4)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.cpp (3 of 4)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingItem.cpp (4 of 4)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingList.cpp (1 of 4) Include the ShoppingItem.h and List.h header files Define variables to store user input Create a List object

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingList.cpp (2 of 4) Remove the newline from the input stream Prompt the user for and input the items name and quantity Add a new ShoppingItem to the linked list

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingList.cpp (3 of 4) Display the contents of the shopping list Delete all ShoppingItem objects in the linked list

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ShoppingList.cpp (4 of 4)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 16 Shopping List Application. Turn in annotated source file with your own comments for Exercise or Exercise if you finish the design challenge. Answer and Turn-in Tutorial 16 Questions 16.1 to Note: The author defines synonym on page 371. Always write the question followed by the answer. Remember to highlight the answer. Exercises and For both exercises start from your completed Tutorial. Due next Wednesday

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lecture 2 – Introduction to Linked Lists

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lecture 3 – Data Operations on Linked List

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lecture 3 – Data Operations on Linked List