1 Applications of Collections LinkedList Nodes Declarations Using the LinkedList Collection Introduction to Stacks and Queues.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Stacks, Queues, and Linked Lists
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.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
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.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COMP 103 Linked Stack and Linked Queue.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
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 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.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
TCSS 342, Winter 2005 Lecture Notes
Chapter 13 Linked Structures - Stacks. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked implementation.
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
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
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.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
LinkedList Many slides from Horstmann modified by Dr V.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
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.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
COS 312 DAY 19 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 Layers using non-opaque panels.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
List Interface and Linked List Mrs. Furman March 25, 2010.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Linked Structures Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Collections (Stack, queue, tree, graph). References as Links References can be used to create a variety of linked structures, such as a linked list.
Linked Lists, Queues, Stacks
Double-Ended Queues Chapter 5.
Stacks Chapter 5.
Marcus Biel, Software Craftsman
QueueStack CS1020.
Chapter 15 Lists Objectives
Stacks.
Stack and Queue APURBO DATTA.
Chapter 4 Linked Structures - Stacks
Stacks and Queues.
Building Java Programs
Stacks, Queues, and Deques
Chapter 4 Linked Structures - Stacks
Stacks, Queues, and Deques
Stacks.
More Data Structures (Part 1)
Stacks, Queues, and Deques
Presentation transcript:

1 Applications of Collections LinkedList Nodes Declarations Using the LinkedList Collection Introduction to Stacks and Queues

2 The LinkedList Class The LinkedList Collection is a better choice in applications requiring frequent insertions and deletions from any place If, on the other hand, there are frequent retrievals, then the ArrayList Collection is a better choice for good performance. A linked list data structure is a collection of nodes storing data and links (references) to other nodes. Two such data structures are singly linked list (SLL) and doubly linked list

3 Example 1: Merging Linked Lists import java.util.*; public class InterleaveLinkedLists { public static void main(String[] args) { LinkedList list1 = new LinkedList (); LinkedList list2 = new LinkedList (); for(int i = 20; i <= 30; i++) list1.add(new Integer(i)); for(int i = 1; i <= 7; i++) list2.add(new Integer(i)); System.out.println("list1 is: " + list1); System.out.println("list2 is: " + list2); ListIterator iter1, iter2; for(iter1=list1.listIterator(),iter2=list2.listIterator(); iter2.hasNext();) { if(iter1.hasNext()) iter1.next(); iter1.add(iter2.next()); } System.out.println("The merged list is:"); System.out.println(list1); }

4 Example 2: Reversing a Linked List import java.util.*; public class ReversingLinkedList { public static void main(String[] args) { LinkedList c = new LinkedList (); ListIterator iter1,iter2; for(int i = 20; i <= 30; i++) c.add(new Integer(i)); System.out.println("Original : " + c); int j,size = c.size(); for(j=0,iter1 = c.listIterator(size),iter2 = c.listIterator(); iter1.hasPrevious() && iter2.hasNext() && j<size/2;) { exchange(c, c.indexOf(iter1.previous()), c.indexOf(iter2.next())); j++; } System.out.println("Reversed : " + c); } public static void exchange(List a, int i, int j) { Integer temp = a.get(i); a.set(i, a.get(j)); a.set(j, temp); }

5 The Stack Collection A stack is a LIFO (Last In First Out) data structure. The last element inserted is the first element to be removed. A stack can be implemented as a linked list in which insertion is always done at the first position (usually called the top) of the stack. Removal is also always done at this position. Usual stack operations: push(), pop(), isEmpty(), top() ( peek() in Java) top

6 Using java.util.Stack import java.util.Stack; class TestStack{ public static void main(String args[]){ Stack s = new Stack (); s.push("One"); s.push("Two"); System.out.println("Top: " + s.peek()); s.push("Three"); System.out.println("This is the order in which stack elts are accessed:"); while(!(s.isEmpty())) System.out.println(s.pop()); }

7 Example 3: Reversing a Text File import java.io.*; import java.util.Stack; public class ReverseTextFile{ public static void main(String[] args) throws IOException{ Stack stack = new Stack (); BufferedReader inputStream = new BufferedReader(new FileReader("infile.txt")); String inputLine, line; while((inputLine = inputStream.readLine()) != null) stack.push(inputLine); inputStream.close(); PrintWriter outputStream = new PrintWriter(new FileWriter("outfile.txt")); while(! stack.isEmpty()) outputStream.println(stack.pop()); outputStream.close(); System.out.println("The reversal process is complete"); }

8 Queues A queue is a FIFO (First In First Out) data structure. A queue can be implemented as a linked list in which insertion (enqueing) is always done at the last queue position (rear), and removal (dequeing) is always done at the first queue position (front). There is no Queue class in the Java API but the ArrayList methods addLast(), getFirst(), removeFirst() and isEmpty() can be used to simulate a queue. rear front