The Stack Data Structure. Classic structure What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO)

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
CSE Lecture 12 – Linked Lists …
Chapter 5.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CS Data Structures II Review COSC 2006 April 14, 2017
COMP 103 Linked Stack and Linked Queue.
Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item.
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
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.
Stacks.
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.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Summary of lectures (1 to 11)
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.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
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.
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,
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Stacks and Queues Introduction to Computing Science and Programming I.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Aggregation/Composition Programming in C++ Fall 2008 Dr. David A. Gaitros
CSC 205 Programming II The ADT Stack. Recap: ADT Abstract Data Type A collection of data (objects) A set of operations on that data Add Remove Retrieve.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
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.
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 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Data Structures and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Homework 4 questions???.
Stacks.
Stack and Queue APURBO DATTA.
Pointers and Linked Lists
Stacks.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
CSC 143 Queues [Chapter 7].
Data Structures and Algorithms
CSC 143 Stacks [Chapter 6].
Pointers & Dynamic Data Structures
Stacks CS-240 Dick Steflik.
template< class T > class Stack { public:
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
LAB#3 Stacks Nora Albabtin nora albabtin.
Stacks, Queues, and Deques
Abstract Data Types Stacks CSCI 240
Stack Implementations
Presentation transcript:

The Stack Data Structure

Classic structure

What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO) Typical Functions Constructor: set data to valid state Push: add data to TOP of stack Pop: delete data at TOP of stack Peek: view or return data at TOP of stack Typical Data Size: total size of stack IsEmpty: is there any data? Top: where is the top of the stack? Linear collection

Why Use a Stack? Usage Constructor creates an empty stack Call push function to add objects, pop function to remove Limited-access container Can only add/remove from top of stack Why??? Useful for Reversing a sequence Managing a series of undoable actions Tracking history (web browsing, undo operations) Prevents making mistakes to protected data The client doesnt have to remember last push to get it back or delete it.

Animations Push and Pop

Stack Underlying Structure Array Linked List

Stack Interface Using an Array #include vector template class MyStack { public: MyStack(); bool isEmpty(); //can use vectors empty() int size();//can use vectors size() void push(Item e); void pop(); Item peek(); private: vector elems; };

Which End of the Array is Top? Push operations: Beginning of the Array?

Which End of the Array is Top? Push operations: Beginning of the Array? Possible, but must move any existing data over to make room for new entriesHARD Push operations: End of the Array?

Which End of the Array is Top? Push operations: Beginning of the Array? Possible, but must move any existing data over to make room for new entriesHARD Push operations: End of the Array? Possible and when space is available no shuffling neededEASY Pop operations: Beginning of the Array?

Which End of the Array is Top? Push operations: Beginning of the Array? Possible, but must move any existing data over to make room for new entriesHARD Push operations: End of the Array? Possible and when space is available no shuffling neededEASY Pop operations: Beginning of the Array? Possible, but must move any existing data up to the topHARD Pop operations: End of the Array?

Which End of the Array is Top? Push operations: Beginning of the Array? Possible, but must move any existing data over to make room for new entriesHARD Push operations: End of the Array? Possible and when space is available no shuffling neededEASY Pop operations: Beginning of the Array? Possible, but must move any existing data up to the topHARD Pop operations: End of the Array? Possible and no shuffling is needed when numUsed is trackedEASY

Which End of the Array is Top? Push operations: End of the Array! Possible and when space is available no shuffling neededEASY Pop operations: End of the Array! Possible and no shuffling is needed when numUsed is trackedEASY

Stack Interface Using a Linked List template class MyStack { public: MyStack(); bool isEmpty(); void push(Item e); void pop(); Item peek(); private: struct cellT{ Item val; cellT *next; }; cellT *head; };

Which End of the List is Top? Push operations: Beginning of the List?

Which End of the List is Top? Push operations: Beginning of the List? We know where the head pointer isEASY Push operations: End of the List?

Which End of the List is Top? Push operations: Beginning of the List? We know where the head pointer isEASY Push operations: End of the List? Possible, but would require traversing the list HARD With a tail pointerEasy Pop operations: Beginning of the List?

Which End of the List is Top? Push operations: Beginning of the List? We know where the head pointer isEASY Push operations: End of the List? Possible, but would require traversing the list HARD With a tail pointerEasy Pop operations: Beginning of the List? We know where the head pointer isEasy Pop operations: End of the List?

Which End of the List is Top? Push operations: Beginning of the List? We know where the head pointer isEASY Push operations: End of the List? Possible, but would require traversing the list HARD With a tail pointerEasy Pop operations: Beginning of the List? We know where the head pointer isEasy Pop operations: End of the List? Possible, but would require traversing the list with a trailing cursorHARD Not made easier with a tail pointer (where is the last node?) Must traverse the list--HARD

Which End of the List is Top? Push operations: Beginning of the List! We know where the head pointer isEASY Pop operations: Beginning of the List! We know where the head pointer isEasy

Client Use of Stack using namespace std; int main() { MyStack s; s.push(1); s.push(2); s.push(3); while (!isEmpty()) cout << s.pop() << endl; return 0; }