Download presentation
Presentation is loading. Please wait.
Published byBarrie Walton Modified over 8 years ago
1
Stacks
2
An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions and deletions occur at one end of the list, known as the top or head of the stack. When an element is inserted at the top of a stack it is said to have been pushed onto the stack.
3
When an item has been deleted from the top of the stack it is said to have been popped off the stack. When a pop occurs the item at the top of the stack is returned as the output from the pop operation.
4
Initially the stack is empty No items can be popped push(3) 3 3 placed on top of stack
5
push(11) pop() 3 3 11 11 inserted at top of stack 11 is returned from the pop operation and removed from stack
6
push(7) push(6) 3 3 7 7 6 stack grows downwards
7
Since the last element pushed onto the stack is the first one to be popped, Stacks are known as Last-In-First- Out ( LIFO ) lists. Stacks and Function Invocation Stacks are very useful data structures in Computer Science. Their LIFO operation makes them suitable for certain types of problems, in particular function invocation.
8
Consider the following function declarations: int f1(int n) { int i = f2(n + 1); printf("In f1: %d\n", n); return i; } int f2(int n) { int j = n * n; printf("In f2: %d\n", n); return j; }
9
void main() { int n = 3; f1(n + 1); printf("In main: %d\n", n); } The output from the program is: In f2: 5 In f1: 4 In main: 3
10
The code segment contains three different occurrences of the variable n. 1. In function main(), where it is passed twice as an argument; 2. In function f1(), where it is an input parameter; 3. In function f2(), where it is an input parameter. The question that might arise is how does the computer know which instance of n to use in each situation and what value it contains.
11
In computers this conflict is solved with the use of stack frames. When a function is called the computer initialises a stack frame containing the bindings of the function arguments with the formal parameters to the function, as well as any local variables. The stack frame is then pushed onto the stack. When the function is being executed, the computer uses the stack frame at the top of the stack to inspect the values of the function parameters. When the function returns, the stack is popped.
12
Reconsider our code segment: On execution of main(), a stack frame is initialised and pushed on the stack. The stack frame will contain the binding for the local variable n. n =3 Stack frame
13
n =3 Stack frame void main() { int n = 3; f1(n + 1); printf("In main: %d\n", n); } f1() is now called with the value n+1.
14
n =3 Stack frame The computer inspects the top stack frame to identify the value of n as 3. The value 3 is then incremented by one and passed into the function f1().
15
n=4, i new f1() Stack frame As soon as execution of f1() begins a new stack frame is pushed onto the stack containing the variable bindings of f1(). int f1(int n) { int i = f2(n + 1); printf("In f1: %d\n", n); return i; } n=3 main() Stack frame
16
n=4, i new f1() Stack frame During execution of f1() a call to f2() is invoked with the argument n+1. The computer inspects the top stack frame to find the value of n, which is 4. The value 5 is now passed to the function f2(). n=3 main() Stack frame
17
n=4, i f1() Stack frame As soon as execution of f2() begins a new stack frame is pushed onto the stack containing the variable bindings of f2(). int f2(int n) { int j = n * n; printf("In f2: %d\n", n); return j; } n=3 main() Stack frame n=5, j new f2() Stack frame
18
n=4, i f1() Stack frame In f2() the stack frame is inspected to find the value of n and the following output is printed: In f2: 5 The function then returns 25 and the stack frame is popped from the stack. int f2(int n) { int j = n * n; printf("In f2: %d\n", n); return j; } n=3 main() Stack frame n=5, j=25 f2() Stack frame
19
In f1() the stack frame is inspected to find the value of n and the following output is printed: In f1: 4 The function then returns 25 and the stack frame is popped from the stack. int f1(int n) { int i = f2(n + 1); printf("In f1: %d\n", n); return i; } n=3 main() Stack frame n=4, i=25 f1() Stack frame
20
In main() the stack frame is inspected to find the value of n and the following output is printed: In main: 3 void main() { int n = 3; f1(n + 1); printf("In main: %d\n", n); } n=3 main() Stack frame
21
The following example highlights the benefit of using stacks for function invocation, particularly when functions contain variables of the same name. It should also be obvious why elegant recursive solutions do not translate efficiently to computers. The initialisation, pushing and popping of stack frames in a recursive programming solution generates large execution and storage overheads, reducing overall performance.
22
Stack Implementation It has been shown that a stack can be implemented as a list with insertions ( pushes ) and deletions ( pops ) at one end. A push can therefore insert an element at position 0, while a pop can be defined as retrieving the element at position 0 and then deleting it. 231567 Top of Stack Stack using a List implementation
23
The following is a stack class which inherits the properties of the List class: class Stack : public List { public: Stack(); void push(Element); Element pop(); } /* Since stacks are simple lists, simply involve the List constructor */ Stack::Stack() : List() {}
24
void Stack::push(Element e) { /* Rely on the List insert behavior */ insert(e,0); } Element Stack::pop() { Element e; /* Retrieve the first element in the list */ e = retrieve(0); /* Now delete it */ delete(e); /* and return it */ return e; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.