linked lists - unbaubanel/cs2023/linked_lists.pdfdeleting a node from a linked list 1. locate the...

Post on 31-Jul-2020

8 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Linked Lists

CS2023 Winter 2004

Outcomes: Linked Lists

� C for Java Programmers, Chapter 11 (11.4.9) and C Programming - a Modern Approach, Chapter 17 (17.5)

� After the conclusion of this section you should be able to

� Write modules using linked lists

� Begin creating other similar data structures, such as trees

Linked Lists

� Chain of structures (nodes) each containing pointer to next node in chain

� More flexible than array

� easily insert/delete nodes

� but lose random access to elements

� accessing node fast if node is close to the beginning, but slow if node is near the end of the list

Declaring a Node Type

� Define

��� � � � � to improve maintainability of code:

� � � � ��� � � � � � � �

� � � � � ��� � � � � � � �

� � � � � �� � � �

� �� � � � � � � � � � � �

� � � "! � � � # �

� The value of � � � will be

$ % %

if there is no next element, otherwise it will be a structure representing the next element.

must use structure tag

Declaring a Node Type

& Need variable that always points to first node in list (C for Java Programmers uses another structure to do this):

'�( )�* +, -. /0 1 2 '3 4 4�5

6 - . /0 1 initialized to ' 3 4 4 to indicate that list is

initially empty

Creating Nodes

1. Allocate memory for the node

2. Store data in the node

3. Insert the node into the list

1. Need variable to point to the node temporarily:

7�8 9�: ;< =: > 7�8 9: ?Allocate memory for the new node:

@ A B B =: > 78 9: C DE F F 8 G B�H @JI : 8 A B 78 9: ; K K K

C C 7L M M K NO O PO ? QSR T QSU V R

W X Y[Z \ ] \^ _

Creating Nodes

2. Store data in the ` a bJc d member of the new node:

e df g�h i dkj l ` a b c d m n oqp

3. Insert node into list

r inserting in at beginning of list is easiests tuv w xSy z v

{ | }[~ � � �� �

Inserting Node at Beginning of List

1. Modify the new node's �� � �

member to point to the node that was previously at beginning of list:

�� � ��� � � � � �� � � � �� �� ���2. Make

� � � � �

point to the new node:

�� �� � � � � � �� � � �

Inserting Node at Beginning of List

1. �� � �[� �� � � �� � � � �  ¡¢ �¤£2.

�   ¡¢ � � �� � � � � � £

¥ ¦§¨ © ªS« ¬­¨

® ¯ °[± ² ³ ²´ µ

¶· ¸¹ º

¥ ¦§¨ © ªS« ¬¨

® ¯ °[± ² ³ ²´ µ

¶· ¸¹ º

Inserting Node at Beginning of List

Add another node:

»¼ ½ ¾[¿ À ¼ Á ÂÃ Ä Ä ¿ Å ÆkÇ ÈÊÉ ¼ ¿ Ë Æ ¾[¿ À ¼ Ì Í ÍÊÎ

»¼ ½ ¾[¿ À ¼ Ï ÐÑ Ã ÄÊÒ ¼ Á ÓÔ Î

Õ Ö×Ø ÙÚ Û

Ü ÖÝÞ ß àSá âÞ

Inserting Node at Beginning of List

Insert new node at beginning of list

ãä å æ[ç è ä é ê ãä ë ì í îï ðñ ì¤ò

îï ð ñ ì í ã ä å æç è ä ò

ó ôõö ÷ø ù

ú ôûü ý þSÿ �ü

ó ôõö ÷ø ù

ú ôûü ý þSÿ �­ü

Inserting Node at Beginning of List

��� ��� �� � � � � � � � � � � � � �� � � � �� ��� � ��� � � ��

�� � �� �� � �� �

� � � � �� � �� � � � � � "! � � � � � � � � �� � � � � �# $ $ �

� � �% &' �(

�) ' $ # *% � �

� � � + ,- � � �� � � �

� � � + , � � � � � � �

� � � � � �� �

.

Inserting Node at Beginning of List

/0 12 3 4 0 52 6 1 3 7 18 5 3 9 /0 12 3;: < = >@?

/0 12 3 4 0 52 6 1 3 7 18 5 3 9 /0 12 3;: A = >@?

How to get

B�C DE F G H FI C G

to update

JK LM N directly, rather than

return a new value of

OP QR S

?

T U VW VYX Z[ \ ] ^ \ UX ] _` U W [ ab c V \ Z ];d egf ] f aih j[ W k l

` U W [ ab f mn o

V c _ _ f mn p qf r r Us _ Z V@t [ U c _` U W [ a k k k p p ` uv v k

[ n V ] _w xy az

^{ y v u| w k o

f mn } ~ Tf r m[ p W o

f mn } ~ X [ n ] p c V \ Z ] o

c V \ Z ] p f mn o

Inserting Node at Beginning of List

� Recall that pointers, like other arguments, are passed by value.

� Need to pass a pointer to

�� �� �

� � � � � �� � � � � � � � � �� � � � �� � �� �� ��� � �g� � � �i� � � �� �

� � � � �� � �� �

� � � � � �� � �� � � �  � � � ¡ � � � �� � � � � � � � � � � ¢£ £ �

� � � � �¤ ¥¦ �§

�¨ ¦ £ ¢© ¤ � �

� �� ª « �� � � � � � �

� �� ª « � � � � � � � � �� �� �

� �� �� ��� � � �� �

¬

Call:

­Y® ¯° ± ² ³ ±´ ® ² µ¶ · ­ ± ¯ ²;¸ ¹ º »@¼

Searching a Linked ListIdiom for traversal of a list:

½@¾ ¿ ÀYÁ  ½Ã ¿Ä ÅÇÆ Á È Â ÉÊ Ë ËÆ Á  Á Ì ÍÎ ÏÐ Å Ñ

Ò Search a list for data n (of type

ÓgÔ Õ Ô Öi× ØÙ ).

Ú return pointer to node containing n,

Û otherwise return null pointer:

ÜgÝ ÞYß àá â ß ãä å æç è â é ê ÜgÝ Þß àá ë è ä â é;ì í ã é ã àiî ï ß ð ñ ò

ÜgÝ ÞYß àá ïó

ëÝ ä ê ï ô ë è ä â é ó ï õ ô Üö ç ç ó ï ô ïi÷ ø ð ß ù é ñ

è ë ê ïi÷ øú ã û@ü ß ô ô ð ñ

ä ß é ü ä ð ïó

ä ß é ü ä ð Üö ç ç ó

ý

Searching a Linked List

Since

þÿ �� � passed by value, can modify it:

��� ��� � � � � �� � � � � ��� �� � � � ��� � � � � �� � � � � �

�� ��� � � � � ! �" � � � � � � � ! � � � �$# % �� & � �

� � � � � � �# %' � (�) � ! ! � �

� � ) � � � � ��

� � ) � �" � � �

*

Deleting a Node from a Linked List

1. Locate the node to be deleted

2. Alter previous node so that it bypasses deleted node

3. Call

+-, . . to reclaim space occupied by deleted node

1. Need pointer to previous node as well as current node:

/-0 1 243 5 1 6 /7 18 9;: < 1= > 6 ?@ A ACB

3 5 1 D 6 ? @ A A E E 3 5 1�F G > H I 5= D 6 JB

< 1= > 6 3 5 1: 3 5 1 6 3 5 1F G J= K 9 L

B

Deleting a Node from a Linked List

2. MN OP Q RS OT U V WX N Q R S OT UZY

3.

[-\ ] ] ^4_ ` \ a-b

c de df gih j k l dm d

n h op qr hs t uv

w�x y�z {| y�z } z ~ z ��� x � � ��� ~ � wx yz {| � � � � ~�� ��� ~ � {�� � z � � �

w�x y�z {| �� � � �� z ��

�x � � �� � � � �� � ~� �� z � � w� � � �

�� � � � w� � � � � �� � � � �� }�� z � � � �

�� z � � �� � � �� � � �� � � � � z � ~ �

�� � � �� � � � w� � � �

� z ~ � � � � �� � ~ � �� � �� � � x ~ �x � � y � �

� � � �� z � � � w� � � � �� � � � � �� � ~ � x y z � �

� �� � ~ � � �� � ~ � � �z � ~ �

z } � z �� � � � � x � z x ~ �z � � x y�z � �

�� z � � � � z � ~ � �� � � � � z � ~ �

�� z z � �� � � �

� z ~ � � � � � � � ~ �

1. Remove all nodes one at a time from the beginning of the list

2. Set pointer to first element to null.

�   ¡¢ ¢ £¤ ¥§¦ ¨ © ¥ ª ¡�¤ ¥ «¬   ¢ £ ­® ¯ ° ¡¦ ¤ ¥§± ²

³

´ µ ¡¶ £ « ¢ £ ¶ £ ¥ £ · ¡¦ ¤ ¥ « ° ¡¦ ¤ ¥± ² ²

¸¯ ° ¡¦ ¤ ¥± ¹ ¬ º ª ª ¸

»

Call:

¼�½ ¾ ¿§À Á  ¿ Ã Ä ¾ ¿ ÅÆ Ç ÄÀ ¾ ¿ È�É

Deleting an Entire Linked List

Ê�Ë Ì Í�Î Ï Î Ì Î Ð Ê�Ñ Ò Ì ÓÔ Õ Í�Î Ö× Ø Ù Ê�Ñ Ò Ì§Ú Û

ÜÔ Õ Í�Î Ö × ÝÞ ß à Ø Ù Ê�Ñ Ò Ì§Ú á

Ê Ù Ó ÝÞ ß à à Ô âã ã Û ä Ø Î åÚ Ìçæ Ï Ê Ò Ì Ø ä

Ñ Î Ì Þ Ñ Ë è á

Ø Ù Ê�Ñ Ò Ì§Ú à ÝÞ ß�é ê Ë Î ß Ì á

ÙÑ Î Î Ó ÝÞ ß Û á

Ñ Î Ì Þ Ñ Ë ë á

ì

Deleting the First Node

Ordered Lists

í Searching is faster than unordered linked list

î can stop after reaching point where desired node would have been located

ï Inserting a node is more difficult

ð Illustrate using a parts database

ñ see C Programming: a Modern Approach, p. 379

Parts Database

òçó ôõ öõ ÷ ø ò§ù ú û ò ôü ù ò ý

þ�ÿ ò ÿ ú � � õ ù �

û �ü ù ÿ ü �õ �� �� �

�� � � �

þ�ÿ ò �ÿ �ü ÿ ö �

ø ò§ù ú û ò ô ü ù ò � ÿ õ � ò �

� �ü ù ò ��� � �ü ù ò � � �

�ü ù ò � � þÿ �õ ÿ ò �ù ó � � � �

(here

��� � � � �! "# is a global variable)

Find Part in InventoryLook up a part number in inventory and return pointer to node containing part number. If not found, return

$% & &

')( * + , ' -. / 0 ')( * + 1 . / + /2 3 4�5 * 687

Find Part in InventoryLook up a part number in inventory and return pointer to node containing part number. If not found, return

9: ; ;

<)= > ? @ < AB C D <)= > ? E B C ? CF G H�I > J

K<)= > ? @ < LM

A8N > E L O B CP I C ? N >Q M

L R O ST U U V V CF G H�I > W LYX W C F G HI > M

L O LYX W CI Z ? J

MB A E L R O ST U U V V C F G HI > O O LYX W C F G HI > J

>I ? F > C LM

>I ? F > C ST U UM

[

\ ] ^_ `a bc d ef \ ] ^_ g8h

i Prompt user for part number then look it up in inventory. If part exists, print name and quantity; if not, print an error message

Prompt user for part number then look it up in inventory. If part exists, print name and quantity; if not, print an error message

j k lm no pq r st j k lm u

vl�w x w y z {o q |

} p q x ~ } � |

� q l�w x �t �� w x o q � pq x w y z {o q � � u |

n r pw �t �� m ��� �w y z {o q u |

� � � l�w m } pq x t w y z {o q u |

l � t � � � �� � � u v

� q l�w x �t � } pq x w p zo � � n �w ��� �Y� � w p zo u |

� q l�w x �t �� y pw x l x�� � � m �w ��� �� � k w � pw m u |

� o � no� q l�w x �t � } pq x w k x � k y w m �w � u |

� � �� � � � � �!� � � � ��  8¡

¢ Prompt user for part number. Print error message if part doesn't exist; otherwise prompts user to enter change in quantity and updates the inventory

Prompt user for part number. Print error message if part doesn't exist; otherwise prompts user to enter change in quantity and updates the inventory

£ ¤ ¥¦ § ¨ ¦ © ª!« ¬ £ ¤ ¥¦ ­

®¥�¯ ª ¯ § ° ±« ²´³ µ ¶ © ¯ ·« ¸

¹ © ² ª º ¹ ¨ ¸

¨ ² ¥�¯ ª » ¬¼ ½ ¯ ª!« ² ¨ © ² ª ¯ § ° ±« ²¾ ¼ ­ ¸

¿ µ ©¯ » ¬ ¼ À¦ ¼ ³ Á¯ § ° ±« ² ­ ¸

¨  » ¥�¯ ¦ ¹ © ² ª ¬ ¯ § ° ±« ² ­ ¸

¥ » ¬ ¨ Ã Â ÄÅ Æ Æ ­ ®

¨ ² ¥�¯ ª » ¬ ¼ ½)¯ ª!« ² µ ¶ ©¯ ·« ¥�¯ Ç § © ¯ ª ¥ ª�È ¾ ¼ ­ ¸

¿ µ ©¯ » ¬ ¼ À¦ ¼ ³ Á µ ¶ ©¯ ·« ­ ¸

¨YÉ Ê ¤ ¯ Ë © ¯ ¦ Ì Â µ ¶ ©¯ ·« ¸

Í « Î ¿«¨ ² ¥�¯ ª » ¬ ¼ ¹ © ² ª ¯ ¤ ª » ¤ § ¯ ¦ ϯ ¼ ­ ¸

Í

Insert Part in InventoryPrompts user for information about a new part and then inserts it into the inventory list. The list remains sorted by part number

Ð Ñ ÒÓ ÒÕÔ Ö× Ø Ù Ú Ð Ñ Ò Ó ÛÝÜ

Insert Part in InventoryPrompts user for information about a new part and then inserts it into the inventory list. The list remains sorted by part number

Þ ß àá àÕâ ãä å æ ç Þ ß à á è

éêìë å æ í ê îï åñð ò åä Þð â ä ó ô ß á ä õ

â ä ó ô ßá ä ö ÷ë ø ø ß î ç ã àÝù ä ß ú ç ê ë å æ í è è õ

à ú ç â ä ó ô ß á ä ö ö ôû ü ü è é

ò å à â æ ú çý þ ë æ ë ÿ ë ãä à ú úï ø ø� â ý è õ

åä æ ï å â õ

�� ��� � � � � � � � �� � � � � � �� � � � �

�� � � � � � �� �� � � ��� �� ! � � � �� � � �

�"� � � �� # ��� $� � � � � %� �� � $ # �& ' ' �

� �� ( # � & ' ' � � � � � � � �� ! � � � �� � ! � �� ! � � � �� � �

�� � $ # � �� � � �� # � �� ! � � ) ��

�� � � �� ( # � & ' ' � � � � � � � �� ! � � � �� � # #

� �� ! � � � �� � � *

�� �� � � + � � � � ,� � � � % � ) � � � � -� � �

�� � � � � � � � �� � �

� � � �� � �

.

/0 1�2 3 45 67 2 3 8 0 /9 0 3 2 9 :8 ; 6< =

0 8 9 >? 12 8 5 2 8 @ A�B >8 C D 2 9 :8 E AF G7H

? 7 A< =

/0 1�2 3 45 67 2 3 8 0 IJ 9 2 3 1 3LK B 2 M9 2 > ; 6< =

NO 9 2 45 6P > 6 E Q2 8 @ AB >8 C D B 2 R9 2 >< =

2 8 @ AB >8 C D 2 8 S 3 T O J 0 =

1 4 5 /0 8 U T T AV ? ? <

1�2 U8 2 3 B 0 K T 2 8 @ AB >8 =

8 W N8/0 8 U C D 2 8 S 3 T 2 8 @ AB >8 =

X

top related