Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CHP-5 LinkedList.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Data Structures Outline 15.1Introduction 15.2Self-Referential Classes 15.3Dynamic Memory.
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.
Summary of lectures (1 to 11)
 2006 Pearson Education, Inc. All rights reserved Data Structures.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Helpful C++ Transitions
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
 2003 Prentice Hall, Inc. All rights reserved Linked Lists Upcoming program has two class templates –Create two class templates –ListNode data.
Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
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.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Data Structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Introduction to Data Structures Systems Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
 2008 Pearson Education, Inc. All rights reserved. 1 Member data stores a value of type parameter NODETYPE Member nextPtr stores a pointer to the next.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 19 – Data Structures Outline 19.1 Introduction 19.2 Self-Referential Classes 19.3 Dynamic Memory.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Course Review FINAL.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Introduction to Data Structures Systems Programming Concepts.
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.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Programming Practice 3 - Dynamic Data Structure
Copyright © 2002 Pearson Education, Inc. Slide 1.
Final Exam –Date: Aug 27 th –Time: 9:00am – 12:00pm –Location: TEL 0014.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Data Structures - Prabir Sarkar. AGENDA Stack Queue Linked List Trees Graphs Searching and Sorting Algorithm.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Chapter 20 Custom Templatized Data Structures
Cpt S 122 – Data Structures Abstract Data Types
Pointers and Linked Lists
Data Structure By Amee Trivedi.
17 Data Structures.
Chapter 12 – Data Structures
Introduction to Computers Computer Generations
5.13 Recursion Recursive functions Functions that call themselves
12 C Data Structures.
12 C Data Structures.
Chapter 22 Custom Generic Data Structures
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists.
Chapter 19 – Data Structures
Review & Lab assignments
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
21 Data Structures.
Presentation transcript:

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures in C++

Topics Introduction Self Referential Classes Dynamic Memory Allocation and Data Structures Linked List  insert, delete, isEmpty, printList Stacks  push, pop Queues  enqueue, dequeue Trees  insertNode, inOrder, preOrder, postOrder

Fixed-size data structures such as one-dimensional arrays and two- dimensional arrays. Dynamic data structures that grow and shrink during execution. Linked lists are collections of data items logically “lined up in a row”  insertions and removals are made anywhere in a linked list. Stacks are important in compilers and operating systems:  Insertions and removals are made only at one end of a stack—its top. Queues represent waiting lines;  insertions are made at the back (also referred to as the tail) of a queue  removals are made from the front (also referred to as the head) of a queue. Binary trees facilitate high-speed searching and sorting of data, efficient elimination of duplicate data items,  representation of file-system directories  compilation of expressions into machine language. Introduction

Classes, class templates, inheritance and composition is used to create and package these data structures for reusability and maintainability. Standard Template Library (STL)  The STL is a major portion of the C++ Standard Library.  The STL provides containers, iterators for traversing those containers algorithms for processing the containers’ elements.  The STL packages data structures into templatized classes.  The STL code is carefully written to be portable, efficient and extensible. Introduction (cont.)

A self-referential class contains a pointer member that points to a class object of the same class type. Self-Referential Classes

A self-referential class contains a pointer member that points to a class object of the same class type. Sample Node class definition: class Node { public: Node( int ); // constructor void setData( int ); // set data member int getData() const; // get data member void setNextPtr( Node * ); // set pointer to next Node Node *getNextPtr() const; // get pointer to next Node private: int data; // data stored in this Node Node *nextPtr; // pointer to another object of same type }; // end class Node Self-Referential Classes

Member nextPtr points to an object of type Node  another object of the same type as the one being declared here, hence the term “self-referential class.” Member nextPtr is referred to as a link  nextPtr can “tie” an object of type Node to another object of the same type. Self-referential class objects can be linked together to form useful data structures such as lists, queues, stacks and trees.  Two self-referential class objects linked together to form a list.  A null ( 0 ) pointer is placed in the link member of the second self-referential class object to indicate that the link does not point to another object.  A null pointer normally indicates the end of a data structure just as the null character ( '\0' ) indicates the end of a string. Self-Referential Classes (cont.)

The new operator takes as an argument  the type of the object being dynamically allocated  returns a pointer to an object of that type. For example, the following statement  allocates sizeof( Node ) bytes,  runs the Node constructor and assigns the new Node ’s address to newPtr. // create Node with data 10 Node *newPtr = new Node( 10 ); If no memory is available, new throws a bad_alloc exception. The delete operator runs the Node destructor and deallocates memory allocated with new  the memory is returned to the system so that the memory can be reallocated in the future. Dynamic Memory Allocation and Data Structures

If nodes contain base-class pointers to base-class and derived-class objects related by inheritance,  we can have a linked list of such nodes and process them polymorphically using virtual function calls. Stacks and queues are linear data structures  can be viewed as constrained versions of linked lists. Trees are nonlinear data structures. Linked Lists (cont.)

A linked list is appropriate when the number of data elements to be represented at one time is unpredictable. Linked lists are dynamic, so the length of a list can increase or decrease as necessary. Linked lists can be maintained in sorted order  By inserting each new element at the proper point in the list.  Existing list elements do not need to be moved.  Pointers merely need to be updated to point to the correct node. Linked Lists Performance

Insertion & deletion in sorted array is time consuming  All the elements following the inserted and deleted elements must be shifted appropriately. Linked list allows efficient insertion operations anywhere in the list Linked-list nodes are not stored contiguously in memory, but logically they appear to be contiguous. Linked Lists Performance (cont.)

The program uses a List class template  manipulate a list of integer values and a list of floating-point values. The program uses class templates  ListNode and List. Encapsulated in each List object is a linked list of ListNode objects. Linked Lists (cont.)

Class template ListNode contains  private members data and nextPtr  a constructor to initialize these members and  function getData to return the data in a node. Member data stores a value of type NODETYPE  the type parameter passed to the class template. Member nextPtr stores a pointer to the next ListNode object in the linked list. ListNode Class Template

ListNode class template definition declares class List as a friend. This makes all member functions of a given specialization of class template List friends of the corresponding specialization of class template ListNode,  so they can access the private members of ListNode objects of that type. ListNode template parameter NODETYPE is used as the template argument for List in the friend declaration,  ListNode specialized with a particular type can be processed only by a List specialized with the same type a List of int values manages ListNode objects that store int values. Linked Lists (cont.)

ListNode Template Class

ListNode Member Function

List Class Template

List class template declare private data members  firstPtr (a pointer to the first ListNode in a List )  lastPtr (a pointer to the last ListNode in a List ). The default constructor initializes both pointers to 0 (null). The destructor ensures that all ListNode objects in a List object are destroyed when that List object is destroyed. List (cont.)

The primary List functions are  insertAtFront,  insertAtBack,  removeFromFront and  removeFromBack. Function isEmpty is called a predicate function Function print displays the List ’s contents. Utility function getNewNode returns a dynamically allocated ListNode object.  Called from functions insertAtFront and insertAtBack. List (cont.)

List Class Constructor

List Class Destructor

insertAtFront()

insertAtBack()

removeFromFront()

removeFromBack()

isEmpty()

print()

Create List objects for types int and double, respectively. Invoke the testList function template to manipulate objects. List (cont.)

List

Singly linked list  begins with a pointer to the first node  each node contains a pointer to the next node “in sequence.” This list terminates with a node whose pointer member has the value 0. A singly linked list may be traversed in only one direction. A circular singly linked list begins with a pointer to the first node  each node contains a pointer to the next node. The “last node” does not contain a 0 pointer  the pointer in the last node points back to the first node, thus closing the “circle.” Linked Lists (cont.)

Circular Singly Linked List

A doubly linked list allows traversals both forward and backward. Implemented with two “start pointers”  one that points to the first element of the list to allow front-to-back traversal of the list  one that points to the last element to allow back-to-front traversal. Each node has both  forward pointer to the next node in the list in the forward direction  backward pointer to the next node in the list in the backward direction List contains an alphabetized telephone directory  a search for someone whose name begins with a letter near the front of the alphabet might begin from the front of the list.  Searching for someone whose name begins with a letter near the end of the alphabet might begin from the back of the list. Doubly Linked List

Circular doubly linked list  forward pointer of the last node points to the first node  backward pointer of the first node points to the last node, thus closing the “circle.” Circular Doubly Linked List