Stack Data Structure: Implementation and Uses, Exams of Biology

An overview of stacks as a data structure, including definitions, operations, and implementations using C++. stack specifications, push and pop functions, stack overflow and underflow, and implementing stacks using templates and dynamic array allocation. Additionally, it discusses the use of stacks in processing postfix expressions.

Typology: Exams

2019/2020

Uploaded on 08/23/2021

quan-bui
quan-bui 🇻🇳

3.5

(2)

6 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Stacks
CS 308 – Data Structures
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Stack Data Structure: Implementation and Uses and more Exams Biology in PDF only on Docsity!

Stacks

CS 308 – Data Structures

What is a stack?

  • It is an ordered group of homogeneous items of elements.
  • Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack).
  • The last element to be added is the first to be removed ( LIFO : Last In, First Out).

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.

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.

Stack Implementation #include "ItemType.h" // Must be provided by the user of the class // Contains definitions for MAX_ITEMS and ItemType class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; ItemType items[MAX_ITEMS]; };

Stack Implementation (cont.) StackType:: StackType() { top = -1; } void StackType:: MakeEmpty() { top = -1; } bool StackType:: IsEmpty() const { return (top == -1); }

Stack overflow

  • The condition resulting from trying to push

an element onto a full stack.

if(!stack.IsFull())

stack.Push(item);

Stack underflow

  • The condition resulting from trying to pop

an empty stack.

if(!stack.IsEmpty())

stack.Pop(item);

Implementing stacks using templates

  • Templates allow the compiler to generate

multiple versions of a class type or a

function by allowing parameterized types.

  • It is similar to passing a parameter to a

function (we pass a data type to a class !!)

Example using templates

// Client code

StackType myStack;

StackType yourStack;

StackType anotherStack;

myStack.Push(35);

yourStack.Push(584.39);

The compiler generates distinct class types and gives its own internal name to each of the types.

Function templates

  • The definitions of the member functions must be
rewritten as function templates.

template StackType:: StackType () { top = -1; } template void StackType:: MakeEmpty () { top = -1; }

Function templates (cont.) template void StackType:: Pop (ItemType& item) { item = items[top]; top--; }

Comments using templates

  • The^ template^ designation must precede the class method name in the source code for each template class method.
  • The word^ class^ is required by the syntax of the language and does not mean that the actual parameter must be the name of a class.
  • Passing a parameter to a template^ has an effect at compile time.

Implementing stacks using dynamic array allocation (cont.) template StackType:: StackType (int max) { maxStack = max; top = -1; items = new ItemType[max]; } template StackType::~ 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!!