CSI 1340 Introduction to Computer Science II - PowerPoint PPT Presentation

1 / 158
About This Presentation
Title:

CSI 1340 Introduction to Computer Science II

Description:

Popping a Node from a Stack. that Contains More than One Node ... Executes faster if pushing and popping occur frequently. Use an Array Implementation when. ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 159
Provided by: csBa4
Learn more at: http://cs.baylor.edu
Category:

less

Transcript and Presenter's Notes

Title: CSI 1340 Introduction to Computer Science II


1
CSI 1340Introduction to Computer Science II
  • Chapter 5
  • Linked Structures

2
Stack ImplementationAs A Linked Structure
3
  • struct NodeType
  • ItemType info
  • NodeType next
  • NodeType topPtr
  • NOTE topPtr is a pointer capable of pointing at
    NodeType nodes

info
next
50
topPtr
4
Linked Structure
(topPtr).next
topPtr-gtnext
50
topPtr
75
NULL
topPtr-gtinfo
(topPtr).info
5
  • include ItemType.h
  • struct NodeType
  • ItemType info
  • NodeType next
  • class StackType // Stack as a linked structure
  • public
  • . . .
  • private
  • NodeType topPtr

topPtr
6
  • . . .
  • class StackType // Stack implementation using a
  • // linked structure
  • public
  • StackType( )
  • void MakeEmpty( )
  • void Push(ItemType newItem)
  • void Pop(ItemType item)
  • bool IsEmpty( ) const
  • bool IsFull( ) const

7
NULL Pointer
  • A pointer that points to nothing.
  • topPtr NULL
  • Requires the header file called ltcstddefgt
  • include ltcstddefgt
  • Can be used to identify an empty stack.

8
  • include Stack.h
  • StackTypeStackType( ) // default constructor
  • topPtr NULL

NULL
topPtr
9
  • bool StackTypeIsEmpty( ) const
  • return (topPtr NULL)

NULL
topPtr
10
Pushing a Node onto anEmpty Stack
11
  • void StackTypePush(ItemType newItem)
  • NodeType location

location
12
  • void StackTypePush(ItemType newItem)
  • NodeType location
  • location new NodeType

location
info next

location
13
  • void StackTypePush(ItemType newItem)
  • NodeType location
  • location new NodeType
  • location-gtinfo newItem

location
info next

location
85
location
14
  • void StackTypePush(ItemType newItem)
  • NodeType location
  • location new NodeType
  • location-gtinfo newItem
  • location-gtnext topPtr

location
info next

location
location
85
85
location
NULL
NULL
topPtr
15
  • void StackTypePush(ItemType newItem)
  • NodeType location
  • location new NodeType
  • location-gtinfo newItem
  • location-gtnext topPtr
  • topPtr location

location
info next

location
location
85
85
location
NULL
topPtr
16
Stack After A Push Operation
topPtr
85
NULL
17
Pushing a Node onto a Stackthat Contains At
Least One Node
18
  • void StackTypePush(ItemType newItem)
  • NodeType location

location
19
  • void StackTypePush(ItemType newItem)
  • NodeType location
  • location new NodeType

location
info next

location
20
  • void StackTypePush(ItemType newItem)
  • NodeType location
  • location new NodeType
  • location-gtinfo newItem

location
info next

location
50
location
21
  • void StackTypePush(ItemType newItem)
  • NodeType location
  • location new NodeType
  • location-gtinfo newItem
  • location-gtnext topPtr

location
info next

location
50
location
50
location
85
topPtr
NULL
22
  • void StackTypePush(ItemType newItem)
  • NodeType location
  • location new NodeType
  • location-gtinfo newItem
  • location-gtnext topPtr
  • topPtr location

location
info next

location
50
location
50
location
85
topPtr
NULL
23
Stack After a Second Push
topPtr
85
NULL
50
24
Popping a Node from a Stackthat Contains More
than One Node
25
  • void StackTypePop(ItemType item)
  • NodeType tempPtr

tempPtr
26
  • void StackTypePop(ItemType item)
  • NodeType tempPtr
  • item topPtr-gtinfo

tempPtr
50
item
27
  • void StackTypePop(ItemType item)
  • NodeType tempPtr
  • item topPtr-gtinfo
  • tempPtr topPtr

tempPtr
50
item
tempPtr
topPtr
85
NULL
50
28
  • void StackTypePop(ItemType item)
  • NodeType tempPtr
  • item topPtr-gtinfo
  • tempPtr topPtr
  • topPtr topPtr-gt next

tempPtr
50
item
tempPtr
topPtr
85
NULL
50
29
  • void StackTypePop(ItemType item)
  • NodeType tempPtr
  • item topPtr-gtinfo
  • tempPtr topPtr
  • topPtr topPtr-gt next
  • delete tempPtr

tempPtr
50
item
tempPtr
topPtr
?
85
NULL
30
Stack After a Pop Operation
topPtr
85
NULL
31
Popping a Node from a Stackthat Contains Only
One Node
32
  • void StackTypePop(ItemType item)
  • NodeType tempPtr

tempPtr
33
  • void StackTypePop(ItemType item)
  • NodeType tempPtr
  • item topPtr-gtinfo

tempPtr
85
item
topPtr
85
NULL
34
  • void StackTypePop(ItemType item)
  • NodeType tempPtr
  • item topPtr-gtinfo
  • tempPtr topPtr

tempPtr
85
item
tempPtr
topPtr
85
NULL
35
  • void StackTypePop(ItemType item)
  • NodeType tempPtr
  • item topPtr-gtinfo
  • tempPtr topPtr
  • topPtr topPtr-gtnext

tempPtr
85
item
tempPtr
topPtr
NULL
85
NULL
36
  • void StackTypePop(ItemType item)
  • NodeType tempPtr
  • item topPtr-gtinfo
  • tempPtr topPtr
  • topPtr topPtr-gtnext
  • delete tempPtr

tempPtr
85
item
tempPtr
topPtr
?
NULL
37
Make Empty
  • Is there an easy way to do it?

38
  • void StackTypeMakeEmpty( )
  • ItemType dummy
  • while (!IsEmpty( ))
  • Pop(dummy)

39
  • StackTypeMakeEmpty( )
  • NodeType tempPtr

tempPtr
topPtr
85
NULL
50
40
  • StackTypeMakeEmpty( )
  • NodeType tempPtr
  • while (topPtr ! NULL)

tempPtr
topPtr
85
NULL
50
41
  • StackTypeMakeEmpty( )
  • NodeType tempPtr
  • while (topPtr ! NULL)
  • tempPtr topPtr

tempPtr
topPtr
85
NULL
50
42
  • StackTypeMakeEmpty( )
  • NodeType tempPtr
  • while (topPtr ! NULL)
  • tempPtr topPtr
  • topPtr topPtr-gtnext

tempPtr
topPtr
85
NULL
50
43
  • StackTypeMakeEmpty( )
  • NodeType tempPtr
  • while (topPtr ! NULL)
  • tempPtr topPtr
  • topPtr topPtr-gtnext
  • delete tempPtr

tempPtr
topPtr
?
85
NULL
44
Stack After Deleting First Node
topPtr
85
NULL
45
  • StackTypeMakeEmpty( )
  • NodeType tempPtr
  • while (topPtr ! NULL)

tempPtr
topPtr
85
NULL
46
  • StackTypeMakeEmpty( )
  • NodeType tempPtr
  • while (topPtr ! NULL)
  • tempPtr topPtr

tempPtr
topPtr
85
NULL
47
  • StackTypeMakeEmpty( )
  • NodeType tempPtr
  • while (topPtr ! NULL)
  • tempPtr topPtr
  • topPtr topPtr-gtnext

tempPtr
topPtr
85
NULL
NULL
48
  • StackTypeMakeEmpty( )
  • NodeType tempPtr
  • while (topPtr ! NULL)
  • tempPtr topPtr
  • topPtr topPtr-gtnext
  • delete tempPtr

tempPtr
topPtr
?
NULL
49
Stack After Deleting Last Node
topPtr
NULL
50
Why is a destructor needed?
  • When a local stack variable goes out of scope,
    the memory space for data member topPtr is
    deallocated. But the nodes that topPtr points to
    are not automatically deallocated.
  • A class destructor is used to deallocate the
    dynamic memory pointed to by the data member.

51
  • StackTypeStackType( )
  • MakeEmpty( )

52
  • bool StackTypeIsFull( ) const
  • NodeType location

location
53
  • bool StackTypeIsFull( ) const
  • NodeType location
  • location new NodeType // Returns NULL if heap
    is depleted

location
54
  • bool StackTypeIsFull( ) const
  • NodeType location
  • location new NodeType // Returns NULL if heap
    is depleted

NULL
location
55
  • bool StackTypeIsFull( ) const
  • NodeType location
  • location new NodeType // Returns NULL if heap
    is depleted
  • if (location NULL)
  • return TRUE

NULL
location
56
  • bool StackTypeIsFull( ) const
  • NodeType location
  • location new NodeType // Returns NULL if heap
    is depleted
  • if (location NULL)
  • return TRUE
  • else

location
57
  • bool StackTypeIsFull( ) const
  • NodeType location
  • location new NodeType // Returns NULL if heap
    is depleted
  • if (location NULL)
  • return TRUE
  • else
  • delete location

location
?
58
  • bool StackTypeIsFull( ) const
  • NodeType location
  • location new NodeType // Returns NULL if heap
    is depleted
  • if (location NULL)
  • return TRUE
  • else
  • delete location
  • return FALSE

location
?
59
Comparing Stack Implementations
60
Stack Comparisons
  • Ways of comparing implementations
  • Storage requirements
  • Efficiency of the algorithms (Big-O)

61
Storage Requirements for Stack Implementation
  • Array-based - implementation takes the same
    amount of memory (maximum possible), no matter
    how many locations are actually used.
  • Linked-based - implementation only requires space
    for the actual number of elements used, but each
    element is larger because both the pointer and
    the data must be stored.

62
Efficiency of Stack Operations (Big-O)
63
Stack Constructor
  • Array implementation Linked implementation
  • StackTypeStackType( ) StackTypeStackType( )
  • top -1 topPtr NULL
  • O(1) O(1)

64
MakeEmpty
  • Array implementation Linked implementation
  • void StackTypeMakeEmpty( ) void
    StackTypeMakeEmpty( )
  • top -1 NodeType tempPtr
  • while (topPtr ! NULL)
  • tempPtr topPtr
  • topPtr topPtr-gtnext
  • delete tempPtr
  • O(1) O(N)

65
Efficiency of Stack Operations (Big-O)
  • Array Linked
  • Class constructor O(1) O(1)
  • MakeEmpty O(1) O(N)
  • IsFull O(1) O(1)
  • IsEmpty O(1) O(1)
  • Push O(1) O(1)
  • Pop O(1) O(1)
  • Destructor O(1) O(N)
  • Overall efficiency is roughly equivalent (because
    MakeEmpty and the Destructor are usually not used
    much)

66
Advantages of Linked Implementation
  • More flexible.
  • Wastes less space when the number of stack items
    can vary greatly and the stack size is small.

67
Use a Linked Implementation when . . .
  • The number of stack items can vary greatly.
  • The stack size is totally unpredictable.

68
Advantages of Array Implementation
  • Short, simple, efficient.
  • Executes faster if pushing and popping occur
    frequently.

69
Use an Array Implementation when . . .
  • The exact number of stack items is known and will
    not vary.
  • The language does not support dynamic memory
    allocation.

70
Queue ImplementationAs A Linked Structure
71
  • struct NodeType
  • ItemType info
  • NodeType next
  • NodeType qFront
  • NodeType qRear

qFront
qRear
72
Linked Structure
qFront
qRear
qRear-gtnext
qFront-gtnext
C
A
NULL
qFront-gtinfo
qRear-gtinfo
73
  • include ItemType.h
  • struct NodeType
  • ItemType info
  • NodeType next
  • class QueType // Queue as a linked structure
  • public
  • . . .
  • private
  • NodeType qFront
  • NodeType qRear

qFront
qRear
74
  • class QueType // Queue implementation using a
  • // linked structure
  • public
  • QueType( )
  • void MakeEmpty( )
  • void Enqueue(ItemType newItem)
  • void Dequeue(ItemType item)
  • bool IsEmpty( ) const
  • bool IsFull( ) const

75
  • include Queue.h
  • include ltstddef.hgt // for NULL
  • QueTypeQueType( ) // default constructor
  • qFront NULL
  • qRear NULL

NULL
qFront
NULL
qRear
76
  • bool QueTypeIsEmpty( ) const
  • return (qFront NULL)

NULL
qFront
77
Enqueuing a Node onto anEmpty Queue
78
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode

newNode
79
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType

newNode

newNode
80
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo newItem

newNode

newNode
C
newNode
81
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo newItem
  • newNode-gtnext NULL

newNode

newNode
newNode
C
C
newNode
NULL
NULL
qFront
NULL
qRear
82
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo newItem
  • newNode-gtnext NULL
  • qFront newNode

newNode

newNode
newNode
C
C
newNode
NULL
qFront
NULL
qRear
83
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo newItem
  • newNode-gtnext NULL
  • qFront newNode
  • qRear newNode

newNode

newNode
newNode
C
C
newNode
NULL
qFront
qRear
84
Queue After an Enqueue Operation
qFront
C
NULL
qRear
85
Enqueuing a Node onto a Queuethat Contains At
Least One Node
86
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode

newNode
qFront
C
NULL
qRear
87
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType

newNode

newNode
qFront
C
NULL
qRear
88
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo newItem

newNode

newNode
A
newNode
qFront
C
NULL
qRear
89
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo newItem
  • newNode-gtnext NULL

newNode

newNode
newNode
A
A
newNode
NULL
qFront
C
NULL
qRear
90
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo newItem
  • newNode-gtnext NULL
  • qRear-gtnext newNode

newNode

newNode
newNode
A
A
newNode
NULL
qFront
C
qRear
91
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo newItem
  • newNode-gtnext NULL
  • qRear-gtnext newNode
  • qRear newNode

newNode

newNode
newNode
A
A
newNode
NULL
qFront
C
qRear
92
Queue After a Second Enqueue
qFront
A
NULL
C
qRear
93
Comparing the Two Implementations
  • void QueTypeEnqueue(ItemType newItem)
  • // Empty queue // Queue with a node
  • NodeType newNode NodeType newNode
  • newNode new NodeType newNode new
    NodeType
  • newNode-gtinfo newItem newNode-gtinfo
    newItem
  • newNode-gtnext NULL newNode-gtnext NULL
  • qFront newNode qRear-gtnext newNode
  • qRear newNode qRear newNode

94
Final Enqueue Implementation
  • void QueTypeEnqueue(ItemType newItem)
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo newItem
  • newNode-gtnext NULL
  • if (qRear NULL)
  • qFront newNode
  • else
  • qRear-gtnext newNode
  • qRear newNode

95
Dequeuing a Node from a Queuethat Contains More
than One Node
96
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr

tempPtr
qFront
A
NULL
C
qRear
97
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo

tempPtr
C
item
qFront
A
NULL
C
qRear
98
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo
  • tempPtr qFront

tempPtr
C
item
tempPtr
qFront
A
NULL
C
qRear
99
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo
  • tempPtr qFront
  • qFront qFront-gtnext

tempPtr
C
item
tempPtr
qFront
A
NULL
C
qRear
100
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo
  • tempPtr qFront
  • qFront qFront-gtnext
  • delete tempPtr

tempPtr
C
item
tempPtr
qFront
?
A
NULL
qRear
101
Queue After a Dequeue Operation
qFront
A
NULL
qRear
102
Dequeuing a Node from a Queuethat Contains Only
One Node
103
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr

tempPtr
qFront
A
NULL
qRear
104
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo

tempPtr
A
item
qFront
A
NULL
qRear
105
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo
  • tempPtr qFront

tempPtr
A
item
tempPtr
qFront
A
NULL
qRear
106
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo
  • tempPtr qFront
  • qFront qFront-gtnext

tempPtr
C
item
tempPtr
qFront
NULL
A
NULL
qRear
107
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo
  • tempPtr qFront
  • qFront qFront-gtnext
  • delete tempPtr
  • qRear NULL

tempPtr
C
item
tempPtr
qFront
?
NULL
qRear
108
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo
  • tempPtr qFront
  • qFront qFront-gtnext
  • delete tempPtr
  • qRear NULL

tempPtr
C
item
tempPtr
qFront
?
NULL
qRear
NULL
109
Comparing the Two Implementations
  • void QueTypeDequeue(ItemType item)
  • // Queue with more than // Queue with only one
    node
  • // one node
  • NodeType tempPtr NodeType tempPtr
  • item qFront-gtinfo item qFront-gtinfo
  • tempPtr qFront tempPtr qFront
  • qFront qFront-gtnext qFront qFront-gtnext
  • delete tempPtr delete tempPtr
  • qRear NULL

110
Final Dequeue Implementation
  • void QueTypeDequeue(ItemType item)
  • NodeType tempPtr
  • item qFront-gtinfo
  • tempPtr qFront
  • qFront qFront-gtnext
  • if (qFront NULL)
  • qRear NULL
  • delete tempPtr

111
IsFull
112
  • bool QueTypeIsFull( ) const
  • NodeType ptr
  • ptr new NodeType
  • if (ptr NULL)
  • return TRUE
  • else
  • delete ptr
  • return FALSE

ptr
113
MakeEmpty
114
tempPtr
  • void QueTypeMakeEmpty( )
  • NodeType tempPtr

qFront
A
NULL
C
qRear
115
tempPtr
  • void QueTypeMakeEmpty( )
  • NodeType tempPtr
  • while (qFront ! NULL)

qFront
A
NULL
C
qRear
116
tempPtr
  • void QueTypeMakeEmpty( )
  • NodeType tempPtr
  • while (qFront ! NULL)
  • tempPtr qFront

tempPtr
qFront
A
NULL
C
qRear
117
tempPtr
  • void QueTypeMakeEmpty( )
  • NodeType tempPtr
  • while (qFront ! NULL)
  • tempPtr qFront
  • qFront qFront-gtnext

tempPtr
qFront
A
NULL
C
qRear
118
tempPtr
  • void QueTypeMakeEmpty( )
  • NodeType tempPtr
  • while (qFront ! NULL)
  • tempPtr qFront
  • qFront qFront-gtnext
  • delete tempPtr

tempPtr
qFront
?
A
NULL
qRear
119
tempPtr
  • void QueTypeMakeEmpty( )
  • NodeType tempPtr
  • while (qFront ! NULL)
  • tempPtr qFront
  • qFront qFront-gtnext
  • delete tempPtr

tempPtr
qFront
A
NULL
qRear
120
tempPtr
  • void QueTypeMakeEmpty( )
  • NodeType tempPtr
  • while (qFront ! NULL)
  • tempPtr qFront
  • qFront qFront-gtnext
  • delete tempPtr

tempPtr
qFront
A
NULL
NULL
qRear
121
tempPtr
  • void QueTypeMakeEmpty( )
  • NodeType tempPtr
  • while (qFront ! NULL)
  • tempPtr qFront
  • qFront qFront-gtnext
  • delete tempPtr

tempPtr
qFront
?
NULL
qRear
122
tempPtr
  • void QueTypeMakeEmpty( )
  • NodeType tempPtr
  • while (qFront ! NULL)
  • tempPtr qFront
  • qFront qFront-gtnext
  • delete tempPtr
  • qRear NULL

tempPtr
qFront
?
NULL
qRear
NULL
123
Queue After MakeEmpty
qFront
NULL
qRear
NULL
124
Comparing Queue Implementations
125
Queue Comparisons
  • Ways of comparing implementations
  • Storage requirements
  • Efficiency of the algorithms (Big-O)

126
Storage Requirementsfor Queue Implementations
  • Array-based - implementation takes the same
    amount of memory (maximum possible), no matter
    how many locations are actually used.
  • Linked-based - implementation only requires space
    for the actual number of elements used, but each
    element is larger because both the pointer and
    the data must be stored.

127
Efficiency of Queue Operations (Big-O)
  • Array Linked
  • Class constructor O(1) O(1)
  • MakeEmpty O(1) O(N)
  • IsFull O(1) O(1)
  • IsEmpty O(1) O(1)
  • Enqueue O(1) O(1)
  • Dequeue O(1) O(1)
  • Destructor O(1) O(N)
  • Overall efficiency is roughly equivalent (because
    MakeEmpty and the Destructor are usually not used
    much)

128
Linked Implementation of a Unsorted List
129
  • include ItemType.h
  • struct NodeType
  • ItemType info
  • NodeType next
  • class UnsortedType // Unsorted list as a linked
    structure
  • public
  • . . .
  • private
  • int length
  • NodeType listData
  • NodeType currentPos

length
listData
currentPos
130
  • class UnsortedType // Unsorted list as a linked
    structure
  • public
  • UnsortedType( )
  • UnsortedType(const UnsortedType source)
  • UnsortedType( )
  • void MakeEmpty( )
  • bool IsFull( ) const
  • int LengthIs( ) const
  • . . .
  • void GetNextItem(ItemType item)

131
Default Constructor
  • Initializes length to 0 and listData to NULL

132
Destructor
  • Deletes the nodes in the list (implementation
    similar to stack) and sets length equal to 0

133
MakeEmpty
  • Implementation identical to destructor

134
IsFull
  • Implementation identical to stack and queue

135
LengthIs
  • Returns the value of length (i.e., return length)

136
InsertItem
  • Inserts an item at the front of the list
    (implementation identical to stack) and
    increments length

137
DeleteItem
  • Deletes a node in the list and decrements length
  • Special cases that must be handled
  • Delete the only node in the list
  • Delete the first node in the list
  • Delete a node between two nodes
  • Delete the last node in the list

138
Deleting the Last Node in a Unsorted List
139
listData
15
30
20
NULL
3
listData
length
15
30
NULL
140
listData
15
30
20
NULL
  • // Need a pointer pointing to the node that will
    be deleted
  • // (tempLocation) and one pointing to the node
    that precedes
  • // it (location)

location
tempLocation
3
length
listData
15
30
NULL
141
listData
15
30
NULL
20
NULL
  • location-gtnext tempLocation-gtnext

location
tempLocation
3
length
listData
15
30
NULL
142
listData
15
30
NULL
?
  • location-gtnext tempLocation-gtnext
  • delete tempLocation

location
tempLocation
3
length
listData
15
30
NULL
143
listData
15
30
NULL
?
  • location-gtnext tempLocation-gtnext
  • delete tempLocation
  • length--

location
tempLocation
2
length
listData
15
30
NULL
144
Efficiency of Unsorted List Operations (Big-O)
145
Big-O Comparisons of Unsorted List
  • Array Linked
  • Class constructor O(1) O(1)
  • Destructor O(1) O(N)
  • MakeEmpty O(1) O(N)
  • IsFull O(1) O(1)
  • LengthIs O(1) O(1)
  • ResetList O(1) O(1)
  • GetNextItem O(1) O(1)
  • RetrieveItem O(N) O(N)
  • InsertItem O(1) O(1)
  • DeleteItem O(N) O(N)

146
Linked Implementation of a Sorted List
147
Member Function Implementations
  • Implementations of the member functions are
    identical to those for the unsorted list except
    for InsertItem

148
InsertItem
  • Inserts the new item in the appropriate place in
    the list and increments length
  • Special cases that must be handled
  • Inserting a new first node
  • Inserting between two nodes
  • Inserting a new last node
  • Inserting into an empty list

149
Inserting a Node Between Two Nodes in a Sorted
List
150
listData
15
60
85
NULL
  • // Need a pointer pointing to the node that will
    precede
  • // the new node (predloc) and one pointing to
    the node that
  • // will follow the new node (location)

predloc
location
3
length
151
listData
15
60
85
NULL
  • NodeType newNode

predloc
location
newNode
3
length
152
listData
15
60
85
NULL
  • NodeType newNode
  • newNode new NodeType

predloc
location
newNode
3
length
153
listData
15
60
85
NULL
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo item

predloc
location
newNode
70
3
length
154
listData
15
60
85
NULL
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo item
  • newNode-gtnext location

predloc
location
newNode
70
3
length
155
listData
15
60
85
NULL
predloc
location
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo item
  • newNode-gtnext location
  • predloc-gtnext newNode

newNode
70
3
length
156
listData
15
60
85
NULL
predloc
location
newNode
70
  • NodeType newNode
  • newNode new NodeType
  • newNode-gtinfo item
  • newNode-gtnext location
  • predloc-gtnext newNode
  • length

4
length
157
Efficiency of Sorted List Operations (Big-O)
158
Big-O Comparisons of Sorted List
  • Array Linked
  • Class constructor O(1) O(1)
  • Destructor O(1) O(N)
  • MakeEmpty O(1) O(N)
  • IsFull O(1) O(1)
  • LengthIs O(1) O(1)
  • ResetList O(1) O(1)
  • GetNextItem O(1) O(1)
  • RetrieveItem O(N) O(N)
  • InsertItem O(N) O(N)
  • DeleteItem O(N) O(N)
  • O(log2N) if binary search is used Cannot do
    binary search
Write a Comment
User Comments (0)
About PowerShow.com