130 likes | 245 Views
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
E N D
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 • 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);
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
studentList References as Links • References can be used to create a variety of linked structures, such as a linked list:
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)
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
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
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
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; }
list Other Dynamic List Implementations • It may be convenient to implement as list as a doubly linked list, with next and previous references:
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
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
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