deleting odd values in linked list java - java

I'm trying to delete odd values in a linked list. My function is called removeOdds. What am I messing up? I am calling cur.next within that if statement. Shouldn't that do the trick?
Here is my code:
public class SLinkedList {
public void editAtIndex(int index, int newElement) {
if (index < 0 || index >= size) {
return;
} else {
Node cur = head;
while (index > 0) {
cur = cur.next;
index--;
}
cur.setElement(newElement);
}
}
public static void main(String[] args) {
Node test = new Node(5);
test.setElement(5);
SLinkedList myList = new SLinkedList();
//System.out.println(myList.contains(5));
myList.addFirst(5);
myList.addFirst(7);
myList.addFirst(9);
myList.addLast(27);
myList.addLast(3);
myList.addLast(453);
myList.addLast(32);
myList.addLast(83);
myList.addLast(43);
myList.addLast(10);
myList.removeOdds();
myList.printList();
//myList.printList();
}
private Node head, tail, nextNode;
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
private int size;
public SLinkedList() {
}
public int size() {
int value = 0;
Node temp = head;
while (temp != null) {
temp = temp.next;
value++;
}
return value;
}
public void addFirst(int element) {
head = new Node(element, head);
size++;
if (size == 1) {
tail = head;
}
}
public void addLast(int element) {
Node last = new Node(element);
if (size == 0) {
head = last;
tail = last;
} else {
tail.setNext(last);
tail = last;
}
size++;
}
public void removeFirst() {
if (size == 0) {
return;
}
head = head.getNext();
size--;
if (size == 0) {
tail = null;
}
}
public void removeLast() {
if (size <= 1) {
head = null;
tail = null;
size = 0;
} else {
Node cur = head;
while (cur.next != tail) {
cur = cur.next;
}
tail = cur;
size--;
tail.next = null;
}
}
public void removeOdds() {
if (size <= 1) {
return;
} else {
Node cur = head;
while (cur.next != tail) {
cur = cur.next;
if (cur.getElement() % 2 != 0) {
size--;
cur = cur.next;
}
}
}
}
public boolean contains(int key) {
Node cur = head;
while (cur != null && cur.getElement() != key) {
cur = cur.next;
}
if (cur == null) {
return false;
}
return true;
}
public int indexOf(int key) {
Node cur = head;
int index = 0;
while (cur != null) {
if (cur.getElement() == key) {
return index;
}
cur = cur.next;
index++;
}
return -1;
}
public void printList() {
System.out.println("A list of size " + size);
Node temp = head;
while (temp != null) {
System.out.print(temp.getElement() + " ");
temp = temp.next;
}
System.out.println();
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public Node getTail() {
return tail;
}
public void setTail(Node tail) {
this.tail = tail;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
private static class Node {
private int element;
private Node next;
public Node(int element) {
this.element = element;
}
public Node(int element, Node next) {
this.element = element;
this.next = next;
}
public Node() {
element = 0;
}
public int getElement() {
return element;
}
public void setElement(int element) {
this.element = element;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}

cur = cur.next; simply replaces the cur variable with the next node. That doesn't mean the earlier variable is deleted. The reference is still held.
You need to hold a reference to the node before the node you wish to delete.
public void removeOdds() {
if (size <= 1) {
return;
} else {
Node cur = head;
Node previous = cur;
while (cur.next != tail) {
cur = cur.next;
if (cur.getElement() % 2 != 0) {
size--;
previous.next = cur.next;
} else {
previous = previous.next;
}
}
}
}
This will ensure the nodes get deleted. I haven't checked the complete code, but it looks like right now you're not handling the head & tail nodes. They'll be skipped. You might want to handle those nodes too in the method.

Related

Remove all occurences of an element in a Linked List using recursion

I'm making the implementation of the Linked-List data structure and I'm looking foward to implement a remove method of all the occurrences of an element using recursion, here's a piece of my code:
public class MyLinkedList<T> {
private Node<T> head;
private Node<T> last;
private int length;
public void remove(T elem) {
if (!(this.isContained(elem)) || this.isEmpty())
return;
else {
if (head.value.equals(elem)) {
head = head.next;
length--;
remove(elem);
} else {
// This is a constructor which requieres a head and last, and a length
new MyLinkedList<>(head.next, last, length-1).remove(elem);
}
}
}
}
I do understand the problem, I'm working with copies of a list not with the original, so, how could I merge or make this sub to the original list?
If I had to do it with recursion, I think it would look something like this:
public void remove(T elem)
{
removeHelper(null, this.head, elem);
}
private void removeHelper(Node<T> prev, Node<T> head, T elem)
{
if (head != null) {
if (head.value.equals(elem)) {
if (head == this.head) {
this.head = head.next;
} else {
prev.next = head.next;
}
if (this.last == head) {
this.last = prev;
}
--this.length;
} else {
prev = head;
}
removeHelper(prev, head.next, elem);
}
}
For the record, if I didn't have to use recursion, I'd do it linearly like this:
private void remove(T elem)
{
Node<T> prev = null;
Node<T> curr = this.head;
while (curr != null) {
if (curr.value.equals(elem)) {
if (this.last == curr) {
this.last = prev;
}
if (prev == null) {
this.head = curr.next;
} else {
prev.next = curr.next;
}
--this.length;
} else {
prev = curr;
}
curr = curr.next;
}
}
I would suggest doing this in a separate static function instead of doing it in the actual node class since you are recursing through the entire linked list.
public void removeAllOccurences(Node<T> head, Node<T> prev, T elem) {
if (head == null) {
return;
}
if (head.value.equals(elem)) {
Node<T> temp = head.next;
if (prev != null) {
prev.next = temp;
}
head.next = null; // The GC will do the rest.
removeAllOccurences(temp, prev, elem);
} else {
removeAllOccurences(head.next, head, elem);
}
}

Finding the second-to last node in a singly linked list

So I have an implementation of the Singly Linked List and I am trying to add a method which reports the second to last node of the list. However, I was not sure if I am allowed to write the method under the Node class then access it from the Singly Linked List class. If I do this, my instance variable of the node class('head' is used as a variable to access the penultimate method but also as the input of the penultimate method. Is that okay? Below is my implementation/attempt.
public class SinglyLinkedList {
private static class Node<Integer>{
private Integer element;
private Node<Integer> next;
private Node<Integer> penultimate;
public Node(Integer e, Node<Integer> n) {
element = e;
next = n;
penultimate = null;
}
public Integer getElement() {return element;}
public Node<Integer> getNext(){return next;}
public void setNext(Node<Integer> n) {next = n;}
public Node<Integer> penultimate(Node<Integer> head) {
Node<Integer> current = head;
while(current != null) {
if(head.getNext() == null) {
penultimate = head;
}
else {
current = current.getNext();
}
}
return penultimate;
}
}
private Node<Integer> head = null;
private Node<Integer> tail = null;
private int size = 0;
public SinglyLinkedList() {}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public Integer first() {
if (isEmpty()) {
return null;
}
return head.getElement();
}
public Integer last() {
if(isEmpty()) {
return null;
}
return tail.getElement();
}
public void addFirst(Integer i) {
head = new Node<> (i, head);
if(size == 0) {
tail = head;
}
size++;
}
public void addLast(Integer i) {
Node<Integer> newest = new Node<>(i,null);
if(isEmpty()) {
head = newest;
}
else {
tail.setNext(newest);
tail = newest;
size++;
}
}
public Integer removeFirst() {
if(isEmpty()) {
return null;
}
Integer answer = head.getElement();
head = head.getNext();
size--;
if(size == 0) {
tail = null;
}
return answer;
}
public void getPenultimate() {
if(isEmpty()) {
System.out.println("List is empty. Please check.");
}
else {
System.out.println("The second last node is: " + head.penultimate(head));
}
}
Remove the field penultimate. You do not want it in every node, in fact in no node, but calculated.
In the Node's penultimate method head should not be used in the loop.
//private Node<Integer> penultimate;
// head: ...#->#->#->P->null
public Node<Integer> penultimate(Node<Integer> head) {
Node<Integer> penultimate = null;
Node<Integer> current = head;
while (current != null) {
if (current.getNext() == null) {
penultimate = current;
break;
}
current = current.getNext();
}
return penultimate;
}
Or the third (second?) to last node:
// head: ...#->#->#->P->#->null
public Node<Integer> penultimate(Node<Integer> head) {
Node<Integer> penultimate = null;
Node<Integer> current = head;
while (current != null) {
if (current.getNext() == null) {
break;
}
penultimate = current;
current = current.getNext();
}
return penultimate;
}
Why not keep track of the second to last node?
private Node<Integer> head = null;
private Node<Integer> tail = null;
private Node<Integer> secondToLast = null;
private int size = 0;
public SinglyLinkedList() {}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public Integer first() {
if (isEmpty()) {
return null;
}
return head.getElement();
}
public Integer last() {
if(isEmpty()) {
return null;
}
return tail.getElement();
}
public void addFirst(Integer i) {
if (size == 1) {
secondToLast = head;
}
head = new Node<> (i, head);
if(size == 0) {
tail = head;
}
size++;
}
public void addLast(Integer i) {
Node<Integer> newest = new Node<>(i,null);
if(isEmpty()) {
head = newest;
}
else {
tail.setNext(newest);
secondToLast = tail;
}
tail = newest;
size++;
}
public Integer removeFirst() {
if(isEmpty()) {
return null;
}
Integer answer = head.getElement();
head = head.getNext();
size--;
if(size == 0) {
tail = null;
}
if (size == 1) {
secondToLast = null;
}
return answer;
}
public void getPenultimate() {
if(isEmpty()) {
System.out.println("List is empty. Please check.");
}
else {
System.out.println("The second last node is: " + secondToLast);
}
}

Remove Method for a Single and Double Linked List (Java)

I am struggling to understand how to implement a remove(); for both a double and single linked class. I have figured out how to remove the first node in the double, but not in the single. First I would like to debug, problem solve the single linked class, then work on the double after that.
Here is the code I have so far for the Single Linked Class.
public class SingleLinkedClass<T> {
private Node <T> head;
private Node <T> tail;
private int size;
public SingleLinkedClass() {
size = 0;
head = null;
tail = null;
}
public void insertAtHead(T v)
{
//Allocate new node
Node newNode = new Node(v, head);
//Change head to point to new node
head = newNode;
if(tail == null)
{
tail = head;
}
//Increase size
size++;
}
public void insertAtTail(T v)
{
if(tail == null)
{
tail = new Node(v, null);
head = tail;
size++;
return;
}
Node newNode = new Node(v, null);
tail.nextNode = newNode;
tail = newNode;
size++;
}
public T removeHead()
{
if(head == null)
{
throw new IllegalStateException("list is empty! cannot delete");
}
T value = head.value;
head = head.nextNode;
size--;
return value;
}
public void removeTail()
{
//Case 1: list empty
if(head == null)
{
return;
}
//Case 2: list has one node
else if(head == tail)
{
head = tail = null;
}
else
{
Node temp = head;
while(temp.nextNode != tail)
{
temp = temp.nextNode;
}
tail = temp;
tail.nextNode = null;
}
size--;
}
public boolean remove(T v) {
Node<T> previous = head;
Node<T> cursor = head.nextNode;
if (head.nextNode == null) {
return false;
}
while(cursor != tail){
if (cursor.value.equals(v)) {
previous = cursor.nextNode;
return true;
}
previous = cursor;
cursor = cursor.nextNode;
}
return false;
}
public String toString() {
if (head == null) {
return "The list is Empty!";
}
String result = "";
Node temp = head;
while (temp != null) {
result += temp.toString() + " ";
temp = temp.nextNode;
}
return result;
}
public int size() {
return size;
}
private class Node <T> {
private T value;
private Node <T> nextNode;
public Node(T v, Node<T> n) {
value = v;
nextNode = n;
}
public String toString() {
return "" + value;
}
}
}
Here is my Double Linked Class
public class DoubelyLinkedList<E> {
private int size;
private Node<E> header;
private Node<E> trailer;
public DoubelyLinkedList() {
size = 0;
header = new Node<E>(null, null, null);
trailer = new Node<E>(null, null, header);
header.next = trailer;
}
public boolean remove(E v) {
//If the list is empty return false
if(header.next == trailer){
return false;
}
//If v is the head of the list remove and return true
Node <E> cursor = header.next;
for (int i = 0; i < size; i++) {
//Remove at Head
if(cursor.value.equals(v)){
removeAtHead();
}
cursor = cursor.next;
}
return true;
}
/*
} */
public void insertAtHead(E v) {
insertBetween(v, header, header.next);
}
public void insertAtTail(E v) {
insertBetween(v, trailer.prev, trailer);
}
private void insertBetween(E v, Node<E> first, Node<E> second) {
Node<E> newNode = new Node<>(v, second, first);
first.next = newNode;
second.prev = newNode;
size++;
}
public E removeAtHead() {
return removeBetween(header, header.next.next);
}
public E removeAtTail() {
return removeBetween(trailer.prev.prev, trailer);
}
private E removeBetween(Node<E> first, Node<E> second) {
if (header.next == trailer)// if the list is empty
{
throw new IllegalStateException("The list is empty!");
}
E result = first.next.value;
first.next = second;
second.prev = first;
size--;
return result;
}
public String toStringBackward() {
if (size == 0) {
return "The list is empty!";
}
String r = "";
Node<E> temp = trailer.prev;
while (temp != header) {
r += temp.toString() + " ";
temp = temp.prev;
}
return r;
}
public String toString() {
if (size == 0) {
return "The list is empty!";
}
String r = "";
Node<E> temp = header.next;
while (temp != trailer) {
r += temp + " ";
temp = temp.next;
}
return r;
}
private static class Node<T> {
private T value;
private Node<T> next;
private Node<T> prev;
public Node(T v, Node<T> n, Node<T> p) {
value = v;
next = n;
prev = p;
}
public String toString() {
return value.toString();
}
}
}
Here is my Driver
public class Driver {
public static void main(String[] args) {
DoubelyLinkedList<String> doubley = new DoubelyLinkedList();
SingleLinkedClass<String> single = new SingleLinkedClass();
single.insertAtHead("Bob");
single.insertAtHead("Sam");
single.insertAtHead("Terry");
single.insertAtHead("Don");
System.out.println(single);
single.remove("Bob");
System.out.println("Single Remove Head: " + single);
/*
single.remove("Don");
System.out.println("Single Remove Tail: " + single);
single.remove("Terry");
System.out.println("Single Remove Inbetween: " + single);
*/
System.out.println();
System.out.println();
doubley.insertAtHead("Bob");
doubley.insertAtHead("Sam");
doubley.insertAtHead("Terry");
doubley.insertAtHead("Don");
System.out.println(doubley);
doubley.remove("Bob");
System.out.println("Double Remove Head: " + doubley);
doubley.remove("Don");
System.out.println("Double Remove Tail: " + doubley);
/*
doubley.remove("Sam");
System.out.println("Double Remove Inbetween: " + doubley);
*/
}
}
In the removeHead moving head to its next, it might become null. Then tail was the head too. Then tail should be set to null too.
DoublyLinkedList is better English than DoubelyLinkedList.
As this is homework, I leave it by this.

InsertItem at Index 0 - DoublyLinkedList

I am currently working on creating a DoublyLinkedList which uses tail recursion.
I have managed to get all my methods fully working except my insert item.
It works for inserting at any Index other than 0. I have tried to write an if statement which deals with this and my code still runs however although it increments the noOfItems. It never adds the item to my list.
Could the issue be with my toString? Or am I missing something in my if case?
This is my DLLNode class:
public class DLLNode
{
private DLLNode previous;
public DLLNode next;
private String value;
public DLLNode(String value)
{
this.value = value;
this.previous = previous;
this.next = next;
}
public DLLNode(String value, DLLNode next, DLLNode previous)
{
this.value = value;
this.next = next;
this.previous = previous;
}
public String GetDataItem()
{
return value;
}
public void setDataItem()
{
this.value = value;
}
public DLLNode GetPreviousNode()
{
return previous;
}
public void setPrevious(DLLNode previous)
{
this.previous = previous;
}
public DLLNode GetNextNode()
{
return next;
}
public void setNextNode(DLLNode next)
{
this.next = next;
}
public void addItem(String value) {
if(this.next == null) {
DLLNode newNode = new DLLNode(value);
this.next = newNode;
} else {
this.next.addItem(value);
}
}
public void InsertItemHelper(String value, int indexToInsert, int current, DLLNode currNode)
{
if (indexToInsert == 0)
{
DLLNode newNode = new DLLNode(value);
currNode.GetNextNode().setPrevious(newNode);
}
else if (current == indexToInsert-1)
{
DLLNode newNode = new DLLNode(value);
newNode.setNextNode(currNode.GetNextNode());
currNode.setNextNode(newNode);
currNode.GetNextNode().setPrevious(newNode);
newNode.setPrevious(currNode);
}
else
{
InsertItemHelper(value, indexToInsert, current+1, currNode.GetNextNode());
}
}
public void DeleteItemHelper(int indexToDelete, int current, DLLNode currNode)
{
if (current == indexToDelete-1)
{
currNode.setNextNode(currNode.GetNextNode().GetNextNode());
}
else
{
DeleteItemHelper(indexToDelete, current+1, currNode.GetNextNode());
}
}
}
And this is my DoublyLinkedList class:
public class DoublyLinkedList
{
private int noOfItems;
private DLLNode head;
private DLLNode tail;
// Default constructor
public DoublyLinkedList()
{
head = null;
tail = null;
this.noOfItems = 0;
}
public int GetNoOfItems()
{
return noOfItems;
}
public String GetItemByIndex(int index)
{
int count = 0;
while (count < index)
{
head = head.GetNextNode();
count++;
}
return head.GetDataItem();
}
public DLLNode GetNodeByIndex(int index)
{
int count = 0;
while (count < index)
{
head = head.GetNextNode();
count++;
}
return head;
}
public void AddItem(String value)
{
if (head == null)
{
DLLNode newNode = new DLLNode(value);
head = newNode;
noOfItems++;
}
else
{
head.addItem(value);
noOfItems++;
}
}
public void InsertItem(int index, String value)
{
if (index > noOfItems)
{
AddItem(value);
}
else {
head.InsertItemHelper(value, index, 0, head);
noOfItems++;
}
}
public void DeleteItem(int index)
{
if (index ==0)
{
System.out.println("Out of Bounds");
}
if (index > noOfItems)
{
System.out.println("Out of Bounds");
}
if (head == null)
{
System.out.println("No Item to remove");
}
else if (index == 1)
{
head = head.GetNextNode();
noOfItems--;
}
else
{
head.DeleteItemHelper(index, 0, head);
noOfItems--;
}
}
public int getNoOfItems()
{
return this.noOfItems;
}
public boolean isEmpty()
{
return (head == null);
}
public String toString()
{
DLLNode currentNode = head;
StringBuilder sb = new StringBuilder();
while (currentNode != null) {
sb.append(currentNode.GetDataItem());
if (currentNode.GetNextNode() != null)
{
sb.append(",");
}
currentNode = currentNode.GetNextNode();
}
return sb.toString();
}
}
I have added the following code into my insert item:
if (index ==0)
{
DLLNode newNode = new DLLNode(value);
head.setNextNode(head);
// newNode.next= head.GetNextNode();
head = newNode;
noOfItems++;
}
If I include the commented out line I get an error to do with string builder.
With the line commented out it adds in the linked list at position 0, but doesn't add any of the rest. It does however increment noOfItems correctly.
The following error is what appears:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2367)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:114)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:415)
at java.lang.StringBuilder.append(StringBuilder.java:132)
at ads2.DoublyLinkedList.toString(DoublyLinkedList.java:155)
at java.lang.String.valueOf(String.java:2847)
at java.lang.StringBuilder.append(StringBuilder.java:128)
at ads2.Main.printList(Main.java:62)
at ads2.Main.main(Main.java:38)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
If you need any more details on the error please let me know.
You need to update the head when adding at position 0
Because you don't update the head, the old head is still linked in the list object, and it's next() returns what should be in the new list object index 1, so you end up with the same list
You could confirm this by confirming
list.getNodeByIndex(0) != list.getNodeByIndex(1).previousNode();

Removing Item at index from a linked list?

public class LinkedList
{
private Node head;
private Node tail;
private int numberOfElements;
public LinkedList()
{
head = null;
tail = null;
this.numberOfElements = 0;
}
public int length()
{
return this.numberOfElements;
}
public void removeFront()
{
Node currNode = head;
head = head.getNextNode();
currNode.setNextNode(null);
this.numberOfElements--;
}
public void removeLast()
{
this.tail = this.tail.prev;
}
public void removeAtIndex(int index) throws AmishException
{
if (index == 0) {
Node q = head;
head = q.getNextNode();
}
else if ((index > numberOfElements - 1) || (index < 0))
System.out.println("Index out bounds.");
else {
Node currNode = head;
for (int i = 0; i < index; i++) {
currNode = currNode.getNextNode();
}
Node temp = currNode;
currNode = temp.getPrevNode();
currNode.setNextNode(temp.getNextNode());
temp = null;
numberOfElements--;
}
}
Node -
public class Node
{
private int payload;
private Node nextNode;
public Node prev;
public Node previous;
public Node next;
public Node(int payload)
{
this.payload = payload;
nextNode = null;
}
public Node getNextNode()
{
return this.nextNode;
}
public Node getPrevNode()
{
return this.getPrevNode();
}
public void setNextNode(Node n)
{
this.nextNode = n;
}
public void display()
{
System.out.print("(" + super.toString() + " : " + this.payload + ")");
if(this.nextNode != null)
{
System.out.print(" -> ");
this.nextNode.display();
}
else
{
System.out.println("");
}
}
public String toString()
{
return "" + super.toString() + " : " + this.payload;
}
public void setPrevNode(Node n)
{
previous = n;
}
}
Driver -
public class Driver
{
public static void main(String[] args) throws Exception
{
LinkedList ll = new LinkedList();
ll.addFront(7);
ll.addFront(5);
ll.addEnd(13);
ll.addEnd(27);
ll.addFront(2);
ll.addAtIndex(1, 0);
ll.addAtIndex(12, 6);
ll.addAtIndex(9, 2);
ll.addAtIndex(11, 2);
//ll.removeFront();
//ll.removeLast();
ll.removeAtIndex(1);
for(int i = 0; i < ll.length(); i++)
{
System.out.println(ll.get(i));
}
}
}
I got what I need to remove the head's index, but how to do anything else is beyond me.
I have the methods to remove the head and the tail now I just need to know how to remove at an index just Like if I were to add at an index.
Okay, completely editing this answer. I took your code and went from that. There were some issues with your Node class, check out mine. I created a barebones Node class, add the any other functionality you may need/want. For the LinkedList class, I created a simple add method that adds to the end of the list. Use this as a prototype for your other adds. Everything right now is working and I put some println's to help you see where the program is at. I highly recommend using a debugger. Also note that this is a doubly linked list.
Linked List
public class LinkedList {
private Node head;
private Node tail;
private int numberOfElements;
public LinkedList()
{
head = null;
tail = null;
numberOfElements = 0;
}
public int length()
{
return numberOfElements;
}
public void removeAtIndex(int index)
{
if (index == 0) {
Node q = head;
head = q.getNextNode();
numberOfElements--;
}
else if ((index > numberOfElements - 1) || (index < 0))
System.out.println("Index out of bounds.");
else {
Node currNode = head;
for (int i = 0; i < index; i++) {
//Node p = currNode;
System.out.println("At this payload " + currNode.getPayload());
currNode = currNode.getNextNode();
System.out.println("Now at this payload " + currNode.getPayload());
}
Node temp = currNode;
System.out.println("Removing the node with payload " + temp.getPayload());
currNode = temp.getPrevNode();
currNode.setNextNode(temp.getNextNode());
temp = null;
numberOfElements--;
}
}
public void add(int num) {
Node newNode = new Node(num);
newNode.setNextNode(null);
if (numberOfElements == 0) {
newNode.setPrevNode(null);
head = newNode;
tail = newNode;
}
else if (numberOfElements == 1) {
head.setNextNode(newNode);
tail = newNode;
tail.setPrevNode(head);
}
else {
newNode.setPrevNode(tail);
tail.setNextNode(newNode);
tail = newNode;
}
System.out.println("Inserted " + num + " into the linked list");
numberOfElements++;
}
public void printList() {
Node temp = head;
while (temp != null) {
System.out.println("Node with payload " + temp.getPayload());
temp = temp.getNextNode();
}
}
}
Node
public class Node {
private int payload;
private Node next;
private Node prev;
public Node(int payload) {
this.payload = payload;
prev = null;
next = null;
}
public Node getNextNode() {
return next;
}
public Node getPrevNode() {
return prev;
}
public void setNextNode(Node n) {
next = n;
}
public void setPrevNode(Node n) {
prev = n;
}
public int getPayload() {
return payload;
}
}
Driver
public class Driver {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.add(7);
ll.add(5);
ll.add(13);
ll.add(27);
ll.add(2);
ll.printList();
ll.removeAtIndex(3);
ll.printList();
}
}

Categories