Stacks.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures1 Basic Data Structures Elementary Structures Arrays Lists Search Structures Binary search Tree Hash Tables Sequence Structures Stacks Queues.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: 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.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
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.
Stacks.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
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.
Chapter 7 Stacks II CS Data Structures I COSC 2006
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.
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.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
The Daikon system for dynamic detection of likely invariants MIT Computer Science and Artificial Intelligence Lab. 16 January 2007 Presented by Chervet.
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,
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 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,
Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types (ADTs) Lists Stacks Queues.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Object-Oriented Programming Simple Stack Implementation.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
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.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Chapter 3 Stack and Queue 3.3 The Stack ADT Stack Model A stack is a list in which insertions and deletions take place at the same end. This end.
Section 3.7 Linked-Based Implementations
Sections 3.4 Formal Specification
Stack: a Linked Implementation
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Animationer ur Weiss kap. 16 Figurerna 16.20, 16.25
CS 201 Data Structures and Algorithms
Week 3 - Friday CS221.
Stacks.
CS201: Data Structures and Discrete Mathematics I
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Stacks Linked Lists Queues Heaps Hashes
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Instructor: Mr.Vahidipour
Deques, Stacks and Queues
Circular List removedNode General Case Single Node Case lastNode aNode
ADT list.
ساختمان داده ها پشته ها Give qualifications of instructors: DAP
Introduction to Programming
Stacks: Implemented using Linked Lists
Stacks and Queues 1.
Abstract Data Type Abstract Data Type as a design tool
CE 221 Data Structures and Algorithms
UNIT-II.
Stacks CS-240 Dick Steflik.
Lecture 16 Stacks and Queues CSE /26/2018.
CMSC 341 Stacks and Queues 4/17/2019.
CMSC 341 Stacks and Queues 4/17/2019.
CMSC 341 Stacks and Queues 4/29/2019.
CMSC 341 Lecture 7.
Lecture 16 Stacks and Queues CSE /26/2018.
Presentation transcript:

Stacks

Data Structure: Stacks Outline Linked-list Implementation Array Implementation Applications 4/26/2019 Data Structure: Stacks

Data Structure: Stacks Stack ADT 2 List A1, A2, …, AN If N=0, it is an empty stack. Methods Push: add the element on the top of stack Pop: take the element from the top of stack Top: read the element on the top of stack pop push topOfStack 7 1 8 3 4 4/26/2019 Data Structure: Stacks

Linked-list Implementation public class StackLi { public StackLi() { topOfStack=null; } public boolean isFull() { return false; } public void makeEmpty() { topOfStack=null; } ... private ListNode topOfStack; } A5 topOfStack A4 A3 A2 A1 4/26/2019 Data Structure: Stacks

Data Structure: Stacks Method push, pop, top public void push(object x) { topOfStack = new ListNode(x, topOfStack); } public void pop() throws Underflow { if (isEmpty()) throw new Underflow(); topOfStack = topOfStack.next; public Object top() { if (isEmpty()) return null; return topOfStack.element; 4/26/2019 Data Structure: Stacks

Data Structure: Stacks Method topAndPop public Object topAndPop() { if (isEmpty()) return null; Object topItem = topOfStack.element; topOfStack = topOfStack.next; return topItem; } 4/26/2019 Data Structure: Stacks

Data Structure: Stacks Array Implementation public class StackAr { public StackAr() { this(DEFAULT_SIZE); } public StackAr(int size) { theArray = new Object [size]; topOfStack=-1;} public boolean isEmpty() { return topOfStack==-1; } public boolean isFull() { return topOfStack== theArray.length-1;} ... private Object [] theArray; private int topOfStack; static final int DEFAULT_SIZE=10; } 6 5 A6 4 A5 3 A4 2 A3 1 A2 A1 topOfStack=5 4/26/2019 Data Structure: Stacks

Data Structure: Stacks Method push, pop, top public void push(object x) throws Overflow { if (isFull())throw new Overflow(); theArray[++topOfStack] = x; } public void pop() throws Underflow { if (isEmpty()) throw new Underflow(); theArray[topOfStack--] = null; public Object top() { if (isEmpty()) return null; return theArray[topOfStack]; 4/26/2019 Data Structure: Stacks

Data Structure: Stacks Method topAndPop public Object topAndPop() { if (isEmpty()) return null; Object topItem = theArray[topOfStack]; theArray[topOfStack--] = null; return topItem; } 4/26/2019 Data Structure: Stacks

Application:Balancing Parenthesis ( ( ( ) ( ) ( ( ) ( ) ) ) ( ) ) StackLi symStk=new StackLi; Object sym=nextsym(); while (sym!=null) { if (sym.isOpenPar()) symStk.push(sym); if (sym.isClsPar()) if (!symStk.isEmpty()) symStk.pop(); else return false; sym=nextsym(); } return symStk.isEmpty(); ( ( ( ( 4/26/2019 Data Structure: Stacks