ds lec 5 linked list

14
7/23/2019 DS Lec 5 Linked List http://slidepdf.com/reader/full/ds-lec-5-linked-list 1/14  Linked List Data Structures Lecture # 5

Upload: williamsock

Post on 18-Feb-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 1/14

 

Linked ListData Structures

Lecture # 5

Page 2: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 2/14

 

Sequential Storage Structure

(Draw Backs)Storage requirements are unpredictable.

The precise amount of data storage in the

applications is data-dependent

The storage requirements cannot be easily

determined before the associated program is

executed.

Page 3: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 3/14

 

Singly inked ist

INFO LINK 

Page 4: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 4/14

 

Basic Concepts  (Linked List) The approach is to store the address of the successor of a particular

element in that element. Using this approach, the element of the linear

list need not be physically adjacent in memory. This method of

allocating storage is called linked allocation.

First node second node nd last node

last nodeFirst

*Pointer variable First contains the address or location of the

 first node in the list.

…………..

Page 5: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 5/14

 

inked ist

. !onsists of a series of structures" hich are not necessarily ad$acent in memory.

. %ach structure contains the element" and a pointer to a record containing its

  successor&The 'ext (ointer).

. Last cell*s Net pointer contains +ero" hich is typically ,non as null pointer.  'LL - defined in iostream.h/

  0111 211 30 44 4

a1

a2

a3

a4

a5

a1 a2 a3 a4 a5800 712 992 692 0

A Linked List

Page 6: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 6/14

 

!"rant# !$ohn# !Paul# !%ic&#

 '((( ')(( '(( '+((

 

0511 0611 011

 

inked ist

Page 7: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 7/14 

inked ist (Insertion)

  !en#

  !"rant# !$ohn# !Paul# !%ic&#

  '((( ')(( '(( '+((

 

'-((

  0511 0611 011

  0711

 0611

Page 8: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 8/14 

inked ist (Deletion)

 

!"rant# !$ohn# !Paul# !%ic&#

'((( ')(( '(( '+((

 

0511 0611 011

  011

Page 9: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 9/14 

Linked Vs Sequential Allocation Lists !onsider" the operations of insertion and deletion in a

sequentially allocated list. 8ssume that e ha9e an n-element list and that it is required to

insert a ne element beteen the second and the thirdelements.

:n this case" the last n ; elements of the list must bephysically mo9ed to ma,e room for the ne element.

For large-si+ed lists" hich are sub$ected to many insertions"this insertion approach can be 9ery costly.

The same conclusion holds in the case of deletion" since allelements after the deleted element must be mo9ed up so as touse the 9acated space caused by the deletion.

Page 10: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 10/14 

Linked Vs Sequential Allocation Lists

These to allocation methods &lin,ed and sequential) can becompared ith respect to other operations as ell.

For a search operation in a lin,ed list" e must follo the lin, from the

first node onards until the desired node is found. This operation is certainly inferior to the computed-address method oflocating an element in a sequentially allocated list.

:f e ant to split or $oin to lin,ed lists" then these operations in9ol9ethe changing of pointer fields ithout ha9ing to mo9e any nodes.

Such is not the case for sequentially allocated counterparts. !learly" pointers or lin,s consume additional memory. The cost of this additional memory becomes less important as the

information contents of a node require more memory.

Page 11: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 11/14

 

Dynamic List Operations The !ommon operations on Dynamic lists are<

'. initialie/ initiali+es the lin,ed list to be empty.+. is0mpty/ returns =true> as the function result if list contains no

element" or otherise returns =false>.-. addTo1ead/ inserts an element in front of the list.. addToTail/ inserts an element at the end of the list.). deleteFrom1ead/ deletes the first element of list.2. deleteFromTail/ deletes the last element of the list.3. delete/ deletes an entry from the list.4. find/ finds an element in the list.5. print/ prints the contents of the list.

'(. is6n7ist/ retruns =true> as the function result if element x is inlist" or otherise returns =false>.

''. 8nd so on i.e. as many operations as you re9uired.

Page 12: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 12/14

 

Implementation o Singly linked

list %9ery node of a lin,ed list has to fields" an information

field and a field hich points to the next node of a list.

Therefore" the class for the node of a list has to datamembers i.e.

8 data member to hold information ?

8 data member hich stores the address of next node of alist

Page 13: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 13/14

 

Class or nodes o t!e list

class 'ode @

public<

int infoA

'ode BnextA'ode&)<next&1) @C

'ode&int el" 'ode Bn 1) @

info elA

next nAC

CA

Page 14: DS Lec 5 Linked List

7/23/2019 DS Lec 5 Linked List

http://slidepdf.com/reader/full/ds-lec-5-linked-list 14/14

 

Class or nodes o t!e list'ode Bp ne 'ode&01)A

'ode Bq ne 'ode&2)A

p-/next qA

10

 p

8

q

q ne 'ode&0)A

12

q

p-/next-/next qA

  8s e see this is an incon9enientand cumbersome process.

Therefore