Data structures Abstract data types Java classes for Data structures and ADTs.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
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.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
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
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 19 Java Data Structures
Java's Collection Framework
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
C++ STL CSCI 3110.
Chapter 18 Java Collections Framework
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
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.
CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009.
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,
CIS 068 Welcome to CIS 068 ! Lesson 10: Data Structures.
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Understanding Data Types and Collections Lesson 2.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks And Queues Chapter 18.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Using the Java Collection Libraries COMP 103 # T2
Chapter 12 – Data Structures
Chapter 19 Java Data Structures
Stacks and Queues.
structures and their relationships." - Linus Torvalds
Pointers and Linked Lists
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Presentation transcript:

Data structures Abstract data types Java classes for Data structures and ADTs

Data structures "Any method of organising a collection of data to allow it to be manipulated effectively…." "Examples data structures are: array, dictionary, graph, hash, heap, linked list, matrix, object, queue, ring, stack, tree, vector" Imperial College Free Online Dictionary of Computing,

Data structures so far we have used the array data structure simple way of storing related elements of same type occupies fixed contiguous space in memory the array elements are next to each other elements are accessed by their index type and length is fixed when array is created myArray =

Abstract data types we also looked at how to use an array to model a list we call a list an abstract data type it has defined operations add to the end of the list find an item in the list delete an item from the list etc. it can be implemented in different ways array, piece of paper, tree, linked list the operations have the same effect no matter how the list is implemented other examples of abstract data types set, queue, stack, map

Sets a set is an unordered group of elements duplicates are not allowed otherwise how would you tell them apart? the set of "foods I like" contains the elements cereal, chicken, chips, tomato soup, orange juice and chocolate Cereal Chicken Chocolate set of foods I like Chips Orange juice Tomato soup

Lists a list is a group of elements arranged in some order so we can talk about the first element in the list the last element in the list element 3 the order could be meaningful alphabetical by size by time or it could simply be the order the elements were added duplicates are allowed they are distinguished by their position 1. cereal 2. orange juice 3. chocolate 4. tomato soup 5. chocolate Things I ate today (in chronological order) 6. chicken 7. chips 8. chocolate

Queue a queue is a a group of items arranged in order of arrival new items can only be added to the back of the queue items can only be removed from the front of the queue shoppers at supermarket check-out taxis in rank computer processes awaiting execution first-in, first-out (FIFO)

Stacks a stack is a group of items arranged in order new items can only be added to the top of the stack items can only be removed from the top of the stack stack of chairs or books plates in cafeteria temporary data storage in CPU last in, first out (LIFO)

Maps A map is a collection of key/element pairs each element is stored in a position determined by its key can look up an element using its key also known as a dictionary key is the word element is the definition algorithm confusion university

Implementing ADTs the abstract parts of abstract data type refers to the fact that we can implement them in different ways but their definition remains unchanged we have already seen how to implement a list using an array how could a queue (first in, first out) stack (last in, first out) be implemented using an array? 3631 myArray = next = 4

Disadvantages of arrays arrays occupy contiguous space in memory the array size has to be specified when it is created so the processor can allocate the right amount of memory space it can't be changed if more (or less) space is required awkward when inserting into or deleting from the middle of an array all the other elements have to shuffled along

Linked lists a more flexible way to store elements in order for each element, store its data where to find the next element a pointer or reference or address or link elements do not need to be stored next to each other list can grow or shrink as required last element links to special value null indicating end

Linked lists a linked list consists of a chain of nodes sarah timadamjim h  this list has four nodes  each node has  a data element (a name)  a link to the next node (arrow)  we can access the elements in turn by following the links in the chain  elements can be added by splicing in new nodes  elements can be deleted by bypassing nodes

Data structures in Java Java provides many classes to implement data structures and ADTs called Collections as they are used to collect together related items in package java.util need to import to use HashSet, Vector, ArrayList, LinkedList, Stack, HashMap

HashSet class implements a set has methods to add and remove elements add(Object o) remove(Object o) and a method to check if an object is a member of the set boolean contains(Object o)

Vector and ArrayList classes the Java Vector and ArrayList classes have similar functionality both model a list both have methods to add an element to the next available position add an element in a given position other elements will be shuffled along to make room you don't need to write the code! return an element, given its index delete a given element, or the element at a given index again the shuffling is done for you return the first element or the last element

Vector and ArrayList classes these classes are implemented using an array when the array is full, the elements are automatically copied over to a larger array we don't interact with the array directly instead we use the public methods of the class Vector was implemented first ArrayList is the later version

LinkedList class models a list like Vector and ArrayList underlying data structure is a linked list more efficient for adding and deleting elements, and changing size less efficient for accessing elements by their index has methods to model a queue offer(Object o) adds an element to the end of the queue remove() removes the element as the head of the queue

Stack class a subclass of Vector that models a stack so uses an array as the underlying data structure push(Object o) pushes an element onto the top of the stack pop() removes and returns the top element of the stack

HashMap models a map when elements put into the map their key must be specified put(Object key, Object element) the key must be an object not a primitive type such as int often Strings are used as keys get(Object key) returns the object corresponding to key the mapping works by generating a hashcode from the key storing the element at a position indicated by the hashcode HashMap class

The Iterator Interface often need to access each element in a Collection in turn print the details of all students enrolled in a School the Iterator interface in java.util allows us to do this first create an Iterator object then use the Iterator next() method to obtain the next object in the collection while the Iterator hasNext() method returns true

The Iterator Interface Vector studentList; studentList = new Vector(); // code omitted to fill up student list Iterator it = studentList.iterator(); while ( it.hasNext()) { // retrieve and cast the next Student Student s = (Student)it.next(); s.printDetails(); } can create an Iterator from HashSet, Vector, ArrayList, LinkedList, Stack collections can create an Iterator indirectly from a HashMap use values() method to first generate a collection from the map

Using Java collections these collections can hold objects of any type but not primitive types such as int need to wrap in an Integer object need to cast to appropriate type when retrieving from collection the Collection class to use depends on requirements list with frequent access using index: Vector or ArrayList list with frequent insertions or deletions: LinkedList collection with fast retrieval using key, order not important : HashMap Stack: Stack Queue: LinkedList Set: HashSet

Further work Reading java documentation – the package java.util ckage-summary.html do NOT use the Java collection classes in your assignment as you will be assessed on your ability to model a list using an array