JAVA & Linked List Implementation
Group VIPresented byRegulapati Venkata Ramana Rao
Objective
Purpose Types of linked list Organization Different operations Bio-Oh of each operation Case study
Introduction
Is the linear data structure represented by nodes Is an alternative to the contiguous array
implementation It Consists of collection of connected , dynamically
allocated Nodes Each node will have at least two elements
Element itself The link to the next node in the list
11 12
Link to next node
Element
Introduction
Linked List Node Representation
Class ListNode {
Object element;ListNode nextNode;
}
Why Linked List ? Inserting or deleting of an item into/from list is easy, where
as it requires extensive data movement if we use arrays Insertion Into Sorted Array
Insert 4 into above array Increase the array size Locate the position of 4. Big-oh is O(N) Move the elements by one position. Big-oh is O(N) So Big-Oh is O(N2)
Introduction
1 2 3 5 6 7
Introduction
Array After Inserting
Linked List Implementation
Inserting Element 4 Create new node with element 4
ListNode tmp = new ListNode();tmp.element = 4;
Search the position of element Big-Oh(N)
1 2 3 5 6 74
1 2 5 6
4
tmp
header
Introduction
Insertion… Point the new node link to the node after the new node
temp.next = current.next; Point the previous node link to new node
current.next = tmp; Big-Oh is O(N)
Note: So Linked list is efficient in this case
1 2 5 6
4tmp
current
Linked Lists are 3 typesSingle Linked List
Consists of data element and reference to the next Node in the Linked list
First node is accessed by reference (Header Node) Last node is set to NULL
Types
11 12 13
Head
Types
Double Linked List Consists of nodes with two link members one points to the
previous and other to the next node Maximizes the needs of list traversals Compared to the Singled list inserting and deleting nodes is a
bit slower as both the links has to be updated It requires the extra storage space for the second link
11 11 11
Head
Types
Circular Linked list Similar to the doubly linked list, but the last node link points
to the first node instead of null Useful in algorithms where there is no particular first or last
item Advantage is we can make head to point any node without
destroying the list Operations are similar to single linked list except identifying
the last node in the list. To find the last node compare the last node to head node (i.e you have found the last node if its link points to the same node that the head points)
Types
Circularly Linked List Representation
11 12 13
first
Header & Trailer Nodes
Header Node: A placeholder node at the beginning of list, used to simplify list processing.It doesn’t hold any data but satisfies that the every node has a previous node.
Trailer Node: A placeholder node at the end of list, used to simplify list processing
Single LinkedList Operations
Insertion of node Consider 3 cases of insertion Node is first node in the linked list Node is an inner node Node is last node
Deleting specific node Printing/Traversing Liked List Find specific node Find Length of Linked List
Implementation
All the above mentioned operations are implemented on a sorted singly linked list in the following example
The implementation has these classes List.java … This is the interface LinkedList.java … Implementation for the
methods declared in interface
Node.java … This is actual Node in Linked list and also has Accessor / Mutator methods
TestLinkedList.java … Test client program
List.java Interface
Node.java
Node.java
LinkedList.java
//This class Implements the List interface
LinkedList.java
LinkedList.java
LinkedList.java
LinkedList.java
LinkedList.java
LinkedList.java
InvalidElementException.java
ClientLinkedList.java
ClientLinkedList.java
ClientLinkedList.java