0% found this document useful (0 votes)
15 views23 pages

Lec 7

The document discusses stacks, emphasizing their LIFO (Last In, First Out) property and implementation methods, specifically linked-list-based stacks. It provides detailed explanations of stack operations such as Push and Pop, along with their time complexities and comparisons with array-based implementations. Additionally, it includes an exercise on replacing items in a stack while maintaining the order of other elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views23 pages

Lec 7

The document discusses stacks, emphasizing their LIFO (Last In, First Out) property and implementation methods, specifically linked-list-based stacks. It provides detailed explanations of stack operations such as Push and Pop, along with their time complexities and comparisons with array-based implementations. Additionally, it includes an exercise on replacing items in a stack while maintaining the order of other elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Data Structures

Stacks
Linked-list Based
What is a stack?
§ It is an ordered group of homogeneous items.
§ Items are added to and removed from the top of the
stack
LIFO property: Last In, First Out
§ The last item added would be the first to be removed

TOP OF THE STACK TOP OF THE STACK


Stack Implementations
Array-based

Linked-list-based
Linked-list-based Stacks
template<class ItemType>
struct NodeType<ItemType> {
ItemType info;
NodeType<ItemType>* next;
};
Linked-list-based Stacks (cont’d)
template<class ItemType>
struct NodeType<ItemType>;

template<class ItemType>
class StackType {
public:
StackType();
~StackType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType);
void Pop(ItemType&);
private:
NodeType<ItemType>* topPtr;
};
Linked-list-based Stacks (cont’d)
template<class ItemType>
StackType<ItemType>::StackType()
{
topPtr = NULL; O(1)
}

template<class ItemType>
void StackType<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;
while(topPtr != NULL) {
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr; O(N)
}
}
Linked-list-based Stacks (cont’d)
template<class ItemType>
StackType<ItemType>::~StackType()
{
MakeEmpty(); O(N)
}

template<class ItemType>
bool StackType<ItemType>::IsEmpty() const
{
return(topPtr == NULL);
} O(1)
Linked-list-based Stacks (cont’d)
template<class ItemType>
bool StackType<ItemType>::IsFull() const
{
NodeType<ItemType>* location;

location = new NodeType<ItemType>; // test


if(location == NULL)
return true;
else {
delete location; O(1)
return false;
}
}
Push (ItemType newItem)

§ Function: Adds newItem to the top of the stack.

§ Preconditions: Stack has been initialized and is not full.

§ Postconditions: newItem is at the top of the stack.


Pushing on a
non-empty
stack
Pushing on a non-empty stack
(cont.)
§ The order of changing the pointers is important!
Special Case: pushing on an
empty stack
Function Push
template <class ItemType>
void StackType<ItemType>::Push(ItemType
item)
{
NodeType<ItemType>* location;

location = new NodeType<ItemType>;


location->info = item;
location->next = topPtr; O(1)
topPtr = location;
}
Pop (ItemType& item)
§ Function: Removes topItem from stack and returns it in
item.

§ Preconditions: Stack has been initialized and is not empty.

§ Postconditions: Top element has been removed from


stack and item is a copy of the removed element.
Popping the top element
Popping the
top element
(cont.)

Need a
temporary
pointer !
Special case: popping the last
element on the stack

tempPtr
Function Pop
template <class ItemType>
void StackType<ItemType>::Pop(ItemType& item)
{
NodeType<ItemType>* tempPtr;

item = topPtr->info;
tempPtr = topPtr;
topPtr = topPtr->next; O(1)
delete tempPtr;
}
Comparing stack implementations
Big-O Comparison of Stack Operations
Operation Array Linked
Implementation Implementation
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)
Array-based vs Linked-list-based
Stack Implementations
§ Array-based implementation is simple but:
§ The size of the stack must be determined when a stack object is
declared.
§ Space is wasted if we use less elements.
§ We cannot "enqueue" more elements than the array can hold.

§ Linked-list-based implementation alleviates these problems but


time requirements might increase.
Exercise 15: Write the body for a function that
replaces each copy of an item in a stack with another
item. Use the following specification.
ReplaceItem(StackType& stack, ItemType oldItem,
ItemType newItem)
Function: Replaces all occurrences of oldItem with
newItem.
Precondition: stack has been initialized.
Postconditions: Each occurrence of oldItem in stack
has been replaced by newItem. Order of other
elements remains unchanged.

Warning: you may not assume any knowledge of


how the stack is implemented!
Stack tempStack
{
ItemType item;
StackType tempStack;

while (!Stack.IsEmpty()) { 3 1
Stack.Pop(item);
if (item==oldItem) 2 5
tempStack.Push(newItem); 1 3
else Stack
tempStack.Push(item);
}
while (!tempStack.IsEmpty()) {
tempStack.Pop(item);
Stack.Push(item); 3
}
oldItem = 2 5
}
newItem = 5 1
{
ItemType item;
StackType tempStack;

while (!Stack.IsEmpty()) { What are the time


Stack.Pop(item); requirements using big-O?
if (item==oldItem)
tempStack.Push(newItem);
else
tempStack.Push(item); O(N)
}
while (!tempStack.IsEmpty()) {
tempStack.Pop(item);
Stack.Push(item);
}
}

You might also like