1 linked structures, linkedset references as links linear linked lists and non-linear structures...

21
1 Linked Structures, LinkedSet • References as Links • Linear Linked Lists and Non- linear Structures • Managing Linked Lists • Data Encapsulation Separate from Linking • Doubly Linked Lists • Reading: L&C : 4.1-4.3

Upload: cecilia-henry

Post on 20-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

1

Linked Structures, LinkedSet

• References as Links

• Linear Linked Lists and Non-linear Structures

• Managing Linked Lists

• Data Encapsulation Separate from Linking

• Doubly Linked Lists

• Reading: L&C : 4.1-4.3

Page 2: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

2

References as Links

• A linked structure is a data structure that uses object reference variables to create links between objects

• Declaring an object reference variableObject obj = new Object();

• A diagram of an object reference variable

obj

Object reference variable An object of Object class

Page 3: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

3

References as Links

• A self-referential Person class that contains a pointer to another Person class objectpublic class Person

{

// attributes of a “person”

private String name;

private String address;

// link (or pointer) to another Person object

private Person next; // next Person in list

Page 4: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

4

References as Links

• A self-referential Person class that contains a pointer to another Person class object// constructor, accessor, and mutator methodspublic Person(. . .) // parameters are attributes{ // set the attributes

next = null;}

public Person(Person next, . . .) { this.next = next;

}

public void setNext(Person next) { this.next = next;

}public Person getNext() {

return next;}}

Page 5: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

5

Linked Lists

• A null terminated linked list of Person objects can be used to represent people waiting in line starting at the “front”

• Beginning of a linked list of Person objectsprivate Person front;

• A diagram of a linked list

Person next; nullfront Person next; Person next;

Page 6: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

6

Linked Lists

• Unlike an array which has a fixed size, a linked list is considered to be a dynamic structure

• The amount of memory used grows and shrinks as objects are added to the list or deleted from the list

• The Java compiler and virtual machine allocate memory for each individual object as it is created (via the new operator) and free memory as each object is garbage collected

Page 7: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

7

Linear/Non-Linear Linked Structures

• A linked list is considered to be a linear structure since it can be visualized as a line

• Some linked structures are non-linear, e.g.

entry

Page 8: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

8

Managing Singly Linked Lists

• The order in which references are changed is crucial to maintaining a linked list

• Can insert a new Person in three places:– at the front– In the middle – at the end

• Can delete an existing Person in three places:– At the front– In the middle – at the end

Page 9: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

9

Inserting Objects in a Linked List

• Create new Person object and link at frontPerson newPerson = new Person(. . .);

newPerson.setNext(front);

front = newPerson;

newPerson = null;

newPerson

front

Person next;

Person next; Person next;

2

3

1

4

Page 10: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

10

Inserting Objects in a Linked List• Create new Person object and link after the

current Person object (current could be at end)Person newPerson = new Person(. . .);

newPerson.setNext(current.getNext());

current.setNext(newPerson);

newPerson = null;newPerson

front

Person next;

Person next; Person next;

2

3

1

4

Person next;

current

Page 11: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

11

Inserting Objects in a Linked List

• How about inserting an element at the end? Let's do it together.

Page 12: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

12

Removing Objects from a Linked List

• Remove Person object at frontPerson removed = front;

front = front.getNext());

removed.setNext(null);

removed

front Person next; Person next; Person next;

2

3

1

Page 13: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

13

Removing Objects from a Linked List

• Remove Person object after current Person object (removed object could be at end)Person removed = current.getNext();

current.setNext(removed.getNext());

removed.setNext(null);

front Person next; Person next; Person next;

2 3

1current removed

Page 14: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

14

Removing Objects from a Linked List

• How about removing an object from the end?

Page 15: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

15

Elements without Links

• It is desirable to have a generic link class that can be used to link any type of objects of any class encapsulating the “real” data

• Class LinearNode<T> does that this way

Object of type T

front LinearNode next;T element;

Object of type T

LinearNode next;T element;

null

Object of type T

LinearNode next;T element;

Page 16: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

16

Elements without Links

• Code for Linear Node<T>public class LinearNode<T>

{

private LinearNode<T> next;

private T element;

public LinearNode() // create an empty node

{

next = null;

element = null;

}

Page 17: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

17

Elements without Links

• Code for LinearNode<T>public LinearNode(T element){

next = null; this.element = element;

}public LinearNode<T> getNext(){

return next;}public void setNext(LinearNode<T> next){

this.next = next;}

Page 18: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

18

Elements without Links

• Code for LinearNode<T>public T getElement()

{

return element;

}

public void setElement(T element)

{

this.element = element;

}

}// end class LinearNode<T>

Page 19: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

19

Elements without Links

• Using the LinearNode class to create a linked list of three Person objects

int size = 3;

LinearNode<Person> front = new LinearNode<Person>();

LinearNode<Person> current = front;

for(int i = 1; i < 3; i++) {

LinearNode<Person> newNode = new

LinearNode<Person>();

current.setNext(newNode);

current = newNode;

}

Page 20: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

20

Doubly Linked Lists

• Each DoubleNode object has a reference to next DoubleNode and previous DoubleNodepublic class DoubleNode<T>

{

private DoubleNode<T> next;

private DoubleNode<T> prev;

private T element;

Object of type T

front DoubleNode<T> nextDoubleNode<T> prevT element

Object of type T

null

null back

DoubleNode<T> nextDoubleNode<T> prevT element

Page 21: 1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from

21

Doubly Linked Lists

• To Add a DoubleNode object to the list, your code must set both the DoubleNode next and prev variables.

• To delete a DoubleNode object from the list, your code must bypass the DoubleNode links in both directions.