Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
-- Java Software Solutions Data Structures Lewis and Loftus Introduction All rights reserved. 1 • Let’s explore some advanced techniques for o and managing information Copyright 1997 by John Lewis and William Loftus. dynamic structures Abstract Data Types (ADTs) linked lists queues stacks • Chapter 16 focuses on: – – – – – Chapter 16 Java Software Solutions Lewis and Loftus Static vs. Dynamic Structures • A static data structure has a fixed size • This meaning is different than those associa static modifier • Arrays are static; once you define the numb elements it can hold, it doesn’t change Copyright 1997 by John Lewis and William Loftus. All rights reserved. 2 • A dynamic data structure grows and shrinks a by the information it contains Chapter 16 Java Software Solutions Object References Lewis and Loftus • Recall that an object is reference a variable that sto the address of an object All rights reserved. 3 • A reference can also be called a pointer student Copyright 1997 by John Lewis and William Loftus. John Smith 40725 3.57 • They are often depicted graphically: • Chapter 16 Java Software Solutions References as Links Lewis and Loftus • Object references can be used links to between create objects John Smith 40725 3.57 All rights reserved. Jane Jones 58821 3.72 Copyright 1997 by John Lewis and William Loftus. 4 • Suppose Student a class contained a reference t another Student object • Chapter 16 Java Software Solutions References as Links Lewis and Loftus All rights reserved. 5 • References can be used to create a variety o structures, such as a: linked list •studentList • • • • Copyright 1997 by John Lewis and William Loftus. • See Library.java • Chapter 16 Abstraction Java Software Solutions Lewis and Loftus • Our data structures should be abstractions is not abstra • That is, they should hide details as appropr • This helps manage complexity • Our original Library solution Copyright 1997 by John Lewis and William Loftus. All rights reserved. 6 • One problem is that we use a public variable links • Chapter 16 Java Software Solutions Abstract Data Types Lewis and Loftus • An abstract data(ADT) type is an organized collec of information and a set of operations used that information • The set of operations define the interface t • As long as the ADT accurately fulfills the p the interface, it doesn't really matter how implemented Copyright 1997 by John Lewis and William Loftus. All rights reserved. 7 • Objects are a perfect programming mechanism ADTs because their internal details are enca • Chapter 16 Java Software Solutions Coupling and Cohesion Lewis and Loftus • A well-defined ADT attempts to minimize coup while maximizing cohesion • Coupling is the strength of the relationship components • Cohesion is the strength of the relationship parts of one component Copyright 1997 by John Lewis and William Loftus. All rights reserved. 8 • We want to formally specify a simple relatio between the ADT and the outer world, and put things that relate to the ADT management ins Chapter 16 Java Software Solutions Library Revisited Lewis and Loftus • A better version of the Library solution wou book_list object that provides services such – add a book to the list – print the book list – • The book_listobject in turn interacts with i Book objects All rights reserved. 9 • Each Bookobject governs its own references p Copyright 1997 by John Lewis and William Loftus. • See Library2.java Chapter 16 Java Software Solutions Adding a Node Lewis and Loftus • To add a new node to the end of a linked lis list looking for the last node, then add the current Copyright 1997 by John Lewis and William Loftus. All rights reserved. 10 • The last node is the one withreference a null next • ... Chapter 16 Java Software Solutions Other List Operations Lewis and Loftus • You may also want to perform such operations – add a node to the front of the list – add a node somewhere in the middle of the list – delete a node from the list – • Each operation can be defined separately as method Copyright 1997 by John Lewis and William Loftus. All rights reserved. 11 • How an operation is implemented depends on t underlying representation of the data struct Chapter 16 Java Software Solutions Lewis and Loftus Other Dynamic List Implementations Copyright 1997 by John Lewis and William Loftus. All rights reserved. 12 • It may be convenient to implement doubly as list as linked list, with next and previous referenc list Chapter 16 Java Software Solutions Lewis and Loftus Other Dynamic List Implementations list count: 4 front rear Copyright 1997 by John Lewis and William Loftus. All rights reserved. 13 • It may also be convenient to use a separate with references to both the front and rear o Chapter 16 Java Software Solutions Library Revisited Lewis and Loftus • Another problem with the existing Library so that the Book class contains the reference to t object in the list • Ideally, we would like to separate the data management from the information it holds • The objects we want to store in the list sho to be involved with the list references Copyright 1997 by John Lewis and William Loftus. All rights reserved. 14 • As we explore other data structures, we will separation Chapter 16 Queues Java Software Solutions Lewis and Loftus First-I • A queue ADT is similar to a list but adds it the end of the list and removes them from th Copyright 1997 by John Lewis and William Loftus. remove All rights reserved. 15 a line of people at a bank teller’ • It is called a FIFO data structure: add • Analogy: Chapter 16 Queues Java Software Solutions Lewis and Loftus • We can define the operations on a queue as f – enqueue - add an item to the rear of the queue – dequeue - remove an item from the front of the que – empty - returns true if the queue is empty – All rights reserved. 16 • By operating on the Object class, any object can b stored in the queue Copyright 1997 by John Lewis and William Loftus. • See QTrek.java Chapter 16 Stacks Java Software Solutions Lewis and Loftus • A stack ADT is also linear, like a list or q • Items are added and removed from only one en stack All rights reserved. 17 Last-In, First-Out Copyright 1997 by John Lewis and William Loftus. a stack of plates • It is therefore LIFO: • Analogy: Chapter 16 Stacks Java Software Solutions add Lewis and Loftus remove Copyright 1997 by John Lewis and William Loftus. All rights reserved. • Stacks are often drawn vertically: Chapter 16 18 Stacks Java Software Solutions Lewis and Loftus push - add an item to the top of the stack pop - remove an item from the top of the stack peek - retrieves the top item without removing it empty - returns true if the stack is empty • Some stack operations: – – – – – All rights reserved. 19 • The java.utilpackage contains Stacka class, which is implemented Vector using a Copyright 1997 by John Lewis and William Loftus. • See Decode.java Chapter 16