linked lists

10

Upload: mallikamt

Post on 14-Feb-2017

143 views

Category:

Software


0 download

TRANSCRIPT

PowerPoint Presentation

What are linked lists?Structure to store data similar to a train.

DATAREF

DATAREF

DATAREF

DATAREFValueAddress

Why linked list?When the number of elements stored may change.Program reports students final scores from a Fall Java class:Array of 50 students

75834675951565.What if the number of students changes to 60 two weeks into the Fall quarter? We need a dynamic data structure.

75REF

83REF

46REF

75REFADD ME!!

Why linked list?Insertions and deletions achieved without moving all the elements.In an array:7583467595156512

..In a list:

75REF

83REF

46REF

75NULL

12REF

List in Java the Node structure

DATAREFValueAddress

public class Node {

//Data public String studentName; public int score; //Reference to next node public Node next;

}

Nodes and referencesNameScoreStacy Edmund75Huan Liu46Neil Walters83Erwin Espinoza75

Neil Walters83

Node@1034bb5Huan Liu46

Node@7f5f58971034bb5

List structureFirst In Last Out

ErwinREF

NeilREF

HuanREF

StacyNULLNameScoreStacy Edmund75Huan Liu46Neil Walters83Erwin Espinoza75

HH = Header NodeHH

Adding to a listAdd Cindy Chang at position 3

ErwinREF

NeilREF

HuanREF

StacyNULL

CindyREF

Deleting from a listRemove Neil Waters

ErwinREF

NeilREF

HuanREF

StacyNULL

CindyREF