Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Implementation of several common methods of LinkedList
/** * Implementation of several common methods of LinkedList * * @author Jayesh Chandrapal * @version 1.1 */ import java.util.*; import java.lang.*; interface MyList<E> { public void insertFront(E data); public void insertRear(E data); public void insertAtIndex(int index, E data); public void delete(E data); public void deleteAtIndex(int index); public void reverse(); public void merge(LinkedList<E> otherLinkedList); public void print(); public boolean isPalindrome(); public boolean hasCycle(); public boolean hasDuplicates(); public boolean empty(); public E itemFromLast(int n); public E middleElement(); public int size(); } class List<E> implements MyList<E> { private class Node<E> { E data; Node<E> next; public Node(E data, Node<E> next) { this.data = data; this.next = next; } } int size; Node<E> head; public List() { size = 0; head = null; } public void insertFront(E data) { Node<E> newnode = new Node<E>(data, null); if(!empty()) { newnode.next = head; } head = newnode; size += 1; } public void insertRear(E data) { Node<E> newnode = new Node<E>(data, null); if(empty()) { head = newnode; } else { Node<E> current = head; while(current.next != null) { current = current.next; } current.next = newnode; } size += 1; } public void insertAtIndex(int index, E data) { if(index < 0 || index > size) { throw new IllegalArgumentException(); } Node<E> newnode = new Node<E>(data, null); if(empty()) { head = newnode; } else { if(index == 0) { newnode.next = head; head = newnode; } else { Node<E> current = head; int i = 0; while(current != null) { if(i == index - 1) { newnode.next = current.next; current.next = newnode; break; } current = current.next; i += 1; } } } size += 1; } public void delete(E data) {} public void deleteAtIndex(int index) {} public void reverse() {} public void merge(LinkedList<E> otherLinkedList) {} public boolean isPalindrome() { return false; } public boolean hasCycle() { return false; } public boolean hasDuplicates() { return false; } public boolean empty() { return size == 0 && head == null; } public E itemFromLast(int n) { if(empty() || n <= 0) { throw new IllegalArgumentException(); } Node<E> first = head; Node<E> second = head; for(int i = 0; i < n - 1; i++) { first = first.next; } if(first == null) { return null; } while(first.next != null && second.next != null) { first = first.next; second = second.next; } if(second != null) { return second.data; } else { return null; } } public E middleElement() { return null; } public int size() { return size; } public void print() { Node<E> current = head; while(current != null) { System.out.print("[ " + current.data + " ] "); current = current.next; } System.out.println(); } } class Rextester { public static void main(String args[]) { MyList<Integer> items = new List<Integer>(); items.insertFront(10); items.insertFront(20); items.insertFront(30); items.insertRear(5); items.insertRear(2); items.insertAtIndex(2, 3); System.out.println("Size = " + items.size()); items.print(); System.out.println(items.itemFromLast(1)); System.out.println("OK"); } }
run
|
edit
|
history
|
help
0
test1
Java
MinMaxArray
Integer to English Words
Display Page (Pagination)
Sort
Counter.java
2A
Java tester
single_digit