1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:

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

Chapter 24 Lists, Stacks, and Queues
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
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:
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.
Data Structures & Algorithms
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack 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.
1 Lecture 26 Abstract Data Types –III Overview  Creating and manipulating Linked List.  Linked List Traversal.  Linked List Traversal using Iterator.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
Information and Computer Sciences University of Hawaii, Manoa
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
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’
LinkedList Many slides from Horstmann modified by Dr V.
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).
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli | Ralph Walde Trinity College Hartford, CT presentation slides for published by Prentice.
“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.
Stacks as an Abstract Data Type CS1316: Representing Structure and Behavior.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
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.
Double-Ended Queues Chapter 5.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Chapter 17 Object-Oriented Data Structures
Data Structures and Database Applications Queues in C#
Building Java Programs
Principles of Computing – UFCFA3-30-1
Pointers and Linked Lists
Stacks, Queues, and Deques
Lecture 21 Stacks and Queues Richard Gesick.
Stacks, Queues, and Deques
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Collections Framework
More Data Structures (Part 1)
Lecture 16 Stacks and Queues CSE /26/2018.
CS210- Lecture 6 Jun 13, 2005 Announcements
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Data Structures & Programming
Presentation transcript:

1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview: Abstract Data Types V

2 Lecture 26 The Stack ADT l A stack is a list that limits insertions and removals to the front (top) of the list. l Operations »Push: insert an object onto the top of the stack. »Pop: remove the top object from the stack. »Empty: returns true if the stack is empty. »Peek: retrieve the top object without removing it. Top In a Stack, insertions and deletions occur at the top New Top

3 Lecture 26 Implementing Stacks as Linked List  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) (push and pop from here) Click here for link Linked List Implementation of Stack Linked List Implementation of Stack

4 Lecture 26 The Stack Class l A Stack is very easy to implement as an extension of our generic List structure. public class Stack extends List { public Stack() { super(); } public void push( Object obj ) { insertAtFront(obj); } public Object pop() { return removeFirst(); } } // Stack Invoke the List() constructor. Abstract Data Type: We limit access to the data to push() and pop().

5 Lecture 26 Testing the Stack Class l Stack behavior: Last In First Out (LIFO) l Use a stack to reverse a string. public static void main( String argv[] ) { Stack stack = new Stack(); String string = "Hello this is a test string"; System.out.println("String: " + string); for (int k = 0; k < string.length(); k++) stack.push(new Character( string.charAt(k))); Object o = null; String reversed = ""; while (!stack.isEmpty()) { o = stack.pop(); reversed = reversed + o.toString(); } System.out.println("Reversed String: " + reversed); } // main() Push each character. Then pop each character.

6 Lecture 26 The Queue ADT l A queue is a list that limits insertions to the rear and removals to the front of a list. l Operations »Enqueue: insert an object onto the rear of the list. »Dequeue: remove the object at the front of the list. »Empty: return true if the queue is empty. Head In a queue, insertions take place at the rear, and removals occur at the front New Head

7 Lecture 26 Linked List Implementation of Queues  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 Queue - enQueue at end of list and deQueue from head of list List (Header) (dequeue from here) (enqueue here) Click here for link Linked List Implementation of Queue Linked List Implementation of Queue

8 Lecture 26 The Queue Class l A Queue is very easy to implement as an extension of our generic List structure. public class Queue extends List { public Queue() { super(); } public void enqueue(Object obj) { insertAtRear( obj ); } public Object dequeue() { return removeFirst(); } } // Queue Invoke the List() constructor. Abstract Data Type: We limit access to the data to enqueue() and dequeue().

9 Lecture 26 Java Library: java.util.LinkedList Java provides a LinkedList class public class LinkedList extends AbstractSequentialList implements List { public void add(int index, Object o); // Inserts o at index position public void addFirst(Object o); // Inserts o at beginning of list public void addLast(Object o); // Appends o to end of list public Object get(int index); // Returns list element at index public Object getFirst(); // Returns first element in the list public Object getLast(); // Returns last element in list. public Object removeFirst(); // Removes and returns first element public Object removeLast(); // Removes and returns last element } Implements the List interface. l Advantage: The methods can be attached to a wide range of sequential structures.