Recap: Solution of Last Lecture Activity

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
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.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Queues What is a queue? Queue Implementations: –As Array –As Circular Array –As Linked List Applications of Queues. Priority queues.
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.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
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.
Implementing and Using Stacks
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Queues What is a Queue? Queue Implementations: Queue As Array
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks top push (8)push (2)
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Information and Computer Sciences University of Hawaii, Manoa
© 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.
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.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
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.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
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.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Stacks (and Queues).
Data Structures Using C++ 2E
Queues.
Unit 4 Stacks And Queues King Fahd University of Petroleum & Minerals
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Queues What is a queue? Queue Implementations: As Array
COMPUTER 2430 Object Oriented Programming and Data Structures I
Introduction to Data Structures
Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Chapter 7 Stack.
Stacks.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Recall What is a Data Structure Very Fundamental Data Structures
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Queues What is a Queue? Queue Implementations: As Array
Stacks.
Introduction to Stacks
Introduction to Stacks
Stacks.
Queues What is a queue? Queue Implementations: As Array
CHAPTER 3: Collections—Stacks
Presentation transcript:

Recap: Solution of Last Lecture Activity //reversing a singly linked list if(head ! = tail){ tail =head; tmp1 = head.next; head.next =null; while(tmp1 != null){ tmp2=head; head = tmp1; tmp1=head.next; head.next=tmp2; } Exercise: can you develop a recursive method for reversing a singly linked list

Stacks Introduction An example of a stack Stack implementation using arrays. using linked lists. Applications of stacks

Introduction Stack is a data structure in which data is added and removed at only one end called the top. To add (i.e. push) an item to the stack, it must be placed on the top of the stack. To remove (i.e. pop) an item from the stack, it must be removed from the top of the stack too. Thus, the last element that is pushed into the stack is the first element to be popped out of the stack. i.e., stack is a Last In First Out (LIFO) data structure

An example of a stack top top top Push(8) Push(2) pop() top top top 1 7 8 1 7 2 top 1 7 2 top top Push(8) Push(2) pop() 8 1 7 2 1 7 2 top 7 2 top top pop() pop()

Stack Implementations public interface Stack { public Object getTop(); public void push(Object obj); public Object pop(); } In our implementation, a stack is a container that extends the AbstractContainer class and implements the Stack interface. Two implementations: StackAsArray The underlying data structure is an array of Object StackAsLinkedList The underlying data structure is an object of MyLinkedList

StackAsArray – Constructor In the StackAsArray implementation that follows, the top of the stack is array[count – 1] and the bottom is array[0]: The constructor’s single parameter, size, specifies the maximum number of items that can be stored in the stack. The variable array is initialized to be an array of length size. public class StackAsArray extends AbstractContainer implements Stack { protected Object[] array; public StackAsArray(int size){ array = new Object[size]; } // …

StackAsArray – purge() Method The purpose of the purge method is to remove all the contents of a container. To empty the stack, the purge method simply assigns the value null to the first count positions of the array. public void purge(){ while (count > 0) array[--count] = null; } Complexity is O(n)

StackAsArray – push() Method push() method adds an element at the top the stack. It takes as argument an Object to be pushed. It first checks if there is room left in the stack. If no room is left, it throws a ContainerFullException exception. Otherwise, it puts the object into the array, and then increments count variable by one. public void push(Object object){ if (count == array.length) throw new ContainerFullException(); else array[count++] = object; } Complexity is O(1)

StackAsArray – pop() Method The pop method removes an item from the stack and returns that item. The pop method first checks if the stack is empty. If the stack is empty, it throws a ContainerEmptyException. Otherwise, it simply decreases count by one and returns the item found at the top of the stack. public Object pop(){ if(count == 0) throw new ContainerEmptyException(); else { Object result = array[--count]; array[count] = null; return result; } Complexity is O(1)

StackAsArray – getTop() Method getTop() method first checks if the stack is empty. getTop() method is a stack accessor which returns the top item in the stack without removing that item. If the stack is empty, it throws a ContainerEmptyException. Otherwise, it returns the top item found at position count-1. public Object getTop(){ if(count == 0) throw new ContainerEmptyException(); else return array[count – 1]; } Complexity is O(1)

StackAsArray – iterator() Method public Iterator iterator() { return new Iterator() { private int position = count-1; public boolean hasNext() { return position >=0; } public Object next () { if(position < 0) throw new NoSuchElementException(); else return array[position--]; };

StackAsLinkedList Implementation public class StackAsLinkedList extends AbstractContainer implements Stack { protected MyLinkedList list; public StackAsLinkedList(){ list = new MyLinkedList(); } public void purge(){ list.purge(); count = 0; // …

StackAsLinkedList Implementation (Cont.) public void push(Object obj){ list.prepend(obj); count++; } public Object pop(){ if(count == 0) throw new ContainerEmptyException(); else{ Object obj = list.getFirst(); list.extractFirst(); count--; return obj; public Object getTop(){ else return list.getFirst(); Complexity is O(1) Complexity is O(1) Complexity is O(1)

StackAsLinkedList Implementation (Cont.) public Iterator iterator() { return new Iterator() { private MyLinkedList.Element position = list.getHead(); public boolean hasNext() { return position != null; } public Object next() { if(position == null) throw new NoSuchElementException(); else { Object obj = position.getData(); position = position.getNext(); return obj; };

Applications of Stack Some direct applications: Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual Machine Evaluating postfix expressions Some indirect applications Auxiliary data structure for some algorithms Component of other data structures

Application of Stack - Evaluating Postfix Expression (5+9)*2+6*5 An ordinary arithmetical expression like the above is called infix-expression (i.e. binary operators appear between their operands) The order of operations evaluation is determined by the precedence rules. When an evaluation order is desired that is different from that provided by the precedence, parentheses are used to override precedence rules.

Application of Stack - Evaluating Postfix Expression Expressions can also be represented using postfix notation (i.e. each operator comes after its two operands). The advantage of postfix notation is that the order of operation evaluation is unique without the need for precedence rules or parenthesis. Postfix Infix 16 2 / 16 / 2 2 14 + 5 * (2 + 14)* 5 2 14 5 * + 2 + 14 * 5 6 2 - 5 4 + * (6 – 2) * (5 + 4)

Application of Stack - Evaluating Postfix Expression The following algorithm uses a stack to evaluate a postfix expression. Start with an empty stack for (each item in the expression) { if (the item is a number) Push the number onto the stack else if (the item is an operator){ Pop two operands from the stack Apply the operator to the operands Push the result onto the stack } Pop the only one number from the stack – that’s the result of the evaluation

Application of Stack - Evaluating Postfix Expression Example: Consider the postfix expression, 2 10 + 9 6 - /, which is (2 + 10) / (9 - 6) in infix, the result of which is 12 / 3 = 4. The following is a trace of the postfix evaluation algorithm for the above.