C++ Plus Data Structures

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.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
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.
1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
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.
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 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
1 Chapter 6 Object-Oriented Software Development.
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 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.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
1 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 9 Trees 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.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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 -
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
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.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
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.
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.
Queues.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
TA: Nouf Al-Harbi Data Structures LAB 3 TA: Nouf Al-Harbi
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
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
CSCS-200 Data Structure and Algorithms
C++ Plus Data Structures
Presentation transcript:

C++ Plus Data Structures Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus

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

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

What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element. ‘B’ ‘C’ ‘L’ ‘T’ ‘V’ ‘Y’ listData

What is a Doubly Linked List? A doubly linked list is a list in which each node is linked to both its successor and its predecessor. listData ‘A’ ‘C’ ‘F’ ‘T’ ‘Z’

Each node contains two pointers template< class ItemType > struct NodeType { ItemType info; // Data member NodeType<ItemType>* back; // Pointer to predecessor NodeType<ItemType>* next; // Pointer to successor }; 3000 ‘A’ NULL . back . info . next

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

Recall Definition of Stack 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. A stack is a LIFO “last in, first out” structure.

Stack ADT Operations MakeEmpty -- Sets stack to an empty state. IsEmpty -- Determines whether the stack is currently empty. IsFull -- Determines whether the stack is currently full. Push (ItemType newItem) -- Adds newItem to the top of the stack. Pop (ItemType& item) -- Removes the item at the top of the stack and returns it in item. 9

class StackType<int> MakeEmpty Pop Push IsFull IsEmpty Private data: topPtr ~StackType 20 30

What happens . . . When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push IsFull IsEmpty Private data: topPtr ~StackType 20 30

Passing a class object by value // FUNCTION CODE template<class ItemType> void MyFunction( StackType<ItemType> SomeStack ) // Uses pass by value { . } 12

Pass by value makes a shallow copy StackType<int> MyStack; // CLIENT CODE . MyFunction( MyStack ); // function call MyStack SomeStack Private data: 7000 6000 topPtr 7000 Private data: topPtr 7000 20 30 shallow copy

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

What’s the difference? 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.

Making a deep copy MyStack topPtr 7000 20 30 SomeStack topPtr 5000 Private data: 7000 6000 topPtr 7000 20 30 SomeStack Private data: 5000 2000 topPtr 5000 20 30 deep copy

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

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

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

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

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

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

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

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

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

What about the assignment operator? The default method used for assignment of class objects makes a shallow copy. 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.

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

C++ Operator Overloading Guides All operators except these :: . sizeof ?: may be overloaded. At least one operand must be a class instance. You cannot change precedence, operator symbols, or number of operands. Overloading ++ and -- requires prefix form use by default, unless special mechanism is used. To overload these operators = ( ) [ ] member functions (not friend functions) must be used. An 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 operator+(myStack, yourStack) 29

Composition (containment) Composition (or containment) means that an internal data member of one class is defined to be an object of another class type. A FAMILIAR EXAMPLE . . .

ItemType Class Interface Diagram class ItemType ComparedTo Private data value Print Initialize

Sorted list contains an array of ItemType SortedType class MakeEmpty Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos IsFull LengthIs RetrieveItem InsertItem DeleteItem ResetList GetNextItem

Inheritance Inheritance is a means by which one class acquires the properties--both data and operations--of another class. When this occurs, the class being inherited from is called the Base Class. The class that inherits is called the Derived Class. AN EXAMPLE . . .

Recall Definition of Queue Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). A queue is a FIFO “first in, first out” structure.

Queue ADT Operations MakeEmpty -- Sets queue to an empty state. IsEmpty -- Determines whether the queue is currently empty. IsFull -- Determines whether the queue is currently full. Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item. 35

class QueType<char> Private Data: qFront qRear ‘C’ ‘Z’ ‘T’ ~QueType Enqueue Dequeue .

// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE #include "ItemType.h" // for ItemType template<class ItemType> class QueType { public: QueType( ); // CONSTRUCTOR ~QueType( ) ; // DESTRUCTOR bool IsEmpty( ) const; bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); void MakeEmpty( ); private: NodeType<ItemType>* qFront; NodeType<ItemType>* qRear; }; 37

SAYS ALL PUBLIC MEMBERS OF QueType CAN BE INVOKED FOR OBJECTS OF TYPE CountedQue // DERIVED CLASS CountedQue FROM BASE CLASS QueType template<class ItemType> class CountedQue : public QueType<ItemType> { public: CountedQue( ); void Enqueue( ItemType newItem ); void Dequeue( ItemType& item ); int LengthIs( ) const; // Returns number of items on the counted queue. private: int length; }; 38

class CountedQue<char> QueType ~QueType Enqueue Dequeue . Private Data: qFront qRear ‘C’ ‘Z’ ‘T’ CountedQue LengthIs length 3

// Member function definitions for class CountedQue template<class ItemType> CountedQue<ItemType>::CountedQue( ) : QueType<ItemType>( ) { length = 0 ; } int CountedQue<ItemType>::LengthIs( ) const return length ; 40

template<class ItemType> void CountedQue<ItemType>::Enqueue( ItemType newItem ) // Adds newItem to the rear of the queue. // Increments length. { length++; QueType<ItemType>::Enqueue( newItem ); } void CountedQue<ItemType>::Dequeue(ItemType& item ) // Removes item from the rear of the queue. // Decrements length. length--; QueType<ItemType>::Dequeue( item ); 41