Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.

Similar presentations


Presentation on theme: "Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of."— Presentation transcript:

1

2 Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of Computer Science California State University, Fresno Fall 2006

3 Introduction to Data Structure, Fall 2006 Slide- 2 California State University, Fresno Stacks A stack is a sequence of items that are accessible at only one end of the sequence.

4 Introduction to Data Structure, Fall 2006 Slide- 3 California State University, Fresno Pushing/Popping a Stack Because a pop removes the item last added to the stack, we say that a stack has LIFO (last-in/first-out) ordering.

5 Introduction to Data Structure, Fall 2006 Slide- 4 California State University, Fresno stack(); Create an empty stack. bool empty(); Check whether the stack is empty. Return true if it is empty and false otherwise. int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition:The stack is not empty. const T& top() const; Constant version of top(). Stack Operations

6 Introduction to Data Structure, Fall 2006 Slide- 5 California State University, Fresno void pop(); Remove the item from the top of the stack. Precondition:The stack is not empty. Postcondition: Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top. Stack Operations

7 Introduction to Data Structure, Fall 2006 Slide- 6 California State University, Fresno Using a Stack to Create a Hex Number

8 Introduction to Data Structure, Fall 2006 Slide- 7 California State University, Fresno Uncoupling Stack Elements

9 Introduction to Data Structure, Fall 2006 Slide- 8 California State University, Fresno Uncoupling Stack Elements

10 Introduction to Data Structure, Fall 2006 Slide- 9 California State University, Fresno Uncoupling Stack Elements

11 Introduction to Data Structure, Fall 2006 Slide- 10 California State University, Fresno Uncoupling Stack Elements

12 Introduction to Data Structure, Fall 2006 Slide- 11 California State University, Fresno Uncoupling Stack Elements

13 Introduction to Data Structure, Fall 2006 Slide- 12 California State University, Fresno Uncoupling Stack Elements

14 Introduction to Data Structure, Fall 2006 Slide- 13 California State University, Fresno Stack - Copying s.pop(); while(!s.empty()) s.pop(); s.push(a[7]); int i=0; while(i++<7) s.push(a[7-i]);

15 Introduction to Data Structure, Fall 2006 Slide- 14 California State University, Fresno Stack - Reversal s.pop(); while(!s.empty()) s.pop(); s.push(a[0]); int i=0; while(i++<7) s.push(a[i]);

16 Introduction to Data Structure, Fall 2006 Slide- 15 California State University, Fresno Stack – Traversal & Search if (s.pop() != 6) s.pop(); while(!s.top() !=6) s.pop();

17 Introduction to Data Structure, Fall 2006 Slide- 16 California State University, Fresno Stack – Insertion if (s.pop() < 6) { s1.push(s.top()); s.pop(); } while(!s.top() < 6) { s1.push(s.top()); s.pop(); } while(!s.top() < 6) { s1.push(s.top()); s.pop(); } s.push(6); while(!s.top() < 6) { s1.push(s.top()); s.pop(); } s.push(6); while(!s1.empty()) { int x = s1.top(); s.push(x); }

18 Introduction to Data Structure, Fall 2006 Slide- 17 California State University, Fresno 1.How to access min() with O(1), i.e., constant time? 2.Postfix evaluation 3.Memory management Stack Problem

19 Introduction to Data Structure, Fall 2006 Slide- 18 California State University, Fresno System Stack for Recursion


Download ppt "Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of."

Similar presentations


Ads by Google