(Part I) Data Structures: Saqib Saleem
(Part I) Data Structures: Saqib Saleem
(Part I)
Data Structures
Saqib Saleem
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
Stack.h:
template <class T>
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
Stack.cpp
template <class T>
Stack<T>::~Stack()
{
delete nodes;
}
Stack.cpp
template <class T>
int Stack<T>::push(T& x)
{
if( top < MAXSTACKSIZE ) {
nodes[++top] = x;
return 1;
}
cout << "stack overflow in push.\n";
return 0;
}
Stack using templates
Stack.cpp
intstack.push(x); intstack.push(y);
cout << "intstack: " << intstack.pop() << ", "
<< intstack.pop() << "\n";
charstack.push(c); charstack.push(d);
cout << "charstack: " << charstack.pop() << ", "
<< charstack.pop() << "\n";
}
Function Call Stack
.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
Windows OS
Task Manager
Memory Organization
Process 1
(browser) Code
Process 3
(word) Static data
Process 4 Stack
(excel)
Process 2
(dev-c++)
Windows OS Heap