COP3804 - INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
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.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
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++)
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Chapter 12: Data Structures
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
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.
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.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
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).
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fifteen: An Introduction to Data Structures.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ Stack ◦ Queue & Priority Queue 2.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Chapter 15 An Introduction to Data Structures. Chapter Goals To learn how to use the linked lists provided in the standard library To be able to use iterators.
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.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
COP INTERMEDIATE JAVA Recursion. The term “recursion” refers to the fact that the same computation recurs, or occurs repeatedly, as a problem is.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 15 – An Introduction to Data Structures.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Prefix notation in action
Week 4 - Monday CS221.
G64ADS Advanced Data Structures
Marcus Biel, Software Craftsman
Unit 1 Hadoop and big data
Chapter 20 An Introduction to Data Structures
Chapter 12: Data Structures
Stacks and Queues.
Chapter 17 Object-Oriented Data Structures
Pointers and Linked Lists
Linked Lists.
Chapter 15 – An Introduction to Data Structures
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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 list, in particular, is a collection that stores its elements in a position-based sequence. The memory allocation for the list elements may be contiguous allocation or linked allocation. ArrayList is an example of a data structure that uses contiguous allocation whereas LinkedList is an example of a data structure that uses linked allocation.

Linked Lists A linked list is a data structure that consists of a number of nodes; each node has an object (the data we are storing) and a reference to the next node in the list. They allow for elements to be efficiently removed and added from the middle of the list since the rest of the elements in the list don’t need to be moved (as in arrays), only the neighboring node references need to be updated. Visiting all the elements of a linked list in sequential order is efficient but accessing the object stored at an arbitrary location is not. Let’s say you only want to access the object stored at position 5, you would first need to iterate through the first 4 elements in the list.

Linked Lists In an application where a lot of elements need to be added or removed at any position in a list, a LinkedList would be a more efficient data structure to use than an ArrayList. For applications where the main operation is accessing elements at a specific location, an Arraylist would perform better.

Linked Lists The Java library provides the java.util.LinkedList class, which has methods to access the first and last elements in the list: getFirst() and getLast() It also provides methods to add and remove the first and last elements: addFirst() addLast() removeFirst() removeLast()

Implementing Linked Lists For assignment 4, you had to create your own implementation of a linked list class based on the one provided in the textbook. We called it StudentLinkedList. The purpose of this exercise was for you to have a better understanding of the inner workings of a linked list. The StudentLinkedList class has fewer methods than the java.util.LinkedList and the data type of the object stored inside each node is Student instead of Object. When retrieving the value inside each node, since the type is Student, we may call the Student class methods without having to cast. For example, assuming that first is an object of type Node and element is an object of type Student. We could say: first.value.equals(element); where equals is a method of the Student class. No casting needed.

Inner classes The implementation of the linked list class in the textbook, as well as the implementation of the StudentLinkedList class, make use of inner classes. Since the Node class is only used by the StudentLinkedList class, it was declared inside the StudentLinkedList class as a private inner class. (It cannot be used by classes outside of the StudentLinkedList class). The advantage of inner classes is that they have access to the private members (variables, methods, other inner classes) of the outer class. Although, in our implementation, the Node class is not accessing the private members of the StudentLinkedList class, which are the first and last instance variables.

Stacks and Queues Stacks and queues are other data structures that allow inserting and removing items at the ends of the list only, not the middle. A stack is a collection of items with a “last in, first out” retrieval concept, known as LIFO. It lets you insert and remove elements at only one end, the top of the stack. You may think of it as a stack of plates at a buffet restaurant. Stacks can be used by any system that needs to process the elements in the reverse order that they were added to the list. A stack class normally provides the following methods: push - to add an element at the top of the stack pop - to remove a element from the top of the stack peek - to look at the element stored at the top of the stack without removing it. empty – to check if there are elements in the stack

Stacks and Queues Queues are similar to stacks but you add elements at the end of the queue and remove elements from the other end, which is called the head (the beginning) of the queue. Queues store items in a “first in, first out” fashion, also known as FIFO. Queues can be used by any system that needs to process elements in the same order that they were added to the list. For example, the list of print jobs for a network printer. A queue class normally provides the following methods: enqueue – to add an element at the tail of the queue dequeue– to remove the element at the head of the queue. peek – to get the element at the head of the queue without removing it. empty – to check if there are elements in the queue.

References Gaddis, Tony, and Godfrey Muganda. Starting out with Java: from Control Structures through Data Structures 2 nd ed. Boston, USA: Addison-Wesley, 2012 Horstmann, Cay. Big Java 4th ed. New York, USA: John Wiley & Sons, Inc., 2010.