Stack - Ciencias Computacionales

Transcription

Stack - Ciencias Computacionales
Propedéutico de
Programación
Coordinación de Ciencias
Computacionales
Semana 4,
Tercera Parte
Dra. Pilar Gómez Gil
http://ccc.inaoep.mx/~pgomez/cursos/programacion/
Versión 1.1 01.07.08
Capítulo 5
ADT´s para
Pilas y Colas
Stacks of Boxes and Books
TOP OF THE STACK
TOP OF THE STACK
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Stacks
Stack
An abstract data type in which
elements are added and removed
from only one end (LIFO)
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Stacks
• Transformers
– Push
– Pop
change state
• Observers
– IsEmpty
– IsFull
– Top
observe state
•
What about an iterator?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Stacks
class StackType
{
public:
StackType();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType item);
void Pop();
ItemType Top() const;
Logical Level
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Linked Implementation
private:
NodeType* topPtr;
}
Can we “borrow” code from
UnsortedType for Push and Pop ?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Linked Implementation
void StackType::Push(ItemType newItem)
{
if (IsFull()) throw FullStack();
else
{
NodeType* location;
location = new NodeType;
location->info = newItem;
location>next = topPtr;
topPtr = location;
}
}
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Linked Implementation
void StackType::Pop()
{
if (IsEmpty()) throw EmptyStack();
else
{
NodeType* tempPtr;
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
Does this
work for
stacks of
one item?
More than
one item?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Linked Implementation
More
than
one
item
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Linked Implementation
One
item
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Linked Implementation
What about the constructor, destructor, and
observer functions?
We can borrow all but Top from class
UnsortedType
ItemType StackType::Top()
{
if (IsEmpty()) throw EmptyStack
else return topPtr->info;
}
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
• Ver los programas en stack.zip
Queues
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Queues
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Queues
Queue
An abstract data type in which
elements are added to the rear and
removed from the front; a “first in, first
out” (FIFO) structure
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Queues
• Transformers
– MakeEmpty
– Enqueue
– Dequeue
change state
• Observers
– IsEmpty
observe state
– IsFull
•
What about an iterator?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
17
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Queues
class QueType
{
public:
QueType(int max);
QueType();
~QueType();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType item);
void Dequeue(ItemType& item);
Logical Level
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Linked Implementation
Data structure for linked queue
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Linked Implementation
Enqueue(newItem)
Set newNode to the address of newly allocated node
Set Info(newNode) to newItem
Set Next(newNode) to NULL
Set Next(rear) to newNode
If queue is empty
Set front to newNode
else
Set Next(rear) to newNode
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Linked Implementation
Dequeue(item)
Set tempPtr to front
Set item to Info(front)
Set front to Next(front)
if queue is now empty
Set rear to NULL
Deallocate Node(tempPtr)
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
• Ver programas en queue.zip
Chapter 6
Lists Plus
Tomado de: Dale, N. Weems, C++ Plus Data Structures 23
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Circular Linked Lists
Circular linked list
A list in which each node has a successor;
the “last” element is succeeded by the
“first” element
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Circular Linked List
Why is it
better
to have
the
external
pointer
point
to the
last
element
?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Circular Linked List
Initialization for search
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Circular Linked List
FindItem
cases
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Circular Linked List
Insert
an
item
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Circular Linked List
Delete
an
Item
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Doubly Linked List
Doubly linked list
A is in which each node is linked to both its
successor and its predecessor
Tomado de: Dale, N. Weems, C++ Plus Data Structures 30
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Doubly Linked List
Insert
an
item
Tomado de: Dale, N. Weems, C++ Plus Data Structures 31
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Doubly Linked List
Does
it
matter
in
which
order
we
change
the
pointers
?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Doubly Linked List
Delete an item
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Copy Structures
In a linked structure, the external pointer is
enclosed within the stack object, but the
linked structure to which it points is not
What
causes
this
problem
?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Copy Structures
Shallow copy
An operation that copies one class object to
another without copying pointed-to data
Deep copy
An operation that not only copies one class
object to another but also makes copies of
any pointed-to data
See the difference?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Copy Structures
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Copy Structures
• In summary
– 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
How do we make a deep copy?
Tomado de: Dale, N. Weems, C++ Plus Data Structures 37
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Copy Structures
Copy constructor
A special member function of a class that is
implicitly invoked when passing a parameter by
value, initializing a variable in a declaration,
and returning an object as the value of function
StackType(const StackType& anotherStack);
Of course, the code should implement a
deep copy!
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Copy Structures
• That about the assignment
operator?
– The assignment operator does a shallow
copy!
– We can overload the assignment operator
class StackType
Can
{
we
public:
...
overload
void operator=(StackType);
other
private:
operators
...
?
void StackType::operator=(StackType anotherStack)
// code for a deep copy
Tomado de: Dale, N. Weems, C++ Plus Data Structures 39
}
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Copy Structrres
Al relational operators can be overloaded
bool operator<(ItemType other) const;
// Returns true if self is less than other
bool operator>(ItemType other) const;
// Returns true if self is greater than other
bool operator==(ItemType other) const;
// Returns true if self is equal to other
...
Of course, you must write the code as for
any other function
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Polymorphism
Polymorphism
The ability to determine which of several operations
with the same name to apply to a particular object; a
combination of static and dynamic binding
Binding
The time at which a name or symbol is bound to the
appropriate
static binding: bound at compile time
dynamic binding: bound at run time
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Polymorphism
NewItemType is derived from ItemType
void PrintResult(ItemType& first, ItemType& second);
How can this be?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
Polymorphism
C++ relaxes scope rules to allow dynamic
binding
The type of the actual parameter may be an
object of a derived class of the formal parameter
virtual void PrintResult(ItemType& first, ItemType&
second);
Word virtual in base class (ItemType),
definition forces dynamic binding
Note: Parameters must be reference
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers
C++ Tips
1. Operators :: . sizeof, and ?: may not be
overloaded
2. At least one operand must be a class instance
3. Precedence, operator symbols, or number of operands
cannot be changed
4. Overloading ++ and -- requires prefix form use by
client
5. To overload these operators = ( ) [ ], member
functions must be used
6. An operator can be given multiple meanings if the
data types of operands differ
Tomado de: Dale, N. Weems, C++ Plus Data Structures 44
4th. Ed. 2007 Instructor material, © Jones & Barlett Publishers