Linked Structures, LinkedSet - PowerPoint PPT Presentation

Loading...

PPT – Linked Structures, LinkedSet PowerPoint presentation | free to download - id: 6fcda6-MDA2Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Linked Structures, LinkedSet

Description:

Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from Linking – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 22
Provided by: BobW83
Learn more at: http://www.cs.umb.edu
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Linked Structures, LinkedSet


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 LC 4.1-4.3

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 variable
  • Object obj new Object()
  • A diagram of an object reference variable

Object reference variable
An object of Object class
obj
3
References as Links
  • A self-referential Person class that contains a
    pointer to another Person class object
  • public 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

4
References as Links
  • A self-referential Person class that contains a
    pointer to another Person class object
  • // constructor, accessor, and mutator methods
  • public 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

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 objects
  • private Person front
  • A diagram of a linked list

Person next
Person next
null
Person next
front
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

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
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

9
Inserting Objects in a Linked List
  • Create new Person object and link at front
  • Person newPerson new Person(. . .)
  • newPerson.setNext(front)
  • front newPerson
  • newPerson null

1
newPerson
Person next
2
4
front
Person next
Person next
3
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

1
newPerson
Person next
2
4
current
front
Person next
Person next
Person next
3
11
Inserting Objects in a Linked List
  • How about inserting an element at the end? Let's
    do it together.

12
Removing Objects from a Linked List
  • Remove Person object at front
  • Person removed front
  • front front.getNext())
  • removed.setNext(null)

1
removed
3
front
Person next
Person next
Person next
2
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)

current
removed
1
Person next
front
Person next
Person next
3
2
14
Removing Objects from a Linked List
  • How about removing an object from the end?

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 LinearNodeltTgt does that this way

front
null
LinearNode next T element
LinearNode next T element
LinearNode next T element
Object of type T
Object of type T
Object of type T
16
Elements without Links
  • Code for Linear NodeltTgt
  • public class LinearNodeltTgt
  • private LinearNodeltTgt next
  • private T element
  • public LinearNode() // create an empty node
  • next null
  • element null

17
Elements without Links
  • Code for LinearNodeltTgt
  • public LinearNode(T element)
  • next null
  • this.element element
  • public LinearNodeltTgt getNext()
  • return next
  • public void setNext(LinearNodeltTgt next)
  • this.next next

18
Elements without Links
  • Code for LinearNodeltTgt
  • public T getElement()
  • return element
  • public void setElement(T element)
  • this.element element
  • // end class LinearNodeltTgt

19
Elements without Links
  • Using the LinearNode class to create a linked
    list of three Person objects
  • int size 3
  • LinearNodeltPersongt front new
    LinearNodeltPersongt()
  • LinearNodeltPersongt current front
  • for(int i 1 i lt 3 i)
  • LinearNodeltPersongt newNode new
  • LinearNodeltPersongt()
  • current.setNext(newNode)
  • current newNode

20
Doubly Linked Lists
  • Each DoubleNode object has a reference to next
    DoubleNode and previous DoubleNode
  • public class DoubleNodeltTgt
  • private DoubleNodeltTgt next
  • private DoubleNodeltTgt prev
  • private T element

front
DoubleNodeltTgt next DoubleNodeltTgt prev T element
null
DoubleNodeltTgt next DoubleNodeltTgt prev T element
null
back
Object of type T
Object of type T
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.
About PowerShow.com