Slide Set 17 for ENCM 339 Fall 2014 Lecture Section 01
Total Page:16
File Type:pdf, Size:1020Kb
Slide Set 17 for ENCM 339 Fall 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2014 ENCM 339 F14 Section 01 Slide Set 17 slide 2/22 Contents Data structures Abstract data types Linked data structures A singly-linked list type in C++ ENCM 339 F14 Section 01 Slide Set 17 slide 3/22 Outline of Slide Set 17 Data structures Abstract data types Linked data structures A singly-linked list type in C++ ENCM 339 F14 Section 01 Slide Set 17 slide 4/22 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 tradeoffs|no single data structure design is best for all of the above goals. ENCM 339 F14 Section 01 Slide Set 17 slide 5/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 6/22 Outline of Slide Set 17 Data structures Abstract data types Linked data structures A singly-linked list type in C++ ENCM 339 F14 Section 01 Slide Set 17 slide 7/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 8/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 9/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 10/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 11/22 Outline of Slide Set 17 Data structures Abstract data types Linked data structures A singly-linked list type in C++ ENCM 339 F14 Section 01 Slide Set 17 slide 12/22 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++.) ENCM 339 F14 Section 01 Slide Set 17 slide 13/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 14/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 15/22 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 ENCM 339 F14 Section 01 Slide Set 17 slide 16/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 17/22 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 ENCM 339 F14 Section 01 Slide Set 17 slide 18/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 19/22 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. ENCM 339 F14 Section 01 Slide Set 17 slide 20/22 Outline of Slide Set 17 Data structures Abstract data types Linked data structures A singly-linked list type in C++ ENCM 339 F14 Section 01 Slide Set 17 slide 21/22 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; }; ENCM 339 F14 Section 01 Slide Set 17 slide 22/22 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 . I What does an empty data structure look like? I What does a data structure with one item look like? I What does a data structure with a few items look like? ENCM 339 F14 Section 01 Slide Set 17 slide 22/22 We'll complete the definitions of the SLListV1 class and its member functions in Slide Set 18..