1 Lecture 26 Abstract Data Types –III Overview  Creating and manipulating Linked List.  Linked List Traversal.  Linked List Traversal using Iterator.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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
COMP 103 Linked Stack and Linked Queue.
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
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.
Data Structures and Applications Hao Jiang Computer Science Department Boston College.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
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.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
TCSS 342, Winter 2005 Lecture Notes
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
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
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
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
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.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
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,
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
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.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
1 Lecture 14: Queues Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Introduction to Data Structures and Algorithms
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
April 27, 2017 COSC Data Structures I Review & Final Exam
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
QueueStack CS1020.
Week 3 - Friday CS221.
Stack and Queue APURBO DATTA.
Data Structures and Database Applications Queues in C#
CMSC 341 Lecture 5 Stacks, Queues
Linked Lists, Stacks and Queues Textbook Sections
Stacks, Queues, and Deques
Stacks, Queues, and Deques
More Data Structures (Part 1)
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Presentation transcript:

1 Lecture 26 Abstract Data Types –III Overview  Creating and manipulating Linked List.  Linked List Traversal.  Linked List Traversal using Iterator class.  Stack Implementation using Linked List.  Queue Implementation using Linked List.  Recursive List Search.  Preview: Introduction to Databases.

2 Lecture 26 Creating and Manipulating a New LinkedList  In the last lecture we implemented the List ADT as a Linked List.  The following shows how we may use it to create a dynamic list (list that can expand and shrink as required by the application. header LinkedList list = new LinkedList(); list.insert(0,’c’); list.insert(lst.length(),‘a’); c ca list.insert(1,’b’); cba list.remove(0); ba

3 Lecture 26 Linked List Traversal  Many common higher level list processing routines require a mechanism to traverse the list. For example:  This implementation is very inefficient as each time retrieve(i) is called it starts searching from the start of the list.  A common convention is to provide an iterator class which provides methods that can be used to iterate or step through the list.  The iterator maintains a record of a current list position. public LinkedList reverseOf(LinkedList list) { LinkedList reverse = new LinkedList(); for(int i=0; i<list.length();i++) { reverse.insert(0,list.retrieve(i)); } return reverse; }

4 Lecture 26 Linked List Iterator Class  Some sample methods contained in an iterator class public class LinkedListIterator { private Node cursor;// reference to current position in list public LinkedListIterator(LinkedList list) {// constructor creates iterator from given list cursor = list.getHeader(); } public boolean isPastEnd() { return cursor == null; } public void next() throws NoSuchElementException { if (cursor == null) throw new java.util.NoSuchElementException(); cursor = cursor.getNext(); } public Object retrieve() throws NoSuchElementException { if (cursor == null) throw new java.util.NoSuchElementException(); return cursor.getElement(); }

5 Lecture 26 Using a Linked List Iterator  The previous reverseOf method can now be re-implemented using an iterator:  This implementation is much more efficient as the iterator maintains a notion of a current position during the list traversal process. public LinkedList reverseOf(LinkedList list) { LinkedList reverse = new LinkedList(); LinkedListIterator iterator = new LinkedListIterator(list); while (!iterator.isPastEnd()) { reverse.insert(0,iterator.retrieve()); // retrieve element at this position iterator.next(); // move to next element in list } return reverse; // return new reversed list }

6 Lecture 26 Stacks and Queues Revisited  The original array implementations of Stacks and Queues suffered from inefficiency in that the array was fixed in size. Too small and it was useless, too big and it wasted resources.  Both stacks and queues can be implemented more efficiently using linked lists to maintain the stack and queue data. n Stack - push and pop from front of list (Header) n Queue - enQueue at end of list and deQueue from head of list List (Header) (push and pop from here) (dequeue from here) (enqueue here)

7 Lecture 26 Stacks as List Nodes public StackNode { Node top; int size; public Stack() { top = null; } boolean isEmpty() { return top==null; } public void push(Object e) { Node n = new Node(e,top); } public Object peek() { return top.getElement(); } public void pop() { top.setNext(top.getNext()); }  A simplistic version using list nodes directly. The next slide discusses the preferred implementation.

8 Lecture 26 public StackList { LinkedList list; public Stack() { list = new LinkedList(); } boolean isEmpty() { return list.isEmpty(); } public void push(Object e) { list.insert(0,e); } public Object peek() { return list.retrieve(0); } public void pop() { list.remove(0); }  An ADT such as a Stack requiring flexible data storage can be implemented in terms of a linked list. Note no exception handling code is provided. Implement a Stack Using a Linked List

9 Lecture 26 Implementing a Queue Using LinkedList public QueueList { private LinkedList items; public QueueList() { items = new LinkedList(); } public void enqueue(Object e) { items.insert(items.length(),e); } public void dequeue() { items.remove(0); } public Object front() { return items.retrieve(0); } public boolean isEmpty() { return items.isEmpty(); } public int size() { return items.length(); }  The queue implementation is now trivial when using List ADT.  One performance issue is with enqueue which calls insert at end of list thus incurring a full list traversal to locate insertion position.  Simple solution is to maintain a reference to the tail of the list.

10 Lecture 26 Iterative/Recursive List Search  In order to locate the position of an element in a list, the list must be traversed  a linear search is performed on the list contents  use the LinkedListIterator class to implement the search LinkedListIterator find(Object e, LinkedListIterator iterator) { while (!iterator.isPastEnd()) { if (e.equals(iterator.retrieve())) return iterator; else iterator.next(); } return iterator; } LinkedListIterator find(Object e, LinkedListIterator iterator) { if (iterator.isPastEnd() || e.equals(iterator.retrieve())) return iterator; else { iterator.next(); return find(e, iterator); } Iterative versionRecursive version