Download presentation
Presentation is loading. Please wait.
Published byOctavia Phillips Modified over 8 years ago
1
Linked Lists and Generics Written by J.J. Shepherd
2
Data Structures Data structures are used to organize and store data. All the data structures are used to group together like data with some defined rules.
3
Arrays Great, and so far only, way to group together like data Random Access for data – Example: If we had an array named numbers and we wanted the access the 5 th element it would look like this number[5] Not perfect – Not resizable – If the amount of data isn’t known or fixed one can waste memory or have to create larger arrays
4
IS THERE ANYTHING WE CAN DO!?
7
Linked List Has nodes that contain – Data – Links to other nodes Resizable But no random access Head Current Tail This means null Previous
8
Linked List Generally all linked lists have these pointers which keeps track of certain nodes – Head: Points to the first element in the list – Tail: Points to the last element in the list – Current: Movable and points to a node of interest – Previous: Always stays one behind current (Optional) Head Current Tail Previous
9
Linked List They also generally have the following methods – Add: Adds a new node – Remove: Removes a certain node – Peek: Returns value of a certain node – Iterate: Moves the current pointer forward – Print: Prints the data for each node Head Current Tail Previous
11
Setting up a Linked Structure Linked structures should behave like a TYPE, thus it needs its own class Much link an array all of the information and the actions need to be internal We must create a class that represents the entire structure, but there’s some problems The structure is made of nodes which should be a class as well
12
Internal Classes You may define classes inside of classes public class LinkedList { private class Node { int data; Node link; } Node head; Node previous; Node current; }
13
Adding to a Linked List Adding to the linked list requires changing links to point to the new data Head Current Tail Previous
14
Adding to a Linked List Original List, soon we are adding a node after Current Head Current Tail Previous
15
Adding to a Linked List A new node is created Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }
16
Adding to a Linked List A new node is created Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }
17
Adding to a Linked List Always check if current is not null Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }
18
Adding to a Linked List New Node Link set to current link Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }
19
Adding to a Linked List Finally Current link is set to the new node Head Current Tail newNode Previous ListNode newNode = new ListNode(); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = newNode; }
20
Moving Through a Linked List This is called “Iterating” Current and previous keep track of iterations Head Current Tail Previous
21
Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;
22
Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;
23
Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;
24
Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;
25
Moving Through a Linked List Moving to next Head Current Tail Previous previous = previous.link; current = current.link;
26
Removing from a Linked List Removing an element requires changing links to skip over Head Current Tail Previous
27
Removing from a Linked List Original List, soon we are removing the node at Current Head Current Tail Previous
28
Removing from a Linked List There are a few cases we must consider when removing a node. The first one is removing a node from the middle of the list Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }
29
Removing from a Linked List If both current and previous are not null we know that the node we are removing is in the middle of the list Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }
30
Removing from a Linked List The previous’ link is now set to the current link to skip over that node Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }
31
Removing from a Linked List Finally the current point is set to the next one in the list Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }
32
Removing from a Linked List The node was removed as it has been skipped over Head Current Tail Previous if ((current != null) && (previous != null)){ previous.link = current.link; current = current.link; }
33
Removing from a Linked List Another case is when the current is pointing to the head Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }
34
Removing from a Linked List We can tell by seeing that the current is not null but previous is Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }
35
Removing from a Linked List Move the head to current’s link Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }
36
Removing from a Linked List Finally move the current to the head Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }
37
Removing from a Linked List The previous head node is deleted as the new head node has been moved forward Head CurrentTail Previous else if ((current != null) && (previous == null)){ head = current.link; current = head; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.