java & linked list implementation group vi presented by regulapati venkata ramana rao

28
JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

Upload: camryn-heyne

Post on 30-Mar-2015

246 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

JAVA & Linked List Implementation

Group VIPresented byRegulapati Venkata Ramana Rao

Page 2: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

Objective

Purpose Types of linked list Organization Different operations Bio-Oh of each operation Case study

Page 3: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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

Page 4: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

Introduction

Linked List Node Representation

Class ListNode {

Object element;ListNode nextNode;

}

Page 5: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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

Page 6: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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

Page 7: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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

Page 8: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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

Page 9: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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

Page 10: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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)

Page 11: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

Types

Circularly Linked List Representation

11 12 13

first

Page 12: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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

Page 13: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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

Page 14: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

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

Page 15: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

List.java Interface

Page 16: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

Node.java

Page 17: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

Node.java

Page 18: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

LinkedList.java

//This class Implements the List interface

Page 19: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

LinkedList.java

Page 20: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

LinkedList.java

Page 21: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

LinkedList.java

Page 22: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

LinkedList.java

Page 23: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

LinkedList.java

Page 24: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

LinkedList.java

Page 25: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

InvalidElementException.java

Page 26: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

ClientLinkedList.java

Page 27: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

ClientLinkedList.java

Page 28: JAVA & Linked List Implementation Group VI Presented by Regulapati Venkata Ramana Rao

ClientLinkedList.java