Objectives: Discuss linked lists Syntax Implementation Linked - - PowerPoint PPT Presentation

objectives
SMART_READER_LITE
LIVE PREVIEW

Objectives: Discuss linked lists Syntax Implementation Linked - - PowerPoint PPT Presentation

Linked List Objectives: Discuss linked lists Syntax Implementation Linked List Characteristics: A collection Holds objects to store, retreive and manipulate Java Library LinkedList Store type Object


slide-1
SLIDE 1

Linked List

  • Objectives:

– Discuss linked lists

  • Syntax
  • Implementation
slide-2
SLIDE 2

Linked List

  • Characteristics:

– A collection

  • Holds objects to store, retreive and manipulate

– Java Library

  • LinkedList
  • Store type Object
  • In package java.Util
  • Definition:

– It is a data structure including a list of nodes so that each node is composed of the object and a reference to another node.

slide-3
SLIDE 3

Linked List

  • We must declare the class Node

class Node { <ObjectType> <ObjectName>; Node link; }

– Example a Node of a Rational umber .

slide-4
SLIDE 4

Linked List

  • Example a Node of a Rational number .

class RationalNode { Rational myRat; RationalNode link; }

slide-5
SLIDE 5

Linked List

  • Declaration:

– Syntax

RatNode rPtr;

– Meaning:

  • Allocate a memory to hold a reference of a RatNode
  • NO RatNode is created therefore, the reference is

null

– Instantiate:

rPtr = new RatNode();

slide-6
SLIDE 6

Constructor

  • Constructor

RatNode ( Rational r, RatNode lPtr) { myRat = r; link = lPtr; }

  • Invoke

new RatNode( new Rational(2,3), null)

slide-7
SLIDE 7

Linked List

  • Look at this instruction

RatNode head = new RatNode(new Rational(2,3), null);

– A new object 2/3 (Rational) is created – A new object of RatNode is created – The reference of RatNode is stored in head.

slide-8
SLIDE 8

Linked List

  • Look at this instruction

RatNode head = new RatNode(new Rational(2,3), head);

– Compare this instruction to the one before.

slide-9
SLIDE 9

Linked List

  • Loop

For (int j= 0; j < max; j++) { // create a new object from Rational class, // this new object must have a reference in r // analyze this instruction head = new RatNode(r, head); }

slide-10
SLIDE 10

Linked List

summary – store many objects with references – are created by new