Linked Lists
Transcript of Linked Lists
-
Linked Lists
-
ArraysWhat is an array?An ordered collection of elements that have the same data type.An array contains a finite number of elements.Elements of an array can be accessed directly and in any order.An index, of value 0 or greater, is used to specify a particular element in an array.
-
ArraysTypes of Arrays:One-dimensionalTakes only one index to designate an element.Ex:double [ ] maxTemps = new double [DaysPerWeek]
MultidimensionalUses more than one index to designate an element.Ex:double [ ] [ ] minTemps = new double [ DaysPerWeek ] [WeeksPerYear]
-
Display Contents of an ArrayWhat would be the code for displaying the contents of an Array?What would be the runtime of this method?
-
Delete an Element in an ArrayWhat would be the code for deleting an element from an Array?What would be the runtime of this method?
-
Insert an Element into an ArrayWhat would be the code for inserting an element into an Array?What would be the runtime of this method?
-
Linked ListA linked list consists of a sequence of nodes.Each node contains a data item and a link (pointer or reference) to the next node.The last node contains a null link.The list can also have a head which is a pointer to the first node in the linked list.
-
Linked ListA nodes successor is the next node in the sequence.Note: the last node has no successor.A nodes predecessor is the previous node in the sequence.Note: the first node has no predecessor.
-
Pseudocodepublic class Cell { int value; Cell next;
public Cell (int v, Cell n) {// constructor value = v; next = n; }}
Cell temp = new Cell(17, null);temp = new Cell(23, temp);temp = new Cell(97, temp);Cell myList = new Cell(44, temp);
-
Pseudocode
-
Singly-Linked ListThe previous example was a singly-linked list.Each node contains a value and a link to its successor (the last node has no successor)The header points to the first node in the list (or contains the null link if the list is empty)
-
Traversing a SLLWhat would be the code for traversing a SLL?What would be the runtime of this method?
-
Traversing a SLL
-
Inserting a Node in a SLLThere are many ways you might want to insert a new node into a list:As the new first elementAs the new last elementBefore a given node (specified by a reference)After a given nodeBefore a given valueAfter a given valueAll are possible, but differ in difficulty
-
Inserting as the First ElementWhat would be the code?What would be the runtime of this method be?Would this work correctly on an empty list?
-
Inserting after a given valueWhat would be the code?What would be the runtime of this method be?Would this work correctly on an empty list?
Steps:Find the node you want to insert after.First, copy the link from the node that's already in the list.Then, change the link in the node that's already in the list
-
Inserting after a given value
-
Deleting a Node from a SLLIn order to delete a node from a SLL, you have to change the link in its predecessorThis is slightly tricky, because you cant follow a pointer backwardsDeleting the first node in a list is a special case, because the nodes predecessor is the list header
-
Deleting a Node from a SLLWhat would be the code?What would be the runtime of this method be?
-
Deleting a Node from a SLL To delete the first element, change the link in the header To delete some other element, change the link in its predecessor Deleted nodes will eventually be garbage collected
-
Doubly Linked ListHere is an example of a Doubly Linked List:
Each node contains a value, a link to its successor (if any), and a link to its predecessor (if any)abc
-
Traversing a DLLWhat would be the code for traversing a DLL?What would be the runtime of this method?
-
Inserting after a given valueWhat would be the code?What would be the runtime of this method be?Would this work correctly on an empty list?
-
Deleting a Node from a DLLWhat would be the code?What would be the runtime of this method be?
-
DLL compared to SLLAdvantages:Can be traversed in either direction (may be essential for some programs)Some operations, such as deletion and inserting before a node, become easier
Disadvantages:Requires more spaceList manipulations are slower (because more links must be changed)Greater chance of having bugs (because more links must be manipulated)
-
Circularly linked ListA DLL or SLL where instead of having a pointer to null on the last node, it points back to the head node.
-
TraversingWhat would be the code for traversing?What would be the runtime of this method?
-
Inserting after a given valueWhat would be the code?What would be the runtime of this method be?Would this work correctly on an empty list?
-
Deleting a NodeWhat would be the code?What would be the runtime of this method be?