1 Chapter 16 Creating the Document and Improving the View.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
DATA STRUCTURES USING C++ Chapter 5
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
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.
Assembler – Assembler Design Options. One-Pass Assemblers (1/2) Main problem  Forward references Data items Labels on instructions Solution  Data items:
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Programming Logic and Design Fourth Edition, Comprehensive
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
1 Chapter 15 Drawing in a Window. 2 The Window Client Area  A coordinate system that is local to the window.  It always uses the upper-left corner of.
Data Structures Using C++ 2E
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
MFC C OLLECTION C LASSES Tr ầ n Anh Tu ấ n A. C ONTENTS ( MFC C OLLECTION C LASSES ) Array Introduction Example List Introduction Example Map Introduction.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
111 © 2002, Cisco Systems, Inc. All rights reserved.
Linked Lists Tonga Institute of Higher Education.
1 5. Abstract Data Structures & Algorithms 5.2 Static Data Structures.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
Ordered Lists and Sorted Lists Chapter 7. Ordered Lists Array version vs. Linked-list version.
Data Structures Using C++1 Chapter 5 Linked Lists.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Chapter 5 The MFC Collection Classes. How to store many data? 1. Use an Array 2. Use a Linked List value: 10 Node * p a a value: 20 Node * p value: 30.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
LINKED LISTS.
Chapter 16 (cont.) Drawing in a Window Using the Mouse
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Review Deleting an Element from a Linked List Deletion involves:
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 5 The MFC Collection Classes
Object Oriented Programming in java
Defining Classes and Methods
Data Structures & Algorithms
STL List.
Chapter 17 Creating the Document and Improving the View
The List Container and Iterators
MFC Collection Classes
STL List.
Presentation transcript:

1 Chapter 16 Creating the Document and Improving the View

2 Line vs. Curve

3 Figure 16-6 (P.926)

4 Collection Classes  MFC provides you with a large number of collection classes for managing data. They are useful especially when you have no advance knowledge of how many items you will need to manage.  MFC supports three kinds of collections (three shapes), differentiated by the way in which the data items are organized. Array List Map

5 Array  Elements in array collections are indexed from 0.  Template class: CArray CArray PointArray;  To avoid the overhead in copying objects when passed by value, the second argument is usually a reference.

6 The CArray Template Class (1)  An array collection can automatically grow to accommodate more data items.

7 Figure 16-1 (P.912) Choose a proper initial value aPoint = PointArray.GetAt(2); PointArray.SetAt(2, NewPoint);

8 The CList Template Class  A doubly linked list It has backward and forward pointing links. It can be searched in either direction. It grows automatically when required.  It is fast in adding items, compared with CArray.  If there are lots of data items in the list, it can be slow in searching for an item.  CList aList; CList PointList;

9 Adding Elements to a List  Both the AddHead() and AddTail() functions return a value of type POSITION, which specifies the position of the inserted object in the list.

10 Retrieving Elements in a List  GetAt()  GetNext()  GetPrev()

11 Iterating through a List  GetHeadPosition() & GetTailPosition()  GetNext() & GetPrev() The position variable will become NULL if you use GetNext() to retrieve the last object. CPoint CurrentPoint(0,0); // Get the position of the first list element POSITION aPosition = PointList.GetHeadPosition(); while (aPosition) // Loop while aPosition is not NULL { CurrentPoint = PointList.GetNext(aPosition); // Process the current object }

12 Modifying a List  InsertBefore() PointList.InsertBefore(aPosition, ThePoint);  InsertAfter()  SetAt() PointList.SetAt(aPosition, aPoint);

13 Searching a List  Find() POSITION aPosition = PointList.Find(ThePoint);  By default, this function only compares the address of the argument with the address of each object in the list.  This implies that if the search is to be successful, the argument must actually be an element in the list – not a copy.  FindIndex() You can also obtain the position of an element in a list by using an index value.  The first element is at index 0, the second at index 1, and so on.  GetCount() Return how many objects are there in a list.

14 Removing Objects from a List  RemoveHead() if(!PointList.IsEmpty()) PointList.RemoveHead();  RemoveAt() PointList.RemoveAt(aPosition);  RemoveAll() PointList.RemoveAll();

15 The CMap Template Class  A map stores an object and key combination. This technique is sometimes called hashing. A key is used to retrieve the item from the map, so it should be unique.  It is fast in storing data items and also in searching, because a key takes you directly to the item.  For sequential access, arrays or lists are faster.  Four arguments are needed to declare a map: CMap PointMap;

16 Retrieving Items in a Map aMap.SetAt(Key2, Object2);

17 Using the CList Template Class  A curve is defined by two or more points. Storing these points in a list would be a natural solution.

18 Define a CList collection class object as a member of the CCurve class // Elements.h class CCurve :public CElement { protected: CCurve(void); CList m_PointList; // Type safe point list public: ~CCurve(void); virtual void Draw(CDC* pDC); // Fucntion to display a curve // Constructor for a curve object CCurve(CPoint FirstPoint, CPoint SecondPoint, COLORREF aColor); void AddSegment(CPoint& aPoint); // Add a segment to the curve };

19 CSketcherView.cpp  Modify the definition of the CreateElement() function to call the CCurve class constructor with correct arguments. case CURVE: return new CCurve(m_FirstPoint, m_SecondPoint, pDoc->GetElementColor());

20 OnMouseMove()  P.926 (compare with P.889) if (CURVE == GetDocument()->GetElementType()) // Is it a curve? { static_cast (m_pTempElement)-> AddSegment(m_SecondPoint); m_pTempElement->Draw(&aDC); return; } aDC.SetROP2(R2_NOTXORPEN); // Set the drawing mode  Move the call to SetROP2() to a position after the code processing a curve.

21 CCurve Constructor CCurve::CCurve(CPoint FirstPoint, CPoint SecondPoint, COLORREF aColor) { m_PointList.AddTail(FirstPoint); m_PointList.AddTail(SecondPoint); m_Color = aColor; m_Pen = 1; m_EnclosingRect = CRect(FirstPoint, SecondPoint); }

22 Enclosing Rectangle

23 AddSegment() void CCurve::AddSegment(CPoint& aPoint) { m_PointList.AddTail(aPoint); m_EnclosingRect = CRect(min(aPoint.x, m_EnclosingRect.left), min(aPoint.y, m_EnclosingRect.top), max(aPoint.x, m_EnclosingRect.right), max(aPoint.y, m_EnclosingRect.bottom)); }

24 Draw()  P.928 POSITION aPosition = m_PointList.GetHeadPosition(); if (aPosition) pDC->MoveTo( m_PointList.GetNext(aPosition)); while(aPosition) pDC->LineTo( m_PointList.GetNext(aPosition));

25 Exercising the CCurve Class