1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Data Structures II Review COSC 2006 April 14, 2017
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
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 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
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.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
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.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
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)
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Chapter 3: Arrays, Linked Lists, and Recursion
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques. 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.
Stacks, Queues, and Deques
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Abstract Data Type (ADT) & Stacks
Chapter 3 Introduction to Collections – Stacks Modified
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
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.
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.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
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.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
“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.
Stacks and Queues MR. Mohammad Alqahtani.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Elementary Data Structures
Stacks.
Stacks and Queues.
Queues Queues Queues.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Stacks Abstract Data Types (ADTs) Stacks
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks, Queues, and Deques
Presentation transcript:

1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue Abstract Data Type  Problems with Array Implementation  Dynamic Data Structures LinkedList Node Implementation  Preview: Abstract Data Types III.

2 Lecture 25 A stack Interface in Java Because of its importance, the stack data stucture is included as a “built-in” class in the java.util package of Java. Class java.util.Stack is a structure that stores generic Java object and includes, among others, the methods push(obj), pop(),peek() equivalent to top()), size(), and empty()(equivalent to isEmpty()). Implementing an abstract data type in Java involves two steps. The first step is definition of a Java Application Programming Interface(API), or simply interface, which describes the names of the methods that the ADT supports and how they are to be declared and used. Note that this interface is very general since it specifies that objects of arbitrary, and possible heterogeneous classes can be inserted into the stack

3 Lecture 25 Interface Stack /** * Interface for a stack: a collection of objects * that are inserted and removed according to the * last-in first-out principle. */ public interface Stack { /** number of elements in the stack. */ public int size(); /** true if the stack is empty, false otherwise. */ public boolean isEmpty(); /** top element in the stack. StackEmptyException if the stack is empty. */ public Object top() throws StackEmptyException ; /** * Insert an element at the top of the stack. element element to be inserted. */ public void push (Object element); /** * Remove the top element from the stack. element removed. StackEmptyException if the stack is empty. */ public Object pop() throws StackEmptyException; }

4 Lecture 25 A Stack Interface

5 Lecture 25 StackEmptyException Exception thrown by methods pop() and top() of the Stack interface when called on an empty Stack /** * Runtime exception thrown when one tries to perform operation top or * pop on an empty stack. */ public class StackEmptyException extends RuntimeException { public StackEmptyException(String err) { super(err); }

6 Lecture 25 Stack ADT(A Simple Array-Based Implementation ) Since an array’s size needs to be determined when it is created, one of the important details of our implementation is that we specify some maximum size N for our stack, say, n= 1000 elements. Our stack then consists of an N-element. A StackFullException can be raised if we try to insert a new element and the array S is full.

7 Lecture 25 An Array-Based Stack(algorithm)

8 Lecture 25 Array-based Java Implementation of the Stack interface /** * Implementation of the Stack interface using a fixed-length array. * An exception is thrown if a push operation is attempted when the size of * the stack is equal to the length of the array. */ public class ArrayStack implements Stack { /** * Default length of the array used to implement the stack. */ public static final int CAPACITY = 1000; /** * Lenght of the array used to implement the stack. */ private int capacity; /** * Array used to implement the stack. */ private Object S[]; /** * Index of the top element of the stack in the array. */ private int top = -1; /** * Initialize the stack to use an array of default length CAPACITY. */ public ArrayStack() { this(CAPACITY); } /** * Initialize the stack to use an array of given length. * cap length of the array. */ public ArrayStack(int cap) { capacity = cap; S = new Object[capacity]; } /** * O(1) time. */ public int size() { return (top + 1); }

9 Lecture 25 Array-based Java Implementation of the Stack interface /** * O(1) time. */ public boolean isEmpty() { return (top < 0); } /** * O(1) time. StackFullException if the array is full. */ public void push(Object obj) throws StackFullException { if (size() == capacity) throw new StackFullException("Stack overflow."); S[++top] = obj; } /** * O(1) time. */ public Object top() throws StackEmptyException { if (isEmpty()) throw new StackEmptyException("Stack is empty."); return S[top]; } /** * O(1) time. */ public Object pop() throws StackEmptyException { Object elem; if (isEmpty()) throw new StackEmptyException("Stack is Empty."); elem = S[top]; S[top--] = null; // dereference S[top] for garbage collection. return elem; }

10 Lecture 25 Casting with a Generic Stack. Example method that reverse elements One of the strong points of our realization of the stack ADT by means of a Java class that implements the Stack interface is that we can store generic objects in the stack, each belonging to an arbitrary class. An ArrayStack in the previous slide can store Integer objects, Student objects or any object. When we retrieve an object from the stack(with either the top or pop method) we always get back a reference of type Object, no matter what the specific class of the object is. Thus in order to use the retrieved element as an instance of the specific class it really belongs to, we must perform a cast, which forces the object to be viewed as a member of a specific class rather than a more general superclass Object. public static Integer[] reverse(Integer[] a) { ArrayStack S = new ArrayStack(a.length); Integer[] b = new Integer[a.length]; for (int i=0; i < a.length; i++) S.push(a[i]); for (int i=0; i < a.length; i++) b[i] = (Integer) (S.pop()); return b; } //Casting is performed to force the object returned by pop to viewed as an Integer object

11 Lecture 25 Queue Abstract Data Type r  (r+1)mod N Using array Q in a circular fashion:”Wrapped around” Normal configuration

12 Lecture 25 A Queue Interface public interface Queue { /** number of elements in the queue. */ public int size(); /** true if the queue is empty, false otherwise. */ public boolean isEmpty(); /** * Inspect the element at the front of the queue. * element at the front of the queue. QueueEmptyException if the queue is empty. */ public Object front() throws QueueEmptyException ; /** * Insert an element at the rear of the queue. * element new element to be inserted. */ public void enqueue (Object element); /** * Remove the element at the front of the queue. * element removed. QueueEmptyException if the queue is empty. */ public Object dequeue() throws QueueEmptyException ; }

13 Lecture 25 Problems with Array Implementation  when implementing data structures using arrays, we need to be careful with the size of array chosen:  too small : unsuitable for the application.  too big : wasteful of space.  performance : can be an issue for certain operations because of need to move array elements around.  An alternative is to use a dynamic list structure which is able to grow and shrink as the application demands:  no estimate of size is required upfront.  Does not suffer from performance problems outlined earlier with an array implementation.  These structures are also known as linked lists

14 Lecture 25 Inserting/Deleting an Element( Problems with Array) Insert 5 here n Move elements from position 1 up one place Delete this n Move elements from position 3 down one place 305

15 Lecture 25 Dynamic Data Structures So far we have studied fixed-size data structures such as single-subscripted arrays Here we are introducing dynamic data structures that grow and shrink during execution. A linked list is based on the concept of a self- referential object -- an object that refers to an object of the same class. Self-referential class objects can be linked together to form useful data structures such as lists, stacks, queues, and treesSelf-referential class objects can be linked together to form useful data structures such as lists, stacks, queues, and trees

16 Lecture 25 The Linked List Data Structure l A linked list is based on the concept of a self-referential object -- an object that refers to an object of the same class. public class Node { private String name; private Node next; } A link to another Node object. Data LinkReferenceNull reference Nuri OmerSami Node

17 Lecture 25 Dynamic Linked List Structures  Each node consists of a data element and reference which points to the next node in the list.  The last node reference is initialised to a null value and this is used to terminate the list. Implementation Issues  If a header reference is used to point to the 1st list node:  insertion and deleting first element requires the header to be updated to reflect the changes to the start of the list.  deleting in general requires that we keep track of the node in front of the one to be deleted so that the links can be updated. header elementnext List Nodes elementnextelementnext

18 Lecture 25 Solution - Using a Dummy Header Node  Using a dummy header node solves the problems outlined earlier with a header reference.  To solve deletion problems we need to provide a findPrevious method which returns a reference to the predecessor of the node we wish to delete.  If we delete the first node then findPrevious will return the header node whose next reference can be updated.  Inserting a new first node only requires that the header next reference be updated to point to the new first node. dummy header elementnext List Nodes elementnextelementnext

19 Lecture 25 Singly-linked list The singly-linked list is the most basic of all the linked data structures. A singly-linked list is simply a sequence of dynamically allocated objects, each of which refers to its successor in the list. Despite this obvious simplicity, there are myriad implementation variations. The figure below shows several of the most common singly-linked list variants. head (a) tail (d) head sentinel (c) head tail (b)

20 Lecture 25 Linked List (Cont’d) The basic singly-linked list is indicated in the above figure by (a). Each element of the list refers to its successor and the last element contains the null reference. One variable, labeled head, is used to keep track of the list. The basic singly-linked list is inefficient in those cases when we wish to add elements to both ends of the list. While it is easy to add elements at the head of the list, to add elements at the other end we need to locate the last element. If the basic basic singly-linked list is used, the entire list needs to be traversed in order to find its tail. The list labeled (b) shows a way in which to make adding elements to the tail of a list more efficient. The solution uses a second variable, tail, which refers to the last element of the list. Of course, this time efficiency comes at the cost of the additional space used to store the variable tail. head (a) head tail (b)

21 Lecture 25 Linked List (Cont’d) The singly-linked list labeled (c) illustrates two common programming tricks. There is an extra element at the head of the list called a sentinel. This element is never used to hold data and it is always present. The principal advantage of using a sentinel is that it simplifies the programming of certain operations. For example, since there is always a sentinel standing guard, we never need to modify the head variable. Of course, the disadvantage of a sentinel such as shown is that extra space is required, and the sentinel needs to be created when the list is initialized. The list (c) is also a circular list. Instead of using a null reference to demarcate the end of the list, the last element of the list refers to the sentinel. The advantage of this programming trick is that insertion at the head of the list, insertion at the tail of the list, and insertion at an arbitrary position of the list are all identical operations. head sentinel (c)

22 Lecture 25 Linked List (Cont’d) Of course, it is also possible to make a circular, singly-linked list that does not use a sentinel. The list labeled (d) shows a variation in which a single variable is used to keep track of the list, but this time the variable, tail, refers to the last element of the list. Since the list is circular in this case, the first element follows the last element of the list. Therefore, it is relatively simple to insert both at the head and at the tail of this list. This variation minimizes the storage required, at the expense of a little extra time for certain operations. tail (d) (e) Doubly Linked List

23 Lecture 25 LinkedList Node Implementation  The structure is self-referential in nature as the reference which is part of the class is also capable of referring to a ListNode (i.e. itself).  A node class should also provide an appropriate set of constructors and node interface methods. public class Node { private Object element; // Object type stored in this node private Node next; // reference to another node // class methods go here }  If an abstract data type is to be truly abstract then the type of the element that it is used to manage should be irrelevant.  We will therefore store instances of the base Java Object type in our node which is represented by a Java class as follows. click here on this link Linked List Linked List