Download presentation
Presentation is loading. Please wait.
1
Linked Lists Damian Gordon
2
Linked Lists Imagine we wanted to have an array where we can dynamically add elements into it, and take them away. We can do this with a linked list.
3
Linked Lists A linked list is made up of nodes.
4
Linked Lists A linked list is made up of nodes.
Each node has two parts to it:
5
Linked Lists A linked list is made up of nodes.
Each node has two parts to it: Value Pointer
6
Linked Lists For example
7
Linked Lists For example 23
8
Linked Lists For example 23 62
9
Linked Lists For example 23 62 37
10
Linked Lists For example 23 62 37 31
11
Linked Lists For example NULL 23 62 37 31
12
Linked Lists For example NULL 23 62 37 31 Start of List End of List
13
Linked Lists To add a value in: NULL 23 62 37 31
14
Linked Lists To add a value in: NULL 23 62 37 31 26
15
Linked Lists To add a value in: NULL 23 62 37 31 26
16
Linked Lists To add a value in: NULL 23 62 37 31 26
17
Linked Lists To add a value in: NULL 23 62 26 37 31
18
Linked Lists To delete a value: NULL 23 62 26 37 31
19
Linked Lists To delete a value: NULL 23 62 26 37 31
20
Linked Lists To delete a value: NULL 23 62 37 31
21
Linked Lists So a Linked List is a series of Nodes
And a Node has two parts Value Pointer A Linked List has a pointer to the start of the list called Head.
22
Linked Lists To add a value in: Value Pointer NULL 23 62 26 37 31 HEAD
23
Linked Lists TYPE Node: INTEGER Value; NODE Pointer; ENDTYPE;
24
Linked Lists We will look at implementing the following modules:
CreateList() Create an empty linked list DeleteList() Delete a linked list IsEmpty() Check if the linked list is empty
25
CreateList()
26
Linked Lists To create a list: NULL HEAD
27
Linked Lists MODULE CreateList(): Head <- NULL; END.
28
DeleteList()
29
Linked Lists To delete a list: Value Pointer NULL 23 62 26 37 31 HEAD
30
Linked Lists To delete a list: Value Pointer NULL 23 62 26 37 31 HEAD
31
Linked Lists MODULE DeleteList(): Head <- NULL; END.
32
IsEmpty()
33
Linked Lists To check if a linked list is empty: NULL HEAD
34
Linked Lists MODULE IsEmpty(): Boolean Empty; IF Head = NULL THEN Empty <- True; ELSE Empty <- False; ENDIF; RETURN Empty; END.
35
Linked Lists Or MODULE IsEmpty(): RETURN Head = NULL; END.
36
Linked Lists We will look at implementing the following modules:
DisplayList() print out all the nodes FindNode(N) Find a node with a given value (N) InsertNode(Pos, N) Insert a node at a particular position DeleteNode(N) Delete a node with a given value (N)
37
DisplayList()
38
NULL Current HEAD Linked Lists
To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD
39
NULL Current HEAD Linked Lists
To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD
40
NULL Current HEAD Linked Lists
To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD
41
NULL Current HEAD Linked Lists
To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD
42
NULL Current HEAD Linked Lists
To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD
43
NULL Current HEAD Linked Lists
To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD
44
Linked Lists MODULE DisplayList(): Node Current <- Head; WHILE (Current.Pointer != NULL) DO Print Current.Value; Current <- Currrent.Pointer; ENDWHILE; END.
45
Linked Lists To print out a count of the number of nodes:
46
Linked Lists MODULE DisplayList(): Node Current <- Head; Integer CountNodes <- 0; WHILE (Current.Pointer != NULL) DO Print Current.Value; Current <- Currrent.Pointer; CountNodes <- CountNodes + 1; ENDWHILE; Print “Number of Nodes:”, CountNodes; END.
47
Linked Lists MODULE DisplayList(): Node Current <- Head; Integer CountNodes <- 0; WHILE (Current.Pointer != NULL) DO Print Current.Value; Current <- Currrent.Pointer; CountNodes <- CountNodes + 1; ENDWHILE; Print “Number of Nodes:”, CountNodes; END.
48
FindNode(N)
49
NULL Current HEAD Linked Lists To find a value (e.g. 37) in the list:
23 62 26 37 31 Current HEAD
50
NULL Current HEAD Linked Lists To find a value (e.g. 37) in the list:
23 62 26 37 31 Current HEAD
51
NULL Current HEAD Linked Lists To find a value (e.g. 37) in the list:
23 62 26 37 31 Current HEAD
52
NULL Current HEAD Linked Lists To find a value (e.g. 37) in the list:
23 62 26 37 31 Current HEAD
53
Linked Lists MODULE FindNode(N): Node Current <- Head; WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Current <- Current.Pointer; ENDWHILE; IF Current.Pointer = NULL THEN Print “Value not found”; ELSE Print “Value found”; ENDIF; END. HEAD Current
54
InsertNode(Pos, N)
55
NULL Current HEAD Linked Lists
To add in a value after position 4, of value N: NULL 23 62 26 37 31 Current HEAD
56
NULL Current HEAD Linked Lists
To add in a value after position 4, of value N: NULL 23 62 26 37 31 Current HEAD
57
NULL Current HEAD Linked Lists
To add in a value after position 4, of value N: NULL 23 62 26 37 31 Current HEAD
58
NULL Current HEAD Linked Lists
To add in a value after position 4, of value N: NULL 23 62 26 37 31 Current HEAD
59
NULL Current HEAD Linked Lists N
To add in a value after position 4, of value N: NULL 23 62 26 37 31 N Current HEAD
60
NULL Current HEAD Linked Lists N
To add in a value after position 4, of value N: NULL 23 62 26 37 31 N Current HEAD
61
NULL Current HEAD Linked Lists N
To add in a value after position 4, of value N: NULL 23 62 26 37 31 N Current HEAD
62
Linked Lists First declare the important variables, and then create a new node: MODULE InsertNode(Pos, N): Node Current <- Head; Integer PositionCounter; PositionCounter = 1; Node NewNode; NewNode.Value <- N; N
63
Linked Lists Next let’s find the position in the linked list:
WHILE (Pos > PositionCounter) DO Current <- Current.Pointer; PositionCounter <- PositionCounter + 1; ENDWHILE;
64
Linked Lists Now add to the start if it’s Pos 0, otherwise add it in after the position pointed to by Current. IF Pos = 0 THEN NewNode.Pointer <- Head; Head <- NewNode; ELSE NewNode.Pointer <- Current.Pointer; Current.Pointer <- NewNode; ENDIF; NewNode HEAD Current
65
Linked Lists MODULE InsertNode(Pos, N): Node Current <- Head; Integer PositionCounter; Node NewNode; NewNode.Value <- N; WHILE (Pos > PositionCounter) DO Current <- Current.Pointer; PositionCounter <- PositionCounter + 1; ENDWHILE; IF Pos = 0 THEN NewNode.Pointer <- Head; Head <- NewNode; ELSE NewNode.Pointer <- Current.Pointer; Current.Pointer <- NewNode; ENDIF; END.
66
DeleteNode(N)
67
NULL Previous Current HEAD
Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD
68
NULL Previous Current HEAD
Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD
69
NULL Previous Current HEAD
Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD
70
NULL Previous Current HEAD
Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD
71
NULL Previous Current HEAD
Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD
72
Linked Lists Check if the required value is in the first node, if so, move the HEAD to the next node, and then it’s deleted: MODULE DeleteNode(N): Node Previous <- NULL; Node Current <- Head; IF Current.Value = N THEN Head <- Current.Pointer; ENDIF; END. HEAD
73
Linked Lists Otherwise find the node in the same way as the previous function: WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Previous <- Current; Current <- Current.Pointer; ENDWHILE; HEAD Previous Current
74
Linked Lists And then delete it: IF (Current.Value = N)
THEN Previous.Pointer <- Current.Pointer; ELSE PRINT “Not found” ENDIF; HEAD Previous Current
75
Linked Lists MODULE DeleteNode(N): Node Previous <- NULL; Node Current <- Head; IF Current.Value = N THEN Head <- Current.Pointer; ELSE WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Previous <- Current; Current <- Current.Pointer; ENDWHILE; IF (Current.Value = N) THEN Previous.Pointer <- Current.Pointer; ELSE PRINT “Not found” ENDIF; END.
76
Linked Lists To print out a count of the node number where the delete was:
77
Linked Lists MODULE DeleteNode(N): Node Previous <- NULL; Node Current <- Head; Integer PositionCounter; IF Current.Value = N THEN Head <- Current.Pointer; ELSE WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Previous <- Current; Current <- Current.Pointer; PositionCounter <- PositionCounter + 1; ENDWHILE; IF (Current.Value == N) THEN Previous.Pointer <- Current.Pointer; Print “Node deleted at position:”, PositionCounter; ELSE PRINT “Not found” ENDIF; END.
78
Linked Lists MODULE DeleteNode(N): Node Previous <- NULL; Node Current <- Head; Integer PositionCounter; IF Current.Value = N THEN Head <- Current.Pointer; ELSE WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Previous <- Current; Current <- Current.Pointer; PositionCounter <- PositionCounter + 1; ENDWHILE; IF (Current.Value == N) THEN Previous.Pointer <- Current.Pointer; Print “Node deleted at position:”, PositionCounter; ELSE PRINT “Not found” ENDIF; END.
79
etc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.