Lecture No.08 Data Structures Dr. Sohail Aslam. Converting Infix to Postfix  Example: (A + B) * C symbpostfixstack( AA( +A( + BAB( + )AB + *AB +* CAB.

Slides:



Advertisements
Similar presentations
Buffer Overflow Prabhaker Mateti Wright State University.
Advertisements

Compiler Construction Sohail Aslam Lecture ExampleExample a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2.
23-Apr-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
Pseudocode for Bracket matching Stack mystack; // declare & initialize stack Loop through all characters in program { if (symbol is an ‘opener’) // (,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
ITEC 320 Lecture 26 C++ Introduction. Introduction Qualification s Ten years of C++ development (albeit not fulltime) Written somewhere between kloc.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
Templates Zhen Jiang West Chester University
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
Overflows & Exploits. In the beginning 11/02/1988 Robert Morris, Jr., a graduate student in Computer Science at Cornell, wrote an experimental, self-replicating,
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
C:\Temp\Templates 4 5 Use This Main Program 6.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
Stack and Heap Memory Stack resident variables include:
Lecture No.09 Data Structures Dr. Sohail Aslam
Computer Architecture and Assembly Language
Lecture No.06 Data Structures Dr. Sohail Aslam
Command Line Arguments
OOP-4-Templates, ListType
Homework Reading Machine Projects Labs PAL, pp ,
Exploiting & Defense Day 2 Recap
Stacks.
Algorithms and Data Structures
Stack.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Lecture No.07 Data Structures Dr. Sohail Aslam
Name: Rubaisha Rajpoot
Assembly Language Programming II: C Compiler Calling Sequences
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is.
Understanding Program Address Space
System Calls David Ferry CSCI 3500 – Operating Systems
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Stacks CS-240 Dick Steflik.
Reference Variables One should be careful about transient objects that are stored by reference in data structures. Consider the following code that stores.
template< class T > class Stack { public:
C++ Plus Data Structures
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Compiler Construction
“Way easier than when we were students”
Function Templates Class Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

Lecture No.08 Data Structures Dr. Sohail Aslam

Converting Infix to Postfix  Example: (A + B) * C symbpostfixstack( AA( +A( + BAB( + )AB + *AB +* CAB + 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.

C++ Templates  We can use C++ Templates to create a “template” of a stack class.  Instantiate float stack, char stack, or stack for any type of element we want.

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 #include "Stack.cpp" #define MAXSTACKSIZE 50 template Stack ::Stack() { top = -1; nodes = new T[MAXSTACKSIZE]; }

Stack using templates Stack.cpp template Stack ::~Stack() { delete nodes; } template int Stack ::empty(void) { if( top < 0 ) return 1; return 0; }

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; }

Stack using templates main.cpp #include "Stack.cpp" int main(int argc, char *argv[]) { Stack intstack; Stack charstack; int x=10, y=20; char c='C', d='D'; 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  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) top(%esp) 8(%esp) 4(%esp)

Call Stack Example: consider the function: int i_avg (int a, int b) { return (a + b) / 2; } # Stack layout on entry: # # 8(%esp) b # 4(%esp) a # (%esp) return address

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++)

Task Manager

Memory Organization Code Static data Stack Heap Process 1 (browser) Process 3 (word) Process 4 (excel) Windows OS Process 2 (dev-c++)