0% found this document useful (0 votes)
25 views7 pages

DDD 2 Qwsss 4 R 5 TRFG

The document discusses the implementation of stacks using dynamic array allocation and provides a template for a stack class. It explains postfix notation for arithmetic expressions, detailing how to evaluate them using stacks without needing precedence rules. Additionally, it includes a function specification for replacing items in a stack and provides an algorithm for performing this operation.

Uploaded by

josephagyeman001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

DDD 2 Qwsss 4 R 5 TRFG

The document discusses the implementation of stacks using dynamic array allocation and provides a template for a stack class. It explains postfix notation for arithmetic expressions, detailing how to evaluate them using stacks without needing precedence rules. Additionally, it includes a function specification for replacing items in a stack and provides an algorithm for performing this operation.

Uploaded by

josephagyeman001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Implementing stacks using dynamic

array allocation (cont.)

template<class ItemType>
StackType<ItemType>::StackType(int max)
{
maxStack = max;
top = -1;
items = new ItemType[max];
}

template<class ItemType>
StackType<ItemType>::~StackType()
{
delete [ ] items;
}
Example: postfix expressions

• Postfix notation is another way of writing arithmetic


expressions.
• In postfix notation, the operator is written after the
two operands.
infix: 2+5 postfix: 2 5 +
• Expressions are evaluated from left to right.

• Precedence rules and parentheses are never


needed!!
Example: postfix expressions
(cont.)
Postfix expressions:
Algorithm using stacks (cont.)
Postfix expressions:
Algorithm using stacks

WHILE more input items exist


Get an item
IF item is an operand
stack.Push(item)
ELSE
stack.Pop(operand2)
stack.Pop(operand1)
Compute result
stack.Push(result)
stack.Pop(result)
Write the body for a function that replaces each copy of an
item in a stack with another item. Use the following
specification. (this function is a client program).

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.

(You may use any of the member functions of the


StackType, but you may not assume any knowledge of how
the stack is implemented).
Stack tempStack
{
ItemType item;
StackType tempStack;

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

You might also like