# M180: Data Structures & Algorithms in Java

## Presentation on theme: "M180: Data Structures & Algorithms in Java"— Presentation transcript:

M180: Data Structures & Algorithms in Java
Linked Lists – Part 1 Arab Open University

Limitation of Arrays An array has a limited number of elements
routines inserting a new value have to check that there is room Can partially solve this problem by reallocating the array as needed (how much memory to add?) adding one element at a time could be costly one approach - double the current size of the array A better approach: use a Linked List

Dynamically Allocating Elements
Allocate elements one at a time as needed, have each element keep track of the next element Result is referred to as linked list of elements, track next element with a pointer

A linked list consists of: A sequence of nodes myList a b c d Each node contains a value and a link (reference) to some other node The last node contains a null link The list may have a header

More terminology A node’s successor is the next node in the sequence
The last node has no successor A node’s predecessor is the previous node in the sequence The first node has no predecessor A list’s length is the number of elements in it A list may be empty (contain no elements)

Linked Lists Stores a collection of items non-contiguously.
Each item in the list is stored with an indication of where the next item is. Must know where first item is. The list will be a chain of objects, called nodes, of type ListNode that contain the data and a reference to the next ListNode in the list. Allows addition or deletion of items in the middle of collection with only a constant amount of data movement. Contrast this with array. con•tig•u•ous adj. 1 in physical contact; touching along all or most of one side 2 near, next, or adjacent SYN. adjacent ListNode ListNode ListNode ListNode first A0 A1 A2 A3

ListNode: Definition public class ListNode <DataType> {
DataType data; ListNode<DataType> next; // constructors ListNode(DataType d, ListNode<DataType> n) data = d; next = n; } ListNode(DataType d) { this (d, null); } ListNode() { this (null); }

a b c d current Insert X immediately after current position a b c d x current

Implementing Insertion: Step By Step
Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); a b current tmp

Implementing Insertion: Step By Step
Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; a b current tmp

Implementing Insertion: Step By Step
Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; a b x current tmp

Implementing Insertion: Step By Step
Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; // x’s next node is b tmp.next = current.next; // a’s next node is x current.next = tmp; a b x current tmp

Implementing Insertion: Shorter Version
A shorter version: // create a new node tmp = new ListNode<DataType>(x,current.next); // a’s next node is x current.next = tmp; a b x current tmp

Implementing Insertion: Shorter Version
A shorter version: // create a new node tmp = new ListNode<DataType>(x,current.next); // a’s next node is x current.next = tmp; a b x current tmp

Implementing Append Insert X immediately at the end of the list
// last refers to the last node in the linked list last.next = new ListNode<DataType>(); last = last.next; // adjust last last.data = x; // place x in the node last.next = null; // adjust next Most efficient approach last = last.next = new ListNode (x, null); a b c d last a b c d X last

Implementing Basic Deletion
Delete an item immediately after current position Basic deletion is a bypass in the linked list. a x b current a b current

Implementing Basic Deletion
Need a reference to node prior to the one to be deleted. current.next = current.next.next; a x b current a x b current a b current