1 / 13

Lecture 9

Lecture 9. dynamic structures Abstract Data Types (ADTs) linked lists linked list variations: singly linked list doubly linked list circular lists. myboy. Smith 407 3.57. Object References. object reference: a variable that stores address of object

Download Presentation

Lecture 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 9 • dynamic structures • Abstract Data Types (ADTs) • linked lists • linked list variations: • singly linked list • doubly linked list • circular lists

  2. myboy Smith 407 3.57 Object References • object reference: a variable that stores address of object • A reference can also be called a pointer • They are often depicted graphically: Public class Student { String name; int id; double avg; } Student myboy = new Student(“Smith”, 407, 3.57);

  3. Smith 407 3.57 Jones 821 3.72 References as Links • Object references can create links between objects: A self-referential structure public class Student { String name; int id; double avg; Student next; } Student myboy = new Student(“Smith”, 407, 3.57); Student myboy.next = new Student(“Jones”, 821, 372) myboy

  4. studentList References as Links • References can be used to create a variety of linked structures, such as a linked list:

  5. Static vs. Dynamic Structures • Dynamic structures: • can grow and shrink during execution • components usually accessed via references • Static structures: • fixed size • storage allocated all at once • components are usually named • Arrays are static; once you define the number of elements it can hold, it doesn’t change • NB: This is a different meaning of the word “static” (do not confuse with the static modifier)

  6. Abstraction - again! • Our data structures should be abstractions • That is, they should hide details as appropriate • We want to separate the interface of the structure from its underlying implementation • This helps manage complexity and makes the structures more useful

  7. Intermediate Nodes • The objects being stored should not have to deal with the details of the data structure in which they may be stored • For example, the Student class stored a link to the next Student object in the list • Instead, we can use a separate node class that holds a reference to the stored object and a link to the next node in the list • Therefore the internal representation actually becomes a linked list of nodes

  8. count head 4 data data data next next next data next Singly-Linked Lists • The Student record can be put in a general data field: public class ListElement { Student data; ListElement next; } public class LinkedList { int count; ListElement head; } public class Student { String name; int id; double avg; } Smith 407 3.57 Jones 821 3.72 Papadakis 132 3.88 Narahari 556 3.80

  9. count head 4 data data data next next next data next Smith 407 3.57 Jones 821 3.72 Papadakis 132 3.88 Narahari 556 3.80 Singly-Linked Lists in textbook: • Naming conventions -slightly different in “Java Structures”: public class SinglyLinkedListElement { Object data; ListElement nextElement; } public class SinglyLinkedList { int count; SinglyLinkedListElement head; } public class Student { String name; int id; double avg; }

  10. list Other Dynamic List Implementations • It may be convenient to implement as list as a doubly linked list, with next and previous references:

  11. list count: 4 head tail Other Dynamic List Implementations • It may also be convenient to use a separate header node, with references to both the front and rear of the list

  12. push pop Stacks • A stack ADT is also linear, like a list or queue • Items are added and removed from only one end of a stack • It is therefore LIFO: Last-In, First-Out • Analogy: a stack of plates

  13. dequeue enqueue Queues • A queue is similar to a list but adds items only to the end of the list and removes them from the front • It is called a FIFO data structure: First-In, First-Out • Analogy: a line of people at a bank teller’s window

More Related