Converting Infix to Postfix and Implementing Stacks in C++, Slides of Data Structures and Algorithms

An example of converting an infix expression to postfix notation using a stack. It also explains how to implement a stack in c++ using templates. The code for a stack class and its implementation.

Typology: Slides

2011/2012

Uploaded on 11/03/2012

ekna
ekna 🇮🇳

4.2

(5)

75 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Converting Infix to Postfix
Example: (A + B) * C
symb postfix stack
( (
A A (
+ A ( +
B AB ( +
) AB +
* AB + *
C AB + C *
AB + C *
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Converting Infix to Postfix and Implementing Stacks in C++ and more Slides Data Structures and Algorithms in PDF only on Docsity!

Converting Infix to Postfix

 Example: (A + B) * C

symb postfix stack

A A (

+ A ( +

B AB ( +

) AB +

* AB + *

C AB + C *

AB + C *

C++ Templates

 We need a stack of operands and a stack

of operators.

 Operands can be integers and floating

point numbers, even variables.

 Operators are single characters.

 We would have to create classes

FloatStack and CharStack.

 Yet the internal workings of both classes is

the same.

Stack using templates

Stack.h:

template

class Stack {

public:

Stack(); int empty(void); // 1=true, 0=false int push(T &); // 1=successful,0=stack overflow T pop(void); T peek(void); ~Stack();

private:

int top; T* nodes;

};

Stack using templates

Stack.cpp

#include <iostream.h> #include <stdlib.h> #include "Stack.cpp"

#define MAXSTACKSIZE 50

template Stack::Stack() { top = -1; nodes = new T[MAXSTACKSIZE]; }

Stack using templates

Stack.cpp

template

int Stack::push(T& x)

{

if( top < MAXSTACKSIZE ) {

nodes[++top] = x; return 1;

}

cout << "stack overflow in push.\n";

return 0;

}

Stack using templates

Stack.cpp

template T Stack::pop(void) { T x; if( !empty() ) { x = nodes[top--]; return x; } cout << "stack underflow in pop.\n"; return x; }

Function Call Stack

 Stacks play a key role in implementation of

function calls in programming languages.

 In C++, for example, the “call stack” is

used to pass function arguments and

receive return values.

 The call stack is also used for “local

variables”

Call Stack

 In GCC, a popular C/C++ compiler on Intel

platform, stack entries are:

return address

first argument

second argument

last argument ……….

n*4(%esp)

(%esp) top

8(%esp)

4(%esp)

Call Stack

Example: consider the function:

int i_avg (int a, int b) { return (a + b) / 2; }

.globl _i_avg

_i_avg:

movl 4(%esp), %eax

addl 8(%esp), %eax # Add the args

sarl $1, %eax # Divide by 2

ret # Return value is in %eax

Memory Organization

 When a program

(.exe) is run, it is

loaded in memory. It

becomes a process.

 The process is given

a block of memory.

 [Control-Alt-DEL]

Process 1

(browser)

Process 3

(word)

Process 4

(excel)

Windows OS

Process 2

(dev-c++)

Memory Organization

Code

Static data

Stack

Heap

Process 1

(browser)

Process 3

(word)

Process 4

(excel)

Windows OS

Process 2

(dev-c++)