Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Advertisements

Review Learn about linked lists
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 ADT Sorted List.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
Chapter 14: Overloading and Templates
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Data Structures Using C++ 2E
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
Templates Zhen Jiang West Chester University
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 6 Lists Plus Lecture 12. What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 What is a Circular Linked List? l A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first”
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
Linked Lists Chapter 6 Section 6.4 – 6.6
Andy Wang Object Oriented Programming in C++ COP 3330
C++ Plus Data Structures
C++ Plus Data Structures
Chapter 8: Class Relationships
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Data Structures and Algorithms Memory allocation and Dynamic Array
SPL – PS3 C++ Classes.
Presentation transcript:

Lists + CS3240, L. Grewe 1

2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked list with a header node or a trailer node or both Implement a doubly linked list Distinguish between shallow copying and deep copying

3 Goals Overload C++ operators Implement a linked list as an array of records Implement dynamic binding with virtual functions

4 C++ Templates Generic Data Type A data type for which the operations are defined but the types of the items being manipulated are not Template A C++ construct that allows the computer to generate multiple versions of a class type or a function by allowing parameterized types Templates implement generic types

5 C++ Templates template class StackType {... } // client code StackType ; Formal parameter Actual parameters

6 C++ Templates Summary The formal parameters are in the class template definition The actual parameters are in the client code Both formal and actual parameters are enclosed in angle brackets The actual parameter can be any type, built- in or user-defined

7 C++ Templates What about function templates? Formal parameter precedes function definition and follows class name before scope resolution operator

8 C++ Templates template void StackType ::Push(ItemType newItem) { if (IsFull()) throw FullStack(); else { top++; items[top] = newItem; } Formal parameters

9 Circular Linked Lists Circular linked list A list in which each node has a successor; the “last” element is succeeded by the “first” element

10 Circular Linked List Why is it better to have the external pointer point to the last element ?

11 Circular Linked List Initialization for search

12 Circular Linked List FindItem cases

13 Circular Linked List Insert an item

14 Circular Linked List Delete an Item

15 Doubly Linked List Doubly linked list A is in which each node is linked to both its successor and its predecessor

16 Doubly Linked List Insert an item

17 Doubly Linked List Does it matter in which order we change the pointers ?

18 Doubly Linked List Delete an item

19 Header and Trailer Nodes Header Node A placeholder node at the beginning of a list that contains a key value smaller than any possible key Trailer Node A placeholder node at the end of a list that contains a key larger than any possible key How do header/trailer nodes simplify processing ?

20 Copy Structures void CopyStack(StackType oldStack), StackType& copy) { StackType tempStack; ItemType item; while (!oldStack.IsEmpty()) { item = oldStack.Top(); oldStack.Pop(); temptStack.Push(item); } while (!tempStack.IsEmpty()) { item = tempStack.Top(); copy.Pop(); } What is the status of oldStack? copy? Does the implementation structure matter?

21 Copy Structures oldStack is a value parameter; doesn’t that protect it from change? Yes, but only in the array-based implementation private: int top; ItemType items[MAX_ITEMS]; top and items are enclosed within the stack object

22 Copy Structures In a linked structure, the external pointer is enclosed within the stack object, but the linked structure to which it points is not What causes this problem ?

23 Copy Structures Shallow copy An operation that copies one class object to another without copying pointed-to data Deep copy An operation that not only copies one class object to another but also makes copies of any pointed-to data See the difference?

24 Copy Structures

25 Copy Structures In summary  A shallow copy shares the pointed to data with the original class object  A deep copy stores its own copy of the pointed to data at different locations than the data in the original class object How do we make a deep copy?

26 Copy Structures Private data: topPtr 7000 SomeStack Private data: topPtr 5000 MyStack deep copy Yes, but how?

27 Copy Structures Deep Copy if anotherStack.topPtr is NULL Set topPtr to NULL else Set topPtr to the address of a newly allocated node Set Info(topPtr) to Info(anotherStack.topPtr) Set ptr1 to Next(anotherStack.topPtr) Set ptr2 to topPtr while ptr1 is not NULL Set Next(ptr2) to the address of a newly allocated node Set ptr2 to next(ptr2) Set Info(ptr2) to Info(ptr1) Set ptr1 to Next(ptr1) Set Next(ptr2) to NULL

28 Copy Structures Relative position of pointers at the beginning of each iteration

29 Copy Structures Copy constructor A special member function of a class that is implicitly invoked when passing a parameter by value, initializing a variable in a declaration, and returning an object as the value of function StackType(const StackType& anotherStack); Of course, the code should implement a deep copy!

30 Copy Structures That about the assignment operator?  The assignment operator does a shallow copy!  We can overload the assignment operator class StackType { public:... void operator=(StackType); private:... void StackType::operator=(StackType anotherStack) // code for a deep copy } Can we overload other operators ?

31 Copy Structrres Al relational operators can be overloaded bool operator<(ItemType other) const; // Returns true if self is less than other bool operator>(ItemType other) const; // Returns true if self is greater than other bool operator==(ItemType other) const; // Returns true if self is equal to other... Of course, you must write the code as for any other function

32 Array-of-Records Implementation Array in static storage Array in dynamic storage

33 Array-of-Records Implementation Linked list in static storage Linked list in dynamic storage

34 Array-of-Records Implementation Sorted list

35 Array-of-Records Implementation A sorted list of values and a list of free space

36 Array-of-Records Implementation Two linked lists plus free list

37 Array-of-Records Implementation One structure for each list

38 Polymorphism The ability to determine which of several operations with the same name to apply to a particular object; a combination of static and dynamic binding Binding The time at which a name or symbol is bound to the appropriate static binding: bound at compile time dynamic binding: bound at run time

39 Polymorphism NewItemType is derived from ItemType void PrintResult(ItemType& first, ItemType& second); How can this be?

40 Polymorphism C++ relaxes scope rules to allow dynamic binding The type of the actual parameter may be an object of a derived class of the formal parameter virtual void PrintResult(ItemType& first, ItemType& second); Word virtual in base class (ItemType), definition forces dynamic binding Note: Parameters must be reference

41 Circular Doubly Linked List What are the advantages of this structure?

42 Circular Doubly Linked List

43 C++ Tips 1.Operators ::. sizeof, and ?: may not be overloaded 2.At least one operand must be a class instance 3.Precedence, operator symbols, or number of operands cannot be changed 4.Overloading ++ and -- requires prefix form use by client 5.To overload these operators = ( ) [ ], member functions must be used 6.An operator can be given multiple meanings if the data types of operands differ