1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.

Slides:



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

Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.
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.
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.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
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.
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Chapter 4 ADT Sorted List.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
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.
1 Nell Dale Chapter 3 ADTs Unsorted List and Sorted List 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.
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 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.
Implementing a Sorted List as a Linked Structure CS 308 – Data Structures.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
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.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
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.
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 -
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
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.
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.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Data Structures: Stacks Queues 1. 2 Stack ADT: What is a Stack? a stack is a varying-length, collection of homogeneous elements Insertion and Deletion.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
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 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
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.
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”
1 Chapter 4 ADT Sorted List. 2 Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
Chapter 14 Dynamic Data and Linked Lists
Linked Lists Chapter 6 Section 6.4 – 6.6
C++ Plus Data Structures ADTs Unsorted List and Sorted List
TA: Nouf Al-Harbi Data Structures LAB 3 TA: Nouf Al-Harbi
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Chapter 4 Linked Lists.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Unsorted Lists CS3240, L. Grewe.
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Yan Shi CS/SE 2630 Lecture Notes
Data Structures and Algorithms Memory allocation and Dynamic Array
Presentation transcript:

1 Chapter 6 Lists Plus

ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators n ResetList n GetNextItem change state observe state process all

3 class SortedType MakeEmpty ~SortedType DeleteItem. InsertItem SortedType RetrieveItem GetNextItem ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos ?

4 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” element.

5 External Pointer to the Last Node

6 What is a Doubly Linked List? l A doubly linked list is a list in which each node is linked to both its successor and its predecessor.

7 Linking the New Node into the List

8 Deleting from a Doubly Linked List

9 What are Header and Trailer Nodes? l A Header Node is a node at the beginning of a list that contains a key value smaller than any possible key. l A Trailer Node is a node at the end of a list that contains a key larger than any possible key. l Both header and trailer are placeholding nodes used to simplify list processing.

10 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 stack items can take place only at the top of the stack. l A stack is a LIFO “last in, first out” structure.

Stack ADT Operations l IsEmpty -- Determines whether the stack is currently empty. l IsFull -- Determines whether the stack is currently full. l Push (ItemType newItem) -- Adds newItem to the top of the stack. l Pop -- Removes the item at the top of the stack and returns it in item. l Top -- Returns a copy of the top item 11

12 class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType 20 30

13 What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack?

14 Pass by value makes a shallow copy StackType MyStack; // CLIENT CODE. MyFunction( MyStack ); // function call Private data: topPtr 7000 Private data: topPtr 7000 MyStack SomeStack shallow copy

15 Shallow Copy vs. Deep Copy l A shallow copy copies only the class data members, and does not copy any pointed-to data. l A deep copy copies not only the class data members, but also makes separately stored copies of any pointed-to data.

16 What’s the difference? lA shallow copy shares the pointed to data with the original class object. lA deep copy stores its own copy of the pointed to data at different locations than the data in the original class object.

17 Making a deep copy Private data: topPtr 7000 SomeStack Private data: topPtr 5000 MyStack deep copy

18 // FUNCTION CODE template void MyFunction( StackType SomeStack ) // Uses pass by value { ItemType item; SomeStack.Pop(item);. } WHAT HAPPENS IN THE SHALLOW COPY SCENARIO? 18 Suppose MyFunction Uses Pop

19 MyStack.topPtr is left dangling ? 30 StackType MyStack; // CLIENT CODE. MyFunction( MyStack ); Private data: topPtr 6000 MyStack SomeStack shallow copy Private data: topPtr 7000

20 MyStack.topPtr is left dangling ? 30 Private data: topPtr 6000 MyStack SomeStack shallow copy Private data: topPtr 7000 NOTICE THAT NOT JUST FOR THE SHALLOW COPY, BUT ALSO FOR ACTUAL PARAMETER MyStack, THE DYNAMIC DATA HAS CHANGED!

21 As a result... l This default method used for pass by value is not the best way when a data member pointer points to dynamic data. l Instead, you should write what is called a copy constructor, which makes a deep copy of the dynamic data in a different memory location.

22 More about copy constructors l When there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value. l You do not call the copy constructor. l Like other constructors, it has no return type. l Because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition.

23 Copy Constructor l Copy constructor is a special member function of a class that is implicitly called in these three situations: n passing object parameters by value, n initializing an object variable in a declaration, n returning an object as the return value of a function.

24 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK template class StackType { public: StackType( ); // Default constructor. // POST: Stack is created and empty. StackType( const StackType & anotherStack ); // Copy constructor. // Implicitly called for pass by value.. ~StackType( ); // Destructor. // POST: Memory for nodes has been deallocated. private: NodeType * topPtr ; }; 24

25 CLASS CONSTRUCTOR CLASS COPY CONSTRUCTOR CLASS DESTRUCTOR Classes with Data Member Pointers Need

26 template // COPY CONSTRUCTOR StackType :: StackType( const StackType & anotherStack ) {NodeType * ptr1 ; NodeType * ptr2 ; if ( anotherStack.topPtr == NULL ) topPtr = NULL ; else // allocate memory for first node {topPtr = new NodeType ; topPtr->info = anotherStack.topPtr->info ; ptr1 = anotherStack.topPtr->next ; ptr2 = topPtr ; while ( ptr1 != NULL )// deep copy other nodes {ptr2->next = new NodeType ; ptr2 = ptr2->next ; ptr2->info = ptr1->info ; ptr1 = ptr1->next ; } ptr2->next = NULL ; } 26

27 What About the Assignment Operator? l The default method used for assignment of class objects makes a shallow copy. l If your class has a data member pointer to dynamic data, you should write a member function to overload the assignment operator to make a deep copy of the dynamic data.

28 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK template class StackType { public: StackType( ); // Default constructor. StackType( const StackType & anotherStack ); // Copy constructor. void operator= ( StackType ); // Overloads assignment operator.. ~StackType( ); // Destructor. private: NodeType * topPtr ; }; 28

29 C++ Operator Overloading Guides 1All operators except these ::. sizeof ?: may be overloaded. 2At least one operand must be a class instance. 3You cannot change precedence, operator symbols, or number of operands. 4Overloading ++ and -- requires prefix form use by default, unless special mechanism is used. 5To overload these operators = ( ) [ ] member functions (not friend functions) must be used. 6An operator can be given multiple meanings if the data types of operands differ.

Using Overloaded Binary operator+ When a Member Function was defined myStack + yourStack myStack.operator+(yourStack) When a Friend Function was defined myStack + yourStack operator+(myStack, yourStack) 30

31 Overloading Operators l C++ allows us to *redefine* the meaning of the relational operators in terms of the data members of a class l This redefinition is called overloading an operator l A simple example was covered in chapter 3

32 Overloading l class StrType l { l public l : l bool operator == (StrType otherstring) const; l : l }

33 Overloaded == operator l bool StrType::operator == (strType otherstring) const l { l int result; l result = std::strcmp(letters, otherstring.letters); l if (result == 0) l return true; l else l return false; l }

34 Overloading an Operator l How to use an overloaded operator? l Overloaded operators can only be used with data items that belong to user defined classes l StrType a; l StrType b; l if (a == b) cout<<“Yes indeed”<<endl; l else cout<<“Sorry! They are not equal”<<endl;

35 Another Example l //Overloading syntax example modified from original program at l #include l using namespace std; l class Integer { l int i; l public: l Integer(int ii) l { i = ii; } l const Integer l operator+(const Integer& rv) const { l cout << "operator+" << endl; l return Integer(i + rv.i); l }

36 l Integer& l operator+=(const Integer& rv){ l cout << "operator+=" << endl; l i += rv.i; l return *this; l } l void display() l { l cout<<i<<endl; l } l };

37 Client Code l void main() { l cout << "built-in types:" << endl; l int i = 1, j = 2, k = 3; l k += i + j; l cout<<"k is now "<<k<<endl; l cout << "user-defined types:" << endl; l Integer I(1), J(2), K(3); l K += I + J; l cout<<"K is now "<<endl; l K.display(); l } ///:~

38 A Sorted list Stored in an Array of Nodes

39 An Array with Linked List of Values and Free Space

40 An Array with Three Lists (Including the Free List)