1 Data Structures CSCI 132, Spring 2016 Notes 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
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 Abstract Data Types. Objectives To appreciate the concept and purpose of abstract data types, or ADTs To understand both the abstract behavior and the.
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.
Data Structures Chapter 4 Linked Stacks and Queues Andreas Savva.
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.
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.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
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.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
1 Data Structures CSCI 132, Spring 2016 Notes13 Queues as Linked Lists, Polynomial Arithmetic.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
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 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.
Popping Items Off a Stack Using a Function Lesson xx
Programming Abstractions
Stacks Chapter 7 introduces the stack data type.
CO4301 – Advanced Games Development Week 2 Introduction to Parsing
לולאות קרן כליף.
5.1 The Stack Abstract Data Type
تهیه کنندگان مهری بابائی،گیتا جوادی رضا امید ملایری ،ناصر بهمدی
Popping Items Off a Stack Lesson xx
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
Stacks Data structure Elements added, removed from one end only
Jordi Cortadella and Jordi Petit Department of Computer Science
Visit for more Learning Resources
Stack.
5.3 Implementing a Stack Chapter 5 – The Stack.
5.1 The Stack Abstract Data Type
Presentation transcript:

1 Data Structures CSCI 132, Spring 2016 Notes 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) { //we will implement this method in class }

4 Implementing pop( ) Error_code Stack ::pop() { //we will implement this method in class }

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

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

7 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.

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

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

11 Processing commands void do_command(char command,Stack &numbers) { double p,q ; switch (command){ case '?': break; case '=': break;

12 More number processing case '+': break; //Add options for further user commands. } //end switch } //end do_command