1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

The unorganized person’s data structure
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
Pseudocode for Bracket matching Stack mystack; // declare & initialize stack Loop through all characters in program { if (symbol is an ‘opener’) // (,
Chapter 2 INTRODUCTION TO STACKS. Outline 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
Stacks.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
Object Oriented Data Structures
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Object-Oriented Programming Simple Stack Implementation.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Chapter 2 INTRODUCTION TO STACKS 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket Matching.
1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists.
Data Structures & Algorithms
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
Reverse Polish Notation Written by J.J. Shepherd.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
Prof. Qing Wang Lecture 5 Stack and Queue Lecture Notes: Data Structures and Algorithms.
1 Data Structures CSCI 132, Spring 2016 Notes13 Queues as Linked Lists, Polynomial Arithmetic.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
Stacks Chapter 7 introduces the stack data type.
לולאות קרן כליף.
تهیه کنندگان مهری بابائی،گیتا جوادی رضا امید ملایری ،ناصر بهمدی
Popping Items Off a Stack Lesson xx
Stack and Queues Stack implementation using Array
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stacks as Linked lists top_node typedef Stack_entry Node_entry;
Introduction to Programming
Introduction to Programming
CSC 143 Stacks [Chapter 6].
COMPUTER 2430 Object Oriented Programming and Data Structures I
Jordi Cortadella and Jordi Petit Department of Computer Science
template< class T > class Stack { public:
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks

2 Recall the Stack class typedef int Stack_entry; const int maxstack =10 ;//small value for testing class Stack { public: Stack(); bool empty()const; Error_code pop(); Error_code top(Stack_entry &item)const; Error_code push(const Stack_entry &item); private: int count ; Stack_entry entry [maxstack ]; };

3 Implementing push( ) Error_code Stack ::push(const Stack_entry &item) { //Implement push() here }

4 Implementing push( ) Error_code Stack ::push(const Stack_entry &item) { Error_code outcome =success ; if (count >=maxstack) { outcome =overflow ; } else { entry [count ]=item ; count++; } return outcome ; }

5 Implementing pop( ) Error_code Stack ::pop() { }

6 Implementing pop( ) Error_code Stack ::pop() { Error_code outcome =success ; if (count ==0) { outcome =underflow ; } else { count-- ; } return outcome ; }

7 Implementing top( ) Error_code Stack ::top(Stack_entry &item) const { //implement top( ) }

8 Implementing top( ) Error_code Stack ::top(Stack_entry &item) const { Error_code outcome =success ; if (count ==0){ outcome =underflow ; } else { item =entry [count - 1]; } return outcome ; }

9 Implementing empty( ) bool Stack ::empty( )const { //implement empty( ) }

10 Implementing empty( ) bool Stack ::empty( )const { bool outcome =true; if (count >0) { outcome =false; } return outcome ; }

11 Implementing Stack( ) Stack ::Stack() {//implement Stack( ) }

12 Implementing Stack( ) Stack ::Stack() { count =0 ; }

13 Reverse Polish Calculator: A stack application Numbers are pushed onto a stack as they are entered. An arithmetic operation is done on the top two items in the stack, which are popped off the stack. The result is pushed onto the stack. Reverse polish arithmetic allows you to do any arithmetic calculation without using parentheses. For example, the computation (6 + 2) is written as in reverse polish notation.

14 Example Regular notation: (6 + 2) / (7 - 3) Reverse Polish: /

15 Rules for Calculator use User enters a command: ?, =, +, -, *, or / ? means the next entry is a number. = means print the result (top of stack). All others mean perform the given arithmetic operation.

The main calculator program #include //stack class from the Standard Template Library int main (void) { char command ; Stack stored_numbers; cout <<"Enter a command: "; cin >> command; command = tolower(command); while (command != 'q'){ if (command == '?'||command == '='||command == '+' || command == '-'||command == '*'||command == '/' ) { do_command(command, stored_numbers); } else { cout <<"Please enter a valid command:"<<endl <<"[?]push to stack [=]print top "<<endl <<"[+][-][*][/]are arithmetic operations "<<endl <<"[Q ]uit."<<endl; } cout << "Enter a command: "; cin >> command; command = tolower(command); } }

17 Processing commands void do_command(char command,Stack &numbers) { double p,q ; switch (command){ case '?': break; case '=': break; cout <<"Enter a real number:" ; cin >>p ; if (numbers.push(p)==overflow) { cout <<"Warning:Stack full,lost number "<<endl; } if (numbers.top(p) == underflow) { cout << "Stack empty." << endl; } else { cout << p <<endl; }

18 More number processing case '+': break; //Add options for further user commands. } //end switch } //end do_command if (numbers.top(p)==underflow){ cout <<"Stack empty "<<endl ; } else { numbers.pop(); if (numbers.top(q)==underflow){ cout <<"Stack has just one entry "<<endl ; numbers.push(p); } else { numbers.pop(); if (numbers.push(q + p)==overflow) { cout <<"Warning:Stack full,lost result "<<endl ; }