07-Stacks&Qu - Ch Chapter 7 Stacks and Queues St This material is based on contents the book Data structures Algorithms and Software

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 7: Stacks and Queues This material is based on contents the book: “Data structures, Algorithms, and Software Principles in C”, from T.A. Standish. Parts also under permission from David McCaughan, University of Guelph, Canada. USF – COP4530 – Data Structures Summer 2009 1of±23 a d cCaug a , U e s ty o Gue p , Ca ada
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Recursive functions in C using stacks C uses a run-time stack to call functions – A stack frame is a collection of information – Each running program is given a stack Stack is filled up with function calls – For every new function call, a new stack frame appears – New stacks are put on top of previous stacks – Each one will point to previous one USF – COP4530 – Data Structures Summer 2009 2of±23
Background image of page 2
Recursive functions in C using stacks Stack frame for function F contains 1. Space to hold the value V returned by the function 2. A pointer to the base of the previous stack frame in the stack 3. A return address, which is the address of an instruction to execute in order to resume execution of F ’s caller after xecution of as terminated execution of F has terminated 4. Parameter storage sufficient to hold the actual parameters of F 5. A set of storage locations sufficient to hold the values of variables declared locally within F USF – COP4530 – Data Structures Summer 2009 3of±23
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Recursive functions in C using stacks Consider Factorial recursive function – Example of run: x = Factorial(3) – First, calculate and place Ψ = &x (the address of x) double Factorial(int n){ if (n <= 1){ return 1.0; } else { eturn n * Factorial(n ); return n Factorial(n 1); } } Space for more stack growth Stack frame for the call α 3 empty 5. Space for locally declared variables 4. Actual parameter n = 3 pg 3. Return address Factorial(3) ? 2. Pointer to previous stack frame base 1. Return value of Factorial(3) ddress of x = USF – COP4530 – Data Structures Summer 2009 4of±23 Ψ Address of x Ψ
Background image of page 4
Recursive functions in C using stacks Some notes: – There are many ways to implement recursive function calls using stacks – Different compilers use different arrangements – Some subtleties were omitted Idea: stack + iteration can implement recursion Stacks can process any kind of nested structure Question: – Why will a recursive function cause a C run-time system to n out of stack at program execution time? USF – COP4530 – Data Structures Summer 2009 run out of stack at program execution time? 5of±23
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Queue ADT Queue representation – If an endless array is used, you can insert in rear and remove from front. This is not practical in reality – Queues can be represented using a circular track Modular arithmetic is used for simulation of track X % N to maintain position values in range 0:N-1 Front = (Front + 1) % N Front () Rear = (Rear + 1) % N Rear USF – COP4530 – Data Structures Summer 2009 6of±23
Background image of page 6
Queue Interface QueueInterface.h #include “QueueTypes.h”
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/28/2009 for the course COP COP 4350 taught by Professor Jimeno during the Summer '09 term at University of South Florida - Tampa.

Page1 / 23

07-Stacks&amp;Qu - Ch Chapter 7 Stacks and Queues St This material is based on contents the book Data structures Algorithms and Software

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online