06 ds linked lists 1

24
Linked Lists (Part 1) Data Structures

Upload: min-min

Post on 13-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 1/24

Linked Lists

(Part 1)

Data Structures

Page 2: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 2/24

CT077-3-2-DSTR Data Structures 2

Objectives

By the end o this !esson" you #i!!$

• Learn a%out !inked !ist data structure

• Beco&e a#are o the %asic 'ro'erties o !inked

!ists• Be a%!e to i&'!e&ent so&e o the %asic

o'erations on !inked !ists

Discoer ho# to create" &ani'u!ate" and use!inked !ist o%ects

Page 3: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 3/24

CT077-3-2-DSTR Data Structures 3

Introduction

•  * co!!ection o ite&s can %e or+ani,ed and'rocessed in &e&ory usin+ a !ist data structure –  * !ist as an a%stract ty'e is a !inear structure #hich

contains a seuence or e!e&ents (irst e!e&ent"

second e!e&ent" !ast e!e&ent" 'reious e!e&ent" etc.)

•  * 'articu!ar e/a&'!e o !ists is array (array !ist)

 – *dacent chunks o &e&ory" #here e!e&entsare accessed direct!y throu+h inte+er indices

 b e a d a

0 1   2 3   4   …

Page 4: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 4/24

CT077-3-2-DSTR Data Structures

Introduction

•  *nother e/a&'!e o !ists is Linked Lists

•  *rray !ists hae adanta+es

 – ast rando& access to any e!e&ent

• %ut a!so hae disadanta+es –

 *rray si,e is i/ed (chan+in+ it is cost!y) – nsertion o ne# e!e&ents and de!etin+ e/istin+

e!e&ents are s!o# o'erations (un!ess ha''enin+ at

the end)

hy need

one4

Page 5: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 5/24

CT077-3-2-DSTR Data Structures 5

• t is a series o connected nodes" #here each

node is a data structure

• 6ery node (e/ce't !ast)

 – Contains address o the ne/t (o!!o#in+) node• ode co&'onents

 – Data$ stores re!eant inor&ation

 –

Link$ stores address

Linked Lists

Structure o a node

Page 6: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 6/24

CT077-3-2-DSTR Data Structures 8

•  *n arro# 'oints to the o!!o#in+ node9s address

 – * 'ointer a!ue stored in node

• Do#n arro# in !ast node indicates :LL !ink ie!d"

i.e. a 'ointer 'ointin+ at nothin+ – ; &ark is a!so used in dia+ra&s instead o do#n arro#

• <ead (or irst) address a!!o#s access to !iste!e&ents

 – *ddress o the irst node in the !ist

Linked Lists

Linked !ist

Page 7: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 7/24CT077-3-2-DSTR Data Structures 7

• 6/a&'!e$ a !ist containin+ t#o e!e&ents (5 and 85) – su''osin+ the irst node is at &e&ory !ocation

1200" and the second node is at &e&ory

!ocation 1575" !inked !ist #i!! !ook !ike this

 – =ost o the ti&e" #e ust dra# arro#s to indicate

'ointers 'ointin+ at &e&ory !ocations" since the

e/act address a!ues are not o hi+h i&'ortance

Linked Lists

Linked !ist and a!ues o the !inks

0

Page 8: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 8/24CT077-3-2-DSTR Data Structures >

Linked Lists

• Because each node o a !inked !ist has t#o

co&'onents" #e need to dec!are each node as a

c!ass (or struct)

 –Data ty'e (or ino) o a node de'ends on thes'eciic a''!ication (!inked !ist o #hat4)

 – The !ink co&'onent o each node is a 'ointer 

struct NodeType {int info;NodeType* link;

};

class NodeType {public:

int info;NodeType*

link;};

?r a struct can %e used si&i!ar!y

Page 9: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 9/24CT077-3-2-DSTR Data Structures @

Linked Lists: Some Properties

•  * head aria%!e is dec!ared as

and it stores address o irst node in the !ist

• or each node – no stores inor&ation

 – Link stores address o ne/t node

• or e/a&'!e" #e can hae the o!!o#in+ !ist

NodeType *head;

Page 10: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 10/24CT077-3-2-DSTR Data Structures 10

Linked Lists: Some Properties

Page 11: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 11/24CT077-3-2-DSTR Data Structures 11

Linked Lists: Some Properties

• NodeType * current = head;

 – Co'ies a!ue o head into current

Aa!ue

current

current-ino

current-!ink

current-!ink-ino

@2

2000

17

2>00

Page 12: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 12/24CT077-3-2-DSTR Data Structures 12

Linked Lists: Some Properties

• current = current->link;

 – Co'ies a!ue o current->link (2>00)

into current

Aa!uecurrent

current-ino

current-!ink

current-!ink-ino

83

2>00

@2

1500

Page 13: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 13/24CT077-3-2-DSTR Data Structures 13

Linked Lists: Some Properties

Aa!ue

head-!ink-!ink

head-!ink-!ink-ino

head-!ink-!ink-!ink

head-!ink-!ink-!ink-ino

current!ink-!ink

current-!ink-!ink-ino

current!ink-!ink-!ink

current!ink-!ink-!ink-ino

5

1500

83

3800

Does not e/ist

3800

5

0 (that is" :LL)

+ies runti&e error 

Page 14: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 14/24CT077-3-2-DSTR Data Structures 1

Traversing a Linked List

• The %asic o'erations o a !inked !ist are$ – Deter&ine the si,e o the !ist

 – nsert an ite& in the !ist

 – Search to deter&ine i an ite& is in the !ist –  *ccessin+ !ist e!e&ents (+et and set e!e&ent at inde/ i)

 – De!ete an ite& ro& the !ist

• These o'erations reuire !ist traersa! – ien a 'ointer to the irst node o the !ist" ste' throu+h

the nodes o the !ist

 – List si,e can %e o%tained #ithout traersa!" %y &odiyin+

the data structure (&ore on this !ater)

Page 15: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 15/24CT077-3-2-DSTR Data Structures 15

Traversing a Linked List

• To traerse a !inked !ist$

• 6/a&'!e" 'rintin+ !ist9s e!e&ents$

Page 16: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 16/24CT077-3-2-DSTR Data Structures 18

Getting Linked List Size

• :sin+ traersa!" ho# to ca!cu!ate the nu&%er o

e!e&ents4

• s there a %etter #ay to kno# !inked !ist9s si,eother than scannin+ the #ho!e !ist eeryti&e4

current = head;int size = 0;

while(current = N!""#{size$$;current = current%&link;

}

Page 17: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 17/24CT077-3-2-DSTR Data Structures 17

Create a LinkedList Structure

• Create a si,e ie!d (data &e&%er) enca'su!ated in theLinkedList data structure (initia!i,ed to ,ero)

• :'date the ie!d u'on insertion and de!etion (1 or -1)

class "inked"ist {

public:NodeType* head;int size;

  int 'etize(#{int size = 0;NodeType * current = head;

while(current = N!""#{  size$$;  current = current%&link;}return size;

  }

};

X

This is a si&'!e e/a&'!eo ho# a data structure

and an a!+orith&

coo'erate to i&'!e&ent

eicient so!utions.

 

e traded o e/tra s&a!!&e&ory (not necessari!y

a!#ays s&a!!) to +ain

s'eed in ca!cu!atin+ the

si,e o the !inked !ist.

Page 18: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 18/24CT077-3-2-DSTR Data Structures 1>

Inserting a Ne !"ement

• e can insert at the %e+innin+ o the !ist" at the

end o it" or at +ien 'osition

  "inked"ist(#{this%&size = 0;

this%&head= N!"";  }

  )oid insertt+e'innin'(int )alue#{NodeType * newNode= new NodeType;newNode%&info = )alue;

newNode%&link = head;head = newNode;size$$;

  }

  ,, -- the other i.ple.ented .ethods};

class NodeType {public:

int info;NodeType* link;

};

class "inked"ist {

 public:

NodeType* head;int size;

Page 19: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 19/24

CT077-3-2-DSTR Data Structures 1@

Inserting a Ne !"ement

• Dra# a dia+ra& re'resentin+ #hat ha''ens in

the &e&ory o$

)oid .ain(#{

"inked"ist l/;"inked"ist l;

l/-insertt+e'innin'(1#;l/-insertt+e'innin'(2#;l/-insertt+e'innin'(3#;

cout 44 l/-'etize(# 44 endl;

l-insertt+e'innin'(2#;l-insertt+e'innin'(5#;cout 44 l-'etize(# 44 endl;

}

Page 20: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 20/24

CT077-3-2-DSTR Data Structures 20

#$% t$e Ne Node S$ou"d be in

t$e &eap'

• hat ha''ens i #e chan+e the i&'!e&entation

o insert so that ne# node is on the stack4

)oid insertt+e'innin'(int)alue#{

  NodeType newNode;  newNode-info = )alue;  newNode-link = head;  head = 6 newNode;  size$$;}

)oid .ain(#{  "inked"ist list;

  list-insertt+e'innin'(1#;

  cout 44 list-head%&info;}

The o%ect ne#ode #i!! %e

de!eted ro& &e&ory ater

insert*tBe+innin+ unction

ends" and thereore the

head o the !ist #i!! %e a

dan+!in+ 'ointer.

Page 21: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 21/24

CT077-3-2-DSTR Data Structures 21

Inserting a Ne !"ement at t$e !nd

• rite the code that nai+ates unti! the !ast node"

and then inserts the ne# node there.  )oid insertt7nd(int )alue#{

NodeType * newNode= new NodeType;newNode%&info = )alue;newNode%&link = N!"";

if (head == N!""#  head = newNode;else{

  NodeType * last = head;  while(last%&link = N!""#

last= last%&link;  last%&link = newNode;}size$$;

  }

Create and setu'

the ne# node

the !ist is e&'ty

The +enera! casereuires indin+

the !ast node in

the !ist and &ake

its !ink 'oint at

the ne# node

Page 22: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 22/24

CT077-3-2-DSTR Data Structures 22

C"earing a Linked List

• So&eti&es you i!! u' a !ist" and #ant to e&'ty it so that it

can %e i!!ed u' a+ain #ith ne# e!e&ents

)oid clear (#{ 

NodeType * current = head;  while(head = N!""#{

current = current%&link;delete head;head = current;

  }

  size = 0;}

Page 23: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 23/24

CT077-3-2-DSTR Data Structures 23

Proper C"eaning o( t$e )emor%

• hen the dyna&ica!!y created nodes shou!d %e de!eted4

• &'!e&ent the destructor to de!ete a!! the nodes #hen the

!ist o%ect is de!eted

• hat is the out'ut o the o!!o#in+ &ain 'ro+ra&4

)oid .ain(#{  if (true# {

"inked"ist list; list-insertt+e'innin'(1#; list-insertt+e'innin'(2#;  }  "inked"ist list; 

list-insertt+e'innin'(3#;}

8"inked"ist(#{  cout 449oin' to delete all 944 size

449 ele.ents of the list-944 endl;  NodeType * current = head;

  while(head = N!""#{current = current%&link;delete head;head = current;

  }}

Page 24: 06 DS Linked Lists 1

7/24/2019 06 DS Linked Lists 1

http://slidepdf.com/reader/full/06-ds-linked-lists-1 24/24