Data Structure Chapter 5
Data Structure Chapter 5
Algorithms
Abstract Data Types - Stack
• Allowe one to insert and delete elements at any place in the list.
Next
• Need data structures which restrict insertions and deletions so that
they can take place only at the beginning or in the end of the list, not
in the middle.
• Stacks
• Queues
Abstract Data Type (ADT)
• Mathematical model for data types
• ADT is defined by its behaviour (from the point of view of a user):
• Possible values,
• Possible operations and behaviour of these operations
Stack of
Dishes
Stack Operations
• Create an empty stack push pop
• Destroy a stack
• Determine whether a stack is empty
• Add a new item -- push top
Stack
Stack Operations
• Stack()
– creates a an empty stack
• ~Stack()
– destroys a stack
• isEmpty():boolean
– determines whether a stack is empty or not
• push(in newItem:StackItemType)
– Adds newItem to the top of a stack
• pop() throw StackException
• topAndPop(out stackTop:StackItemType)
– Removes the top of a stack (ie. removes the item that was added most recently
• getTop(out stackTop:StackItemType)
– Retrieves the top of stack into stackTop
Implementation of the Stack ADT
• Stack can be implemented using;
• An array
• A linked list
Implementation of Stack
An Array Based Implementation of Stack
• A pointer variable TOP (contains the location of the top element of
the stack)
• A variable MAXSTK which gives the maximum number of elements
that can be held by the stack.
• Pop
An Array Based Implementation of Stack
• Private data fields
– An array of items of type StackItemType
– The index top
• Compiler-generated destructor, copy constructor, and assignment
operator
Programming Languages Concepts Used
• Templates
• Output:
An Array Based Implementation – Header File
#include "StackException.h"
const int MAX_STACK = maximum-size-of-stack;
template <class T>
class Stack {
public:
Stack(); // default constructor; copy constructor and destructor are supplied by the compiler
// stack operations: Defined as constant function, hence is not
bool isEmpty() const; // Determines whether a stack is empty. allowed to change the values of the data
members of its class.
void push(const T& newItem); // Adds an item to the top of a stack.
void pop(); // Removes the top of a stack.
void topAndPop(T& stackTop);
void getTop(T& stackTop) const; // Retrieves top of stack.
private:
T items[MAX_STACK]; // array of stack items
int top; // index to top of stack
};
An Array Based Implementation - push
template <class T>
void Stack<T>::push(const T& newItem) {
if (isEmpty())
throw StackException("StackException: stack empty on pop");
else
--top; // stack is not empty; pop top
}
An Array Based Implementation – topAndPop
if (isEmpty())
throw StackException("StackException: stack empty on pop");
else // stack is not empty; retrieve top
stackTop = items[top--];
}
An Array Based Implementation – getTop
template <class T>
void Stack<T>::getTop(T& stackTop) const {
if (isEmpty())
throw StackException("StackException: stack empty on getTop");
else
stackTop = items[top];
}
An Array Based Implementation
Disadvantages of the array based implementation is similar to the
disadvantages of arrays
It forces all stack objects to have MAX_STACK elements
A Pointer Based Implementation of Stack
• A pointer-based implementation
– Required when the stack needs to grow and shrink dynamically
– Very similar to linked lists
Object item;
StackNode* next;
};
A Pointer Based Implementation of Stack
#include "StackException.h"
template <class T>
class Stack{
public:
Stack(); // default constructor
Stack(const Stack& rhs); // copy constructor
~Stack(); // destructor
Stack& operator=(const Stack& rhs); // assignment operator
bool isEmpty() const;
void push(const T& newItem);
void pop();
void topAndPop(T& stackTop);
void getTop(T& stackTop) const;
private:
• StackNode<T> *topPtr; // pointer to the first node in the stack
};
A Pointer Based Implementation of Stack –
Constructor, isEmpty
template <class T>
Stack<T>::Stack() : topPtr(NULL) {} // default constructor
35
Testing the Stack Class
std::cout << "Printing s2:" << std::endl;
while (!s2.isEmpty()) {
int value;
s2.topAndPop(value);
std::cout << value << std::endl;
}
return 0;
}
36
References
• Lecture Notes, Yusuf Sahillioğlu, METU.
• Schaum’s Outline of Theory and Problems of Data Structures,
Seymour Lipschutz.