Slide Set 17 for ENCM 339 Fall 2015

Slide Set 17 for ENCM 339 Fall 2015

Slide Set 17 for ENCM 339 Fall 2015 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2015 SN's ENCM 339 Fall 2015 Slide Set 17 slide 2/46 Contents Data structures Abstract data types Linked data structures A singly-linked list type in C++ Operations and implementation of SLListV1 The Big 3 for a linked-list class Ordered linked lists SN's ENCM 339 Fall 2015 Slide Set 17 slide 3/46 Outline of Slide Set 17 Data structures Abstract data types Linked data structures A singly-linked list type in C++ Operations and implementation of SLListV1 The Big 3 for a linked-list class Ordered linked lists SN's ENCM 339 Fall 2015 Slide Set 17 slide 4/46 Data structures In discussion of programming, the term data structure means a collection of data items, organized to support some of the following goals: I fast insertion or removal of data items I fast access to data items I fast searching for the location of a particular data item, if the item is in the collection I maintaining items in some kind of sorted order I keeping the memory footprint of the collection as small as possible There are design trade-offs|no single data structure design is best for all of the above goals. SN's ENCM 339 Fall 2015 Slide Set 17 slide 5/46 Here are two kinds of data structures that everybody in this course should be familiar with: I arrays I C and C++ structure objects We're about to study a kind of data structure called a linked list. Students in the Software Engineering major or Computer Engineering minor will study many other data structures in CPSC 319. Note that data structure is not a synonym for structure object. A structure object is just one of the many kinds of data structures. SN's ENCM 339 Fall 2015 Slide Set 17 slide 6/46 Outline of Slide Set 17 Data structures Abstract data types Linked data structures A singly-linked list type in C++ Operations and implementation of SLListV1 The Big 3 for a linked-list class Ordered linked lists SN's ENCM 339 Fall 2015 Slide Set 17 slide 7/46 Abstract data types An abstract data type (ADT) can be thought of as a description of a type I with very precise specification of all of the operations available with objects of the given type; I with very little or no specification of how the operations are implemented|no detail about all the numbers, addresses, if-statements, loops, and so on, that will be used to make the type work. SN's ENCM 339 Fall 2015 Slide Set 17 slide 8/46 Example ADT: C++ vector Suppose that you use a vector<double> in a C++ program, with particular C++ compiler and a particular C++ library. Then vector<double> is really a specific, \concrete" type|all of the implementation details can be found in the (surprisingly complicated!) library source code. However, descriptions of vector types in textbooks and online documentation tend to treat vector as an ADT, and focus on describing the operations available with vector objects. SN's ENCM 339 Fall 2015 Slide Set 17 slide 9/46 A few of the most important vector operations are I initialization of an empty vector I initialization of a vector to have some non-zero number of elements, with initial element values specified one way or another I access to an individual element using an index I resizing a vector|making the number of elements smaller or larger Of course, the abstraction is not perfect. To use a vector object safely and effectively, it is helpful to know that its elements are all stored in a single array on the heap. SN's ENCM 339 Fall 2015 Slide Set 17 slide 10/46 Data structures, ADTs, and C++ classes An ADT is a design idea. When that idea is translated into code for a C++ class, development often follows this pattern: I The operations of the ADT are translated into prototypes for public member functions. I For each public member function, parameter types, the return value types and supporting documentation reflect the operation performed by that function. I One or more data structures are chosen for the implementation of the class. I The choice of data structure(s) leads to choices for private members and choices for algorithms used in member function definitions. SN's ENCM 339 Fall 2015 Slide Set 17 slide 11/46 Outline of Slide Set 17 Data structures Abstract data types Linked data structures A singly-linked list type in C++ Operations and implementation of SLListV1 The Big 3 for a linked-list class Ordered linked lists SN's ENCM 339 Fall 2015 Slide Set 17 slide 12/46 Linked data structures A linked data structure stores data items in a collection of nodes. A node is a chunk of memory that contains I some data I information about where zero, one, or two or more other nodes are located Typically in C and C++, I a node is a structure object allocated in the heap I information about locations of other nodes is stored in pointers (Sometimes class types are used instead of struct types for node types in C++.) SN's ENCM 339 Fall 2015 Slide Set 17 slide 13/46 Examples of linked data structures The next several slides show a few different kinds of linked data structures. The data in each node in all of the examples is a single integer. That's for simplicity|storing a collection of integers is not the most practical application of linked data structures! You can assume that all of the nodes are in the heap. I am being deliberately vague about where and how the pointer variables root, head, tail, and dummy are allocated. I'll be more precise about that later. SN's ENCM 339 Fall 2015 Slide Set 17 slide 14/46 Binary search tree An example of a binary search tree is shown on the next slide. Binary search trees (BSTs) are perhaps both the most beautiful and most useful kind of linked data structure. For the BST shown on the slide, what kind of code would work to set up a node type in C? In C++? Variations of BSTs are used, for example, to support the C++ library set, multiset, map and multimap container types. Unfortunately, some BST programming is difficult (for example, removal of a node from a BST is surprisingly hard to to do properly), so we won't look at the details of BSTs in ENCM 339. SN's ENCM 339 Fall 2015 Slide Set 17 slide 15/46 root data 60 left right data 35 data 80 left right left right data 10 data 50 data 90 left right left right left right SN's ENCM 339 Fall 2015 Slide Set 17 slide 16/46 Singly-linked list with head and tail pointers head data data data data 200 100 400 300 next next next next tail For this kind of list, what kind of code would work to set up a node type in C? In C++? We'll soon see that traversal|visiting all of the nodes|is easy going head-to-tail, but awkward and slow going tail-to-head. SN's ENCM 339 Fall 2015 Slide Set 17 slide 17/46 Singly-linked list with only a head pointer For some uses of singly-linked lists, maintaining a tail pointer is unnecessary work . head data data data data 200 100 400 300 next next next next SN's ENCM 339 Fall 2015 Slide Set 17 slide 18/46 Circular doubly-linked list with a \dummy" node data data data data X 66 33 99 dummy next next next next prev prev prev prev X is borrowed from ENEL 353 and means \don't-care". The actual data items, are, first-to-last, 66, 33, 99. Last-to-first, they're 99, 33, 66. SN's ENCM 339 Fall 2015 Slide Set 17 slide 19/46 Circular doubly-linked lists . Something like the list on the previous slide is what you get with list types in the standard C++ library. Traversals from last-to-first and first-to-last are equally easy with doubly-linked lists. We will not study doubly-linked lists in detail in ENCM 339, and they will not appear on the final exam. SN's ENCM 339 Fall 2015 Slide Set 17 slide 20/46 Outline of Slide Set 17 Data structures Abstract data types Linked data structures A singly-linked list type in C++ Operations and implementation of SLListV1 The Big 3 for a linked-list class Ordered linked lists SN's ENCM 339 Fall 2015 Slide Set 17 slide 21/46 A singly-linked list type in C++ Let's look at an as-simple-as-possible type, with a few of the features of forward_list types available in the C++11 standard library. We'll call the type SLListV1, short for singly-linked list, Version 1. Below is the node type. The typedef allows us to make a quick change to the type of data item stored in a node. typedef int ItemType; struct Node { ItemType item; Node *next; }; SN's ENCM 339 Fall 2015 Slide Set 17 slide 22/46 Here is a sketch of the class definition: class SLListV1 f public: member function prototypes private: Node *headM; // points to first node Node *tailM; // points to last node g; For a linked data structure, before we start to think about what operations are supported, it's a good idea to consider .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    46 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us