The unorganized person’s data structure

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Stacks, Queues, and Linked Lists
Stacks Chapter 11.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks1 Stacks The unorganized person’s data structure.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
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 Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Stacks  Introduction  Applications  Implementations  Complex Applications.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures & Algorithms
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
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.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
CC 215 Data Structures Stack ADT
Homework 4 questions???.
Stacks Chapter 7 introduces the stack data type.
Stacks.
Stacks Stack: restricted variant of list
Algorithms and Data Structures
Stack.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
5.1 The Stack Abstract Data Type
Stack and Queues Stack implementation using Array
Jordi Cortadella and Jordi Petit Department of Computer Science
5.3 Implementing a Stack Chapter 5 – The Stack.
5.1 The Stack Abstract Data Type
Presentation transcript:

The unorganized person’s data structure Stacks The unorganized person’s data structure stacks

Stack characteristics Entries are ordered in terms of access -- both insertion and removal take place at same spot (top of stack) Specialized type of container class; defining characteristic is insertion/removal order LIFO = last in, first out; entries are removed in reverse order of insertion stacks

Stack operations Push -- insert item on stack Pop -- remove item from stack Peek -- examine, but don’t remove item stacks

Stack operations Important to know if stack is empty -- attempt to remove an item from an empty stack is an underflow error Depending on implementation, may be necessary to check if stack is full -- attempt to add item to a full stack is an overflow error stacks

Implementation of Stack ADT Stacks can be array based (static or dynamic) or linked list based Invariant for static array implementation: The number of items stored in the stack is found in member variable used Items are stored in member variable data, a static array with the stack bottom at data[0] and the stack top at data[used - 1] stacks

Stack class -- static array version template <class Item> class Stack { public: enum {CAPACITY = 64}; Stack ( ); // default constructor Item pop ( ); // removes top Item Item peek ( ) const; // reveals top Item . . . stacks

Stack ADT continued void push (const Item & entry); // adds Item to stack size_t size ( ) const {return used;} bool is_empty ( ) const {return used == 0;} private: Item data[CAPACITY]; // the stack itself size_t used; // # of items stored in stack }; stacks

Stack function implementations: constructor // Postcondition: empty stack is created template <class Item> Stack<Item>::Stack( ) { used = 0; } stacks

Pop function // Precondition: stack is not empty // Postcondition: top item is removed template <class Item> Item Stack<Item>::pop ( ) { assert (!is_empty( )); used--; return data[used]; } stacks

Peek function // Precondition: stack is not empty // Postcondition: top item is revealed template <class Item> Item Stack<Item>::peek ( ) const { assert (!is_empty( )); return data[used - 1]; } stacks

Push function // Precondition: stack is not full // Postcondition: an item is inserted on stack template <class Item> void Stack<Item>::push(const Item& entry) { assert (size() < CAPACITY); data[used] = entry; used++; } stacks

Stack application examples Compilers use stacks for a variety of purposes: syntax analysis: matching brackets, parentheses, etc. activation records: structures associated with functions, keeping track of local variables, return address, etc. stacks

Example application: balanced parentheses Pseudocode algorithm: scan string left to right if ‘(‘ is encountered, push on stack if ‘)’ is encountered, and stack is not empty, pop one ‘(‘ -- if stack is empty, expression is unbalanced if stack is empty when entire string has been scanned and analyzed, expression is balanced stacks

A program to test for balanced parentheses int main( ) { String user_input; // uses String data type defined in ch. 4 - based on array cout << "Type a string with some parentheses and no white space:\n"; cin >> user_input; if (balanced_parentheses(user_input)) cout << "Those parentheses are balanced.\n"; else cout << "Those parentheses are not balanced.\n"; cout << "That ends this balancing act.\n"; return EXIT_SUCCESS; } stacks

balanced_parentheses function bool balanced_parentheses(const String& expression) // Library facilities used: assert.h, stack1.h, stdlib.h, mystring.h. { // Meaningful names for constants const char LEFT_PARENTHESIS = '('; const char RIGHT_PARENTHESIS = ')'; Stack<char> store; // Stack to store the left parentheses as they occur size_t i; // An index into the String char next; // The next character from the String char discard; // A char popped off the stack and thrown away bool failed = false; // Becomes true if a needed parenthesis is not found . . . stacks

balanced_parentheses continued for (i = 0; !failed && (i < expression.length( )); i++) { next = expression[i]; if (next == LEFT_PARENTHESIS) if (store.size( ) < store.CAPACITY); store.push(next); } else if ((next == RIGHT_PARENTHESIS) && (!store.is_empty( ))) discard = store.pop( ); else if ((next == RIGHT_PARENTHESIS) && (store.is_empty( ))) failed = true; return (store.is_empty( ) && !failed); stacks

Stack ADT as linked list Can make use of toolkit functions to simplify task Stack can grow & shrink as needed to accommodate data -- no fixed size Invariant: stack items are stored in a linked list member variable top is head pointer to list stacks

Class definition for new Stack template <class Item> class Stack { public: Stack( ) { top = NULL; } Stack(const Stack& source); ~Stack( ) { list_clear(top); } ... stacks

Stack definition continued void push(const Item& entry); Item pop( ); void operator =(const Stack& source); size_t size( ) const {return list_length(top);} bool is_empty( ) const {return top == NULL;} Item peek( ) const; private: Node<Item> *top; // Points to top of stack }; stacks

Push function template <class Item> void Stack<Item>::push(const Item& entry) { list_head_insert(top, entry); } stacks

Pop function template <class Item> Item Stack<Item>::pop( ) { assert(!is_empty( )); Item answer = top->data; list_head_remove(top); return answer; } stacks

Peek function template <class Item> Item Stack<Item>::peek( ) const { assert(!is_empty( )); return top->data; } stacks

Copy constructor template <class Item> Stack<Item>::Stack(const Stack<Item>& source) { list_copy(source.top, top); } stacks

Assignment operator template <class Item> void Stack<Item>::operator =(const Stack<Item>& source) { if (source.top== top) // Handle self-assignment return; list_clear(top); list_copy(source.top, top); } stacks

A stack-based calculator Input to program is a fully-parenthesized expression -- examples: ((5.3 * 1.2) / 3.1) (4 - 3) Two stacks are used -- one for operators, one for operands Right parenthesis is signal to pop the stacks and evaluate the expression stacks

Algorithm for expression evaluation Evaluate leftmost, innermost expression; continue evaluating, left to right Read each part of expression Push numbers on operand stack, operators on operator stack When right parenthesis is encountered, pop the stacks, evaluate, and push result on operand stack stacks

Code for stack calculator int main( ) { double answer; cout << "Type a fully parenthesized arithmetic expression:" << endl; answer = read_and_evaluate(cin); cout << "That evaluates to " << answer << endl; return EXIT_SUCCESS; } stacks

Code for stack calculator double read_and_evaluate(istream& ins) { const char DECIMAL = '.'; const char RIGHT_PARENTHESIS = ')'; Stack<double> numbers; Stack<char> operations; double number; char symbol; ... stacks

Code for stack calculator while (!ins.eof( ) && ins.peek( ) != '\n') { if (isdigit(ins.peek( )) || (ins.peek( ) == DECIMAL)) ins >> number; numbers.push(number); } … stacks

Code for stack calculator else if (strchr("+-*/", ins.peek( )) != NULL) { ins >> symbol; operations.push(symbol); } stacks

Code for stack calculator else if (ins.peek( ) == RIGHT_PARENTHESIS) { cin.ignore( ); evaluate_stack_tops(numbers, operations); } else } // end of while loop return numbers.pop( ); stacks

Code for stack calculator void evaluate_stack_tops(Stack<double>& numbers, Stack<char>& operations) { double operand1, operand2; operand2 = numbers.pop( ); operand1 = numbers.pop( ); ... stacks

Code for stack calculator switch (operations.pop( )) { case '+': numbers.push(operand1 + operand2); break; case '-': numbers.push(operand1 - operand2); ... stacks

Code for stack calculator 4/12/2017 Code for stack calculator case '*': numbers.push(operand1 * operand2); break; case '/': numbers.push(operand1 / operand2); } // end switch statement } // end function stacks stacks