CSC 143 Stacks [Chapter 6].

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

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.
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
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.
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.
Stacks.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
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.
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.
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
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
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.
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,
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
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.
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.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
131 3/30/98 CSE 143 More Collection ADTs [Sections ]
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
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.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
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.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Stacks and Queues.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Homework 4 questions???.
Cinda Heeren / Geoffrey Tien
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Building Java Programs Chapter 14
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
Chapter 5 ADTs Stack and Queue.
Pointers and Linked Lists
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks and Queues.
Stack and Queues Stack implementation using Array
CSC 143 Queues [Chapter 7].
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Abstract Data Type Abstract Data Type as a design tool
slides created by Marty Stepp
Jordi Cortadella and Jordi Petit Department of Computer Science
Stacks and Queues: Concepts and Implementations
Stacks CS-240 Dick Steflik.
template< class T > class Stack { public:
Stack.
Dynamic allocation (continued)
Data Structures and Algorithms 2/2561
Stacks and Queues.
Abstract Data Types Stacks CSCI 240
Presentation transcript:

CSC 143 Stacks [Chapter 6]

Collection ADTs We’ll study the following “classic” ADT Stack: ordered collection accessible in LIFO manner Other classics Queue: ordered collection accessible in FIFO manner Set: unordered collection without duplicates Table: unordered mapping of keys to values (e.g. names -> StudentRecords)

Stack ADT Top: Uppermost element of stack, first to be removed Bottom: Lowest element of stack, last to be removed Height or Depth: Number of elements Elements are always inserted and removed from the top (LIFO) Homogeneous collection top ... bottom Stack:

Stack Operations push(item): Adds an element to top of stack, increasing stack height by one pop(): Removes topmost element from stack and return it, decreasing stack height by one top(): Return a copy of topmost element of stack, leaving stack unchanged

Stack Example Show the changes to the stack in the following example: Stack s; s.push(5); s.push(3); s.push(9); int v1 = s.pop(); int v2 = s.top(); s.push(6); s.push(4);

What is the result of: Stack s; s.push(1); s.push(2); int v1 = s.pop(); s.push(3); s.push(4); int v2 = s.pop(); s.push(5); int v3 = s.pop(); int v4 = s.pop(); int v5 = s.pop(); int v6 = s.pop(); v2 v3 v4 v5 v1 v6 2 4 5 3 1 Error!

Common Uses of Stacks Implementing function calls Postfix notation 4 7 + 5 * instead of (4 + 7) * 5 push operands until operator found, pop 2 values, apply operator, push result Backtracking, e.g., to explore paths through a maze

Stack Interface class IntStack { public: IntStack( );// construct empty stack bool isEmpty( ); // = “stack is empty” bool isFull( ); // = “stack is full” void push(int item); // add item to top int pop( ); // remove & return top item int top( ); // return the top item private: . . . };

A Stack Client //Read numbers and print in reverse order void ReverseNumbers() { IntStack s; int k; while ( cin >> k ) { if ( !s.isFull( ) ) s.push(k); } while ( !s.IsEmpty( ) ) cout << s.pop() << endl;

Stack Implementations Many possibilities Array-based Linked-List ... Show one example here; rest left as exercises

Stack Implementation One possibility: store the data in an array class IntStack { public: ... private: const int maxItems = 100;// max # items // in stack int nItems; // # items currently in stack int data[maxItems]; // Items in stack are stored in data[0..nItems-1]. // data[0] is the bottom of the stack; // data[nItems-1] is the top item on the stack };

Stack Implementation (2) // construct empty IntStack IntStack::IntStack() {nItems = 0;} // = “this stack is full” bool IntStack::isFull() { return nItems == maxItems; } // = “this stack is empty” bool IntStack::isEmpty() { return nItems == 0;

Stack Implementation (3) // Push item onto top of this stack // pre: this stack is not full void IntStack::push(int item) { assert (!isFull()); data[nItems] = item; nItems++; } //return a copy of the top item on the stack //pre: this stack is not empty int IntStack::top() { assert (!isEmpty( )); return data[nItems-1];

Stack Implementation (4) // Return the top item on this stack, and // delete that item from the top of the stack. // pre: this stack is not empty int IntStack::pop() { assert (!isEmpty( )); int topVal = data[nItems-1]; nItems--; return topVal; }