The Stack Data Structure Mugurel Ionu Andreica Spring 2012.

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

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Data Structures Mugurel Ionu Andreica Spring 2012.
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 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.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Implementing and Using Stacks
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.
Topic 15 Implementing and Using Stacks
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Chapter 7 Stacks II CS Data Structures I COSC 2006
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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,
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
The Queue Data Structure Mugurel Ionu Andreica Spring 2012.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
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.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
The Linked List Data Structure Mugurel Ionu Andreica Spring 2012.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Click to edit Master text styles Stacks Data Structure.
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.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
ADT Stack What Is a Stack? Standard Template Library Stack Class
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Stacks.
CSE 373: Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Chapter 5 Adapted from Pearson Education, Inc.
CSC 143 Stacks [Chapter 6].
COMPUTER 2430 Object Oriented Programming and Data Structures I
The Heap Data Structure
Jordi Cortadella and Jordi Petit Department of Computer Science
Stacks CS-240 Dick Steflik.
Stack.
C++ STL Stack, Queue, and Deque
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Data Structures and Algorithms 2/2561
Presentation transcript:

The Stack Data Structure Mugurel Ionu Andreica Spring 2012

Operations push(x) –Adds the element x at the top of the stack pop() –Removes the element from the top of the stack and returns it –Returns an error if the stack is empty peek() –Returns (but does not remove) the element at the top of the stack isEmpty() –Returns 1 if the stack is empty and 0 otherwise The axioms are given implicitly – they determine the expected behavior of the stack for a given sequence of operations –See the examples on the upcoming slides

Stack - Example

Stack – Array-based Implementation #include #define NMAX 100 template class Stack { private: T stackArray[NMAX]; int topLevel; public: void push(T x) { if (topLevel >= NMAX - 1) { fprintf(stderr, "Error The stack is full!\n"); return; } topLevel++; stackArray[topLevel] = x; } T pop() { if (isEmpty()) { fprintf(stderr, "Error The stack is empty!\n"); T x; return x; } T x = stackArray[topLevel]; topLevel--; return x; } T peek() { if (isEmpty()) { fprintf(stderr, "Error The stack is empty!\n"); T x; return x; } return stackArray[topLevel]; } int isEmpty() { return (topLevel < 0); } Stack() { // constructor topLevel = -1; } // the stack is empty in the beginning }; int main() { Stack s; s.push(7); s.push(3); s.push(10); s.push(13); s.push(20); s.push(9); s.push(1); printf("%d\n", s.pop()); printf("%d\n", s.peek()); printf("%d\n", s.peek()); printf("%d\n", s.pop()); printf("%d\n", s.peek()); s.push(100); printf("%d\n", s.pop()); return 0; }

Paranthesis Checking Given a string S composed of the characters ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{‘, ‘}’ (round bracket, square bracket and curly bracket), determine if the parantheses are correctly balanced S=“([()[{([]()){}}[[]]]{}])[]({})” is correctly balanced S1=“([]({)})”, S2=“[{{}(())([]{“, S3=“[(){}]]” are not correctly balanced Algorithm –We use a stack –We traverse the string from left to right We push on the stack every open bracket For every closed bracket, the bracket at the top of the stack must be an open bracket of the same type (and the stack must not be empty) => then we pop the open bracket from the stack In the end, the stack must be empty

Paranthesis Checking - Implementation #include // copy & paste the definition of the Stack class char s[200]="([{()[]}(()[{}()])])(){}[{{()[]}()}]"; int error = 0; int main() { Stack stk; for (int i = 0; i < strlen(s); i++) { if (s[i] == '(' || s[i] == '[' || s[i] == '{') stk.push(s[i]); else if (stk.isEmpty()) { fprintf(stderr, "Error Empty stack\n"); error = 1; break; } else { if ((s[i] == ')' && stk.peek() != '(') || (s[i] == ']' && stk.peek() != '[') || (s[i] == '}' && stk.peek() != '{')) { fprintf(stderr, "Error Wrong paranthesis\n"); error = 1; break; } stk.pop(); } if (!error && !stk.isEmpty()) fprintf(stderr, "Error The stack is not empty at the end\n"); else if (!error) printf("OK\n"); return 0; }

Header with the Stack Class Definition Definition of the Stack class must be copy-pasted in every program in which it is used => annoying + prone to mistakes (maybe we do not copy-paste everything, or we copy-paste more than we need) Elegant solution: define the Stack class in a header file (e.g. mystack.h) Any program that uses the Stack class will include the header file –#include “mystack.h” (or #include ) –we will proceed similarly for the upcoming data structures