Nell Dale - PowerPoint PPT Presentation

About This Presentation
Title:

Nell Dale

Description:

No Slide Title – PowerPoint PPT presentation

Number of Views:472
Avg rating:3.0/5.0
Slides: 56
Provided by: Sylvi118
Category:
Tags: dale | nell

less

Transcript and Presenter's Notes

Title: Nell Dale


1
C Plus Data Structures
Nell Dale Chapter 6 Lists Plus Slides by
Sylvia Sorkin, Community College of Baltimore
County - Essex Campus
2
ADT Sorted List Operations
  • Transformers
  • MakeEmpty
  • InsertItem
  • DeleteItem
  • Observers
  • IsFull
  • LengthIs
  • RetrieveItem
  • Iterators
  • ResetList
  • GetNextItem

change state observe state process all
3

class SortedTypeltchargt
SortedType
Private data length
3 listData currentPos
MakeEmpty
SortedType
RetrieveItem
InsertItem
DeleteItem . . .
GetNextItem
4
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.

5
Circularly Linked List
  • We can keep track of the list by just using one
    pointer, the pointer pointing to the last element
    of the list
  • In this way, we can add at the back (queue rules)
    using the rear pointer directly instead of
    writing a loop until you hit the NULL pointer
  • We can also reach the first element in one
    step(How?)
  • What else is different?

6
Circularly Linked List
  • RetrieveItem,InsertItem,DeleteItem all require
    search. We can write a general search function to
    help all of these functions
  • FindItem accepts item and returns location,
    predLoc and found
  • What if we want to go both ways?

7
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.

A C F
T Z
listData
8
Each node contains two pointers
templatelt class ItemType gt struct NodeType
ItemType info // Data member
NodeTypeltItemTypegt back // Pointer to
predecessor NodeTypeltItemTypegt next //
Pointer to successor
3000 A NULL
. back . info . next
9
Doubly Linked List
  • The doubly linked list is a linear list. Its
    first node has the back pointer set to NULL and
    its last node has the next pointer as NULL
  • We can traverse this list in both directions.
    Thus we are able to print lowest to highest as
    well as highest to lowest members
  • Doubly linked list is more difficult to maintain
    because it has more pointers

10
Doubly Linked List
  • For example, consider the doubly linked list
    shown and determine all the pointers that will be
    used and/or moved if we delete C

A C F
T Z
listData
11
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
12
Why Header and Trailer Nodes?
  • So that we never delete or add things before the
    starting node or after the last node
  • Thus we get rid of the special cases that we had
    to write
  • A list of names can have a start node AAAAAAAA
    and last node as ZZZZZZZZ

13
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.

14
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.

14
15

class StackTypeltintgt
16
What happens . . .
  • When a function is called that uses pass by
    value for a class object like our dynamically
    linked stack?

17
Passing a class object by value
  • // FUNCTION CODE
  • templateltclass ItemTypegt
  • void MyFunction( StackTypeltItemTypegt SomeStack )
  • // Uses pass by value
  • .
  • .
  • .
  • .

17
18
Pass by value makes a shallow copy
StackTypeltintgt MyStack // CLIENT
CODE . . . MyFunction(
MyStack ) // function call
MyStack
SomeStack
Private data topPtr 7000
Private data 7000
6000 topPtr 7000
20 30
shallow copy
19
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.

20
Whats 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.

21
Making a deep copy
MyStack
Private data 7000
6000 topPtr 7000
20 30
SomeStack
Private data 5000
2000 topPtr 5000
20 30
deep copy
22
Suppose MyFunction Uses Pop
  • // FUNCTION CODE
  • templateltclass ItemTypegt
  • void MyFunction( StackTypeltItemTypegt SomeStack )
  • // Uses pass by value
  • ItemType item
  • SomeStack.Pop(item)
  • .
  • .
  • .
  • WHAT HAPPENS IN THE SHALLOW COPY SCENARIO?

22
23
MyStack.topPtr is left dangling
StackTypeltintgt MyStack // CLIENT CODE
. . . MyFunction( MyStack )
MyStack
SomeStack
Private data topPtr 6000
Private data 7000
6000 topPtr 7000
? 30
shallow copy
24
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 topPtr 6000
Private data 7000
6000 topPtr 7000
? 30
shallow copy
25
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.

26
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.

27
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.
  • It does not run when you assign an object to
    another

28
  • // DYNAMICALLY LINKED IMPLEMENTATION OF STACK
  • class StackType
  • public
  • StackType( )
  • // Default constructor.
  • // POST Stack is created and empty.
  • StackType( const StackType anotherStack )
  • // Copy constructor.
  • // Implicitly called for pass by value.
  • .
  • .
  • .
  • StackType( )
  • // Destructor.
  • // POST Memory for nodes has been deallocated.
  • private
  • NodeType topPtr

28
29
Classes with Data Member Pointers Need
  • CLASS CONSTRUCTOR
  • CLASS COPY CONSTRUCTOR
  • CLASS DESTRUCTOR

30
  • // COPY CONSTRUCTOR, Copying from
    anotherstack
  • StackType
  • StackType( const StackType anotherStack )
  • NodeType ptr1
  • NodeType ptr2
  • if ( anotherStack.topPtr NULL )
  • topPtr NULL
  • else // allocate memory for first node
  • topPtr new NodeType
  • topPtr-gtinfo anotherStack.topPtr-gtinfo
  • ptr1 anotherStack.topPtr-gtnext
  • ptr2 topPtr
  • while ( ptr1 ! NULL ) // deep copy other nodes
  • ptr2-gtnext new NodeType
  • ptr2 ptr2-gtnext
  • ptr2-gtinfo ptr1-gtinfo
  • ptr1 ptr1-gtnext
  • ptr2-gtnext NULL

30
31
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.
  • Thus ab will make a deep copy

32
Overloading Operators
  • C allows us to redefine the meaning of the
    relational operators in terms of the data members
    of a class
  • This redefinition is called overloading an
    operator
  • A simple example was covered in chapter 3

33
Overloading
  • class StrType
  • public
  • bool operator (StrType otherstring) const

34
Overloaded operator
  • bool StrTypeoperator (strType otherstring)
    const
  • int result
  • result stdstrcmp(letters, otherstring.letters)
  • if (result 0)
  • return true
  • else
  • return false

35
Overloading an Operator
  • How to use an overloaded operator?
  • Overloaded operators can only be used with data
    items that belong to user defined classes
  • StrType a
  • StrType b
  • if (a b) coutltltYes indeedltltendl
  • else coutltltSorry! They are not equalltltendl

36
Another Example
  • //Overloading syntax example modified from
    original program at http//www.codeguru.com/cpp/ti
    c/tic0127.shtml
  • include ltiostreamgt
  • using namespace std
  • class Integer
  • int i
  • public
  • Integer(int ii)
  • i ii
  • const Integer
  • operator(const Integer rv) const
  • cout ltlt "operator" ltlt endl
  • return Integer(i rv.i)

37
  • Integer
  • operator(const Integer rv)
  • cout ltlt "operator" ltlt endl
  • i rv.i
  • return this
  • void display()
  • coutltltiltltendl

38
Client Code
  • void main()
  • cout ltlt "built-in types" ltlt endl
  • int i 1, j 2, k 3
  • k i j
  • coutltlt"k is now "ltltkltltendl
  • cout ltlt "user-defined types" ltlt endl
  • Integer I(1), J(2), K(3)
  • K I J
  • coutltlt"K is now "ltltendl
  • K.display()
  • ///

39
  • // DYNAMICALLY LINKED IMPLEMENTATION OF STACK
  • class StackType
  • public
  • StackType( )
  • // Default constructor.
  • StackType( const StackType anotherStack )
  • // Copy constructor.
  • void operator ( StackType )
  • // Overloads assignment operator.
  • .
  • .
  • .
  • StackType( )
  • // Destructor.
  • private
  • NodeType topPtr

39
40
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.

41
Using Overloaded Binary operator
  • When a Member Function was defined
  • myStack yourStack
  • myStack.operator(yourStack)
  • When a Friend Function was defined
  • myStack yourStack
  • operator(myStack, yourStack)

41
42
Composition and Inheritance
  • Classes are related in three ways
  • 1) Independent of one another
  • 2) One class is contained within another class
    (e.g. the array of ItemType class that was used
    to build stack ADT)
  • 3) One class may acquire the properties of
    another class. (A Derived class inherits
    properties of the base class)

43
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 . . .
44
ItemType Class Interface Diagram
class ItemType
ComparedTo
Private data value
Print
Initialize
45

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
46
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 . . .
47
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.

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

48
49
class QueType
QueType
Private Data qFront qRear
QueType
Enqueue
Dequeue . . .
50
  • // DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE
  • class QueType
  • public
  • QueType( ) // CONSTRUCTOR
  • QueType( ) // DESTRUCTOR
  • bool IsEmpty( ) const
  • bool IsFull( ) const
  • void Enqueue( char item )
  • void Dequeue( char item )
  • void MakeEmpty( )
  • private
  • NodeType qFront
  • NodeType qRear

50
51
Inheritance
  • Now let us define a new class CountedQueue that
    inherits all data and properties of the Queue ADT
  • This new class adds enumeration to the queue by
    adding a new data member length and redefines
    enqueue and dequeue functions so that they can
    also increment and decrement length of the queue

52
SAYS ALL PUBLIC MEMBERS OF QueType CAN BE INVOKED
FOR OBJECTS OF TYPE CountedQue
  • // DERIVED CLASS CountedQue FROM BASE CLASS
    QueType
  • class CountedQue public QueType
  • public
  • CountedQue( )
  • void Enqueue( char newItem )
  • void Dequeue( char item )
  • int LengthIs( ) const
  • // Returns number of items on the counted queue.
  • private
  • int length

52
53
class CountedQue
54
  • // Member function definitions for class
    CountedQue
  • CountedQueCountedQue( ) QueType( )
  • length 0
  • int CountedQueLengthIs( ) const
  • return length

54
55
  • void CountedQueEnqueue( char newItem )
  • // Adds newItem to the rear of the queue.
  • // Increments length.
  • length
  • QueTypeEnqueue( newItem )
  • void CountedQueDequeue(char item )
  • // Removes item from the rear of the queue.
  • // Decrements length.
  • length--
  • QueTypeDequeue( item )

55
Write a Comment
User Comments (0)
About PowerShow.com