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.

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
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
CS Data Structures II Review COSC 2006 April 14, 2017
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
Stacks, Queues, and Deques
CMPT 225 Stacks.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
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.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Stacks.
Stacks, Queues & Deques CSC212.
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.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
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.
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.
© 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.
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.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
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.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Sections 3.4 Formal Specification
Stack: a Linked Implementation
CC 215 Data Structures Queue ADT
CC 215 Data Structures Stack ADT
Stacks.
Stacks and Queues.
Stack and Queue APURBO DATTA.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
ADT list.
Elementary Data Structures
Stacks: Implemented using Linked Lists
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
Stacks CS-240 Dick Steflik.
Ch. 7 Stack 도입을 위한 예 “←” in keyboard input line
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
CHAPTER 3: Collections—Stacks
Presentation transcript:

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 out data structure all insertions are made at the top of the stack all removals are also made from the top of the stack Common uses of stacks: can used to reverse the order of input used to perform balanced symbol checking ( ) { } used to control program execution with function calls used for expression evaluation

3 ADT Stack Operations isEmpty boolean -determines whether stack is empty. push adds one item to the top of the stack. pop removes one item from the top of the stack. peek returns most recently inserted item. popAll removes all items from stack. isFull- boolean often not needed.

4 Linked List Stack Implementation Stacks can grow and shrink dynamically No particular linked list variations are required: The head pointer can also serve as the top of the stack. Pushing becomes inserting the first node in a linked list. Popping becomes removing the first node. All basic operations take place in O(1) time - or constant time complexity

5 Stack Interface public interface StackInterface { public boolean isEmpty(); // Determines whether the stack is empty. // Precondition: None. // Postcondition: Returns true if the stack is empty; otherwise returns false. public boolean isFull(); // Determines whether the stack is full. // Precondition: None. // Postcondition: Returns true if the stack is full; otherwise returns false. public void push(Object newItem) throws StackException; // Adds an item to the top of a stack. // Precondition: newItem is the item to be added. // Postcondition: If insertion is successful, newItem is on the top of the stack. // Exception: Some implementations may throw // StackException when newItem cannot be placed on the stack.

6 Stack Interface (2) public Object pop() throws StackException; // Removes the top of a stack. // Precondition: None. // Postcondition: If the stack is not empty, the item // that was added most recently is removed from the stack and returned. // Exception: Throws StackException if the stack is empty. public void popAll(); // Removes all the items from the stack. // Precondition: None. // PostCondition: Stack is empty. public Object peek() throws StackException; // Retrieves the top of a stack. // Precondition: None. // Postcondition: If the stack is not empty, the item that was added most //recently is returned. The stack is unchanged. // Exception: Throws StackException if the stack is empty. } // end StackInterface

7 List Stack Implementation // Assumes that the classes ListInterface and Node are available public class ListStack implements StackInterface { private Node top; // class Node is the same used in the LinkedList class except it stores // Objects rather than Comparables public ListStack() { top = null; } // end default constructor public boolean isEmpty() { return (top == null); } // end isEmpty public boolean isFull() { return false; } // end isFull

8 List Stack Implementation (2) public void push(Object newItem) { if (!isFull()) { Node temp = new Node(newItem); temp.setNext(top); top = temp; } else { throw new StackException("StackException on " + "push: stack full"); } // end if } // end push public Object pop() throws StackException { if (!isEmpty()) { Object temp = top.getItem(); top = top.getNext(); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop

9 List Stack Implementation (3) public void popAll() { while(!isEmpty()) pop(); } // end popAll public Object peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek } // end ListStack

10 Stack Exception public class StackException extends java.lang.RuntimeException { public StackException(String s) { super(s); } // end constructor } // end StackException

11 List Stack Test public class ListStackTest { public static void main(String[ ] args) { Integer items[] = new Integer[15]; System.out.println("Pushing:"); for (int i=0; i<15; i++) { items[i] = new Integer(i); if (!stack.isFull()) { System.out.print(" "+i); stack.push(items[i]); } // end if } // end for System.out.println("\nPopping:"); while (!stack.isEmpty()) { // cast result of pop to Integer System.out.print(" "+(Integer)(stack.pop())); } // end while System.out.println(); } // end main }

12 List Stack Test Output Pushing: Popping:

13 Array Stack Implementation public class ArrayStack implements StackInterface { final int MAX_STACK = 50; // maximum size of stack private Object items[ ]; private int top; public ArrayStack() { items = new Object[MAX_STACK]; top = -1; } // end default constructor public boolean isEmpty() { return top < 0; } // end isEmpty public boolean isFull() { return top == MAX_STACK-1; } // end isFull

14 Array Stack Implementation (2) public void push(Object newItem) throws StackException { if (!isFull()) { items[++top] = newItem; } else { throw new StackException("StackException on " + "push: stack full"); } // end if } // end push public void popAll() { items = new Object[MAX_STACK]; top = -1; } // end popAll

15 Array Stack Implementation (3) public Object pop() throws StackException { if (!isEmpty()) { return items[top--]; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop public Object peek() throws StackException { if (!isEmpty()) { return items[top]; } else { throw new StackException("Stack exception on " + "peek - stack empty"); } // end if } // end peek } // end ArrayStack

16 Array Stack Test public class ArrayStackTest { public static final int MAX_ITEMS = 15; public static void main(String[ ] args) { ArrayStack stack = new ArrayStack(); Integer items[ ] = new Integer[MAX_ITEMS]; System.out.println("Pushing:"); for (int i=0; i<MAX_ITEMS; i++) { items[i] = new Integer(i); if (!stack.isFull()) { System.out.print(" "+i); stack.push(items[i]); } // end if } // end for System.out.println("\nPopping:"); while (!stack.isEmpty()) { // cast result of pop to Integer System.out.print(" "+(Integer)(stack.pop())); } // end while System.out.println(); } // end main } // end ArrayStackTest

17 Array Stack Test Output Pushing: Popping: