... stack to an empty state. IsEmpty -- Determines whether the stack is ... Pop (ItemType& item) -- Removes the item at the top of the stack and returns it in item. ... – PowerPoint PPT presentation
Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus
2 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.
3 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.
3 4 ADT Stack Operations
Transformers
MakeEmpty
Push
Pop
Observers
IsEmpty
IsFull
change state observe state 4 5 Another Stack Implementation
One advantage of an ADT is that the kind of implementation used can be changed.
The dynamic array implementation of the stack has a weakness -- the maximum size of the stack is passed to the constructor as parameter.
Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack.
6
class StackTypeltchargt 7
class StackTypeltfloatgt 23.4 -7.9 8
class StackTypeltStrTypegt 9 Tracing Client Code V letter 10 Tracing Client Code letter V Private data topPtr NULL char letter V StackTypelt char gt myStack myStack.Push(letter) myStack.Push(C) myStack.Push(S) if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ) myStack.Push(K)
11 Tracing Client Code letter V Private data topPtr V char letter V StackTypelt char gt myStack myStack.Push(letter) myStack.Push(C) myStack.Push(S) if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ) myStack.Push(K)
12 Tracing Client Code letter V Private data topPtr C V char letter V StackTypelt char gt myStack myStack.Push(letter) myStack.Push(C) myStack.Push(S) if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ) myStack.Push(K)
13 Tracing Client Code letter V Private data topPtr S C V char letter V StackTypelt char gt myStack myStack.Push(letter) myStack.Push(C) myStack.Push(S) if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ) myStack.Push(K)
14 Tracing Client Code letter V Private data topPtr S C V char letter V StackTypelt char gt myStack myStack.Push(letter) myStack.Push(C) myStack.Push(S) if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ) myStack.Push(K)
15 Tracing Client Code letter S Private data topPtr C V char letter V StackTypelt char gt myStack myStack.Push(letter) myStack.Push(C) myStack.Push(S) if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ) myStack.Push(K)
16 Tracing Client Code letter S Private data topPtr K C V char letter V StackTypelt char gt myStack myStack.Push(letter) myStack.Push(C) myStack.Push(S) if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ) myStack.Push(K)
17 Dynamically Linked Implementation of Stack
// DYNAMICALLY LINKED IMPLEMENTATION OF STACK
include "ItemType.h" // for ItemType
templateltclass ItemTypegt
struct NodeType
ItemType info
NodeTypeltItemTypegt next
17 18
// DYNAMICALLY LINKED IMPLEMENTATION OF STACK continued
templateltclass ItemTypegt
class StackType
public
StackType( ) // constructor
// Default constructor.
// POST Stack is created and empty.
void MakeEmpty( )
// PRE None.
// POST Stack is empty.
bool IsEmpty( ) const
// PRE Stack has been initialized.
// POST Function value (stack is empty)
bool IsFull( ) const
// PRE Stack has been initialized.
// POST Function value (stack is full)
18 19
// DYNAMICALLY LINKED IMPLEMENTATION OF STACK continued
void Push( ItemType item )
// PRE Stack has been initialized.
// Stack is not full.
// POST newItem is at the top of the stack.
void Pop( ItemType item )
// PRE Stack has been initialized.
// Stack is not empty.
// POST Top element has been removed from stack.
// item is a copy of removed element.
StackType( ) // destructor
// PRE Stack has been initialized.
// POST Memory allocated for nodes has been
// deallocated.
private
NodeTypeltItemTypegt topPtr
19 20
// DYNAMICALLY LINKED IMPLEMENTATION OF STACK continued
// member function definitions for class StackType
templateltclass ItemTypegt
StackTypeltItemTypegtStackType( ) // constructor
topPtr NULL
templateltclass ItemTypegt
void StackTypeltItemTypegtIsEmpty( ) const
// Returns true if there are no elements
// on the stack false otherwise
return ( topPtr NULL )
20 21 Using operator new
If memory is available in an area called the free store (or heap), operator new allocates the requested object, and returns a pointer to the memory allocated.
The dynamically allocated object exists until the delete operator destroys it.
21 22 Adding newItem to the stack newItem B
newItem B
NodeTypeltchargt location
location new NodeTypeltchargt
location-gtinfo newItem
location-gtnext topPtr
topPtr location
23 Adding newItem to the stack newItem B
newItem B
NodeTypeltchargt location
location new NodeTypeltchargt
location-gtinfo newItem
location-gtnext topPtr
topPtr location
location 24 Adding newItem to the stack newItem B
newItem B
NodeTypeltchargt location
location new NodeTypeltchargt
location-gtinfo newItem
location-gtnext topPtr
topPtr location
location 25 Adding newItem to the stack newItem B
newItem B
NodeTypeltchargt location
location new NodeTypeltchargt
location-gtinfo newItem
location-gtnext topPtr
topPtr location
B location 26 Adding newItem to the stack newItem B
newItem B
NodeTypeltchargt location
location new NodeTypeltchargt
location-gtinfo newItem
location-gtnext topPtr
topPtr location
B location 27 Adding newItem to the stack newItem B
The object currently pointed to by the pointer is deallocated, and the pointer is considered unassigned. The memory is returned to the free store.
29 30 Deleting item from the stack
NodeTypeltItemTypegt tempPtr
item topPtr-gtinfo
tempPtr topPtr
topPtr topPtr-gtnext
delete tempPtr
B X C L tempPtr 31 Deleting item from the stack item B
NodeTypeltItemTypegt tempPtr
item topPtr-gtinfo
tempPtr topPtr
topPtr topPtr-gtnext
delete tempPtr
B X C L tempPtr 32 Deleting item from the stack item B
NodeTypeltItemTypegt tempPtr
item topPtr-gtinfo
tempPtr topPtr
topPtr topPtr-gtnext
delete tempPtr
B X C L tempPtr 33 Deleting item from the stack item B
NodeTypeltItemTypegt tempPtr
item topPtr-gtinfo
tempPtr topPtr
topPtr topPtr-gtnext
delete tempPtr
B X C L tempPtr 34 Deleting item from the stack B item
NodeTypeltItemTypegt tempPtr
item topPtr-gtinfo
tempPtr topPtr
topPtr topPtr-gtnext
delete tempPtr
X C L tempPtr 35 Implementing Pop
templateltclass ItemTypegt
void StackTypeltItemTypegtPop ( ItemType item )
// Removes element at the top of the stack and
// returns it in item.
if (IsEmpty())
throw PopOnEmptyStack()
NodeTypeltItemTypegt tempPtr
item topPtr-gtinfo
tempPtr topPtr
topPtr topPtr-gtnext
delete tempPtr
35 36
templateltclass ItemTypegt
bool StackTypeltItemTypegtIsFull( ) const
// Returns true if there is no room for another NodeType // node on the free store false otherwise.
NodeTypeltItemTypegt location
try
location new NodeTypeltItemTypegt
delete location
return false
catch(bad_alloc exception)
return true
36 37
// Alternate form that works with older compilers
templateltclass ItemTypegt
bool StackTypeltItemTypegtIsFull( ) const
// Returns true if there is no room for another NodeType // node on the free store false otherwise.
NodeTypeltItemTypegt location
location new NodeTypeltItemTypegt
if ( location NULL )
return true
else
delete location
return false
37 38 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.
39
templateltclass ItemTypegt
void StackTypeltItemTypegtMakeEmpty( )
// Post Stack is empty all elements deallocated.
NodeTypeltItemTypegt tempPtr
while ( topPtr ! NULL )
tempPtr topPtr
topPtr topPtr-gtnext
delete tempPtr
templateltclass ItemTypegt
StackTypeltItemTypegtStackType( ) // destructor
MakeEmpty( )
39 40
templateltclass ItemTypegt
void QueTypeltItemTypegtDequeue( ItemType item )
// Removes element from from front of queue
// and returns it in item.
// Pre Queue has been initialized.
// Queue is not empty.
// Post Front element has been removed from queue.
PowerShow.com is a leading presentation sharing website. It has millions of presentations already uploaded and available with 1,000s more being uploaded by its users every day. Whatever your area of interest, here you’ll be able to find and view presentations you’ll love and possibly download. And, best of all, it is completely free and easy to use.
You might even have a presentation you’d like to share with others. If so, just upload it to PowerShow.com. We’ll convert it to an HTML5 slideshow that includes all the media types you’ve already added: audio, video, music, pictures, animations and transition effects. Then you can share it with your target audience as well as PowerShow.com’s millions of monthly visitors. And, again, it’s all free.
About the Developers
PowerShow.com is brought to you by CrystalGraphics, the award-winning developer and market-leading publisher of rich-media enhancement products for presentations. Our product offerings include millions of PowerPoint templates, diagrams, animated 3D characters and more.