implementing circular linked list with java generics - java

I am learning java and i am still a beginner.i have written this code to implement a circular linked list and it keeps printing the numbers when i try to print the list. it looks like some kind of an infinite loop maybe. I even tried to use a debug but it didn't do much for me. I would very much appreciate it if you could review the code and see why this is happening. here is the code below. I would be also for giving me feedback on the code :)
this is the class for the circular linked list
public class CircularLinkedList<E> implements API<E> {
private Node<E> head;
private int size = 0;
public void placeAtBeginning(E element) {
Node<E> newNode = new Node<E>(element);
if(head == null) {
head = newNode;
head.setNext(head);
}else {
Node<E> temp = head;
head = newNode;
newNode.setNext(temp);
}
size++;
}
public void placeAtEnd(E element) {
Node<E> newNode = new Node<E>(element);
if (head == null) {
head = newNode;
}else {
Node<E> temp = head;
while (temp.getNext() != head) {
temp = temp.getNext();
}
temp.setNext(newNode);
}
newNode.setNext(head);
size++;
}
public void deleteFromBeginning() {
Node<E> temp = head;
while (temp.getNext() != head) {
temp = temp.getNext();
}
temp.setNext(head.getNext());
head = head.getNext();
size --;
}
public void deleteFromEnd() {
Node<E> temp = head;
while(temp.getNext().getNext() != head) {
temp = temp.getNext();
}
temp.setNext(head);
size--;
}
public void print() {
Node<E> temp = head;
while(temp.getNext()!= head) {
System.out.print(temp.getValue() + " , ");
temp = temp.getNext();
}
System.out.print(temp.getValue());
}
}
this is the class for my node
public class Node<T> {
private Node<T> next;
private T item;
public Node(T item) {
this.item = item;
}
public void setNext(Node<T> next) {
this.next = next;
}
public Node<T> getNext() {
return this.next;
}
public T getValue() {
return this.item;
}
}
this is my main where i tried to test it using int.
public class Main {
public static void main(String [] args) {
API <Integer> list = new CircularLinkedList<Integer>();
int a = 10;
int b = 3;
int c = 15;
int d = 8;
int f = 9;
list.placeAtBeginning(a);
list.placeAtEnd(b);
list.print();
System.out.println();
list.placeAtBeginning(c);
list.placeAtBeginning(d);
list.print();
}
}
this is my API which I used
public interface API <E> {
public void placeAtBeginning(E element);
public void placeAtEnd(E element);
public void deleteFromBeginning();
public void deleteFromEnd();
public void print();
}

Your method placeAtBeginning() doesn't insert the new element in the circular list but simply lets the next of the new element refer to the original circular list.
Try this:
public void placeAtBeginning(E element)
{
Node<E> newNode = new Node<E>(element);
if(head == null)
{
head = newNode;
head.setNext(head);
}
else
{
Node<E> last = head;
while (last.getNext() != head)
last = last.getNext();
newNode.setNext(head);
head = newNode;
last.setNext(head);
}
size++;
}
I didn't check the other methods. They might contain a similar error.

Related

Can't access my linked list methods and can't iterate trough it

So im following along this playlist about data structures and in this video to conclude the linked list part, the professor explain we need an inner class called IteratorHelper.
Video:
https://www.youtube.com/watch?v=bx0ebSGUKto&list=PLpPXw4zFa0uKKhaSz87IowJnOTzh9tiBk&index=21
This is the code in my github with the linked list implementation and the main class called tester:
https://github.com/Ghevi/Algos-DataStructures/tree/master/src/com/ghevi/ads/linkedlists
The problem is that the tester class can't compile. If I instantiate the linked list as an ListIterator i can't access its methods. I also can't iterate trough it regardless of having the IteratorHelper inner class.
In the video he writes "implements ListI<>" is just a shorter version for ListIterator<>?
Sorry im just a beginner.
package com.ghevi.ads.linkedlists;
import java.util.ListIterator;
public class Tester {
public static void main(String[] args) {
ListIterator<Integer> list = new LinkedList<Integer>();
int n = 10;
for (int i = 0; i < n; i++)
list.addFirstWithTail(i);
int removedFirst = list.removeFirst();
int removedLast = list.removeLast();
for(int x : list){
System.out.println(x);
}
}
}
The video is not very clear, but basically LinkedList should implement Iterable, not ListIterator. IteratorHelper should implement ListIterator (see 4:20 timestamp).
Here's the fixed code:
package linkedlists;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
// Notes at Notes/Singly LinkedList.txt
public class LinkedList<E> implements Iterable<E> {
#Override
public Iterator<E> iterator() {
return new IteratorHelper();
}
class IteratorHelper implements ListIterator<E>{
Node<E> index;
public IteratorHelper(){
index = head;
}
// Return true if there is an element to return at the pointer
#Override
public boolean hasNext() {
return (index != null);
}
// Return the element where the pointer is and mover the pointer to the next element
#Override
public E next() {
if(!hasNext())
throw new NoSuchElementException();
E val = index.data;
index = index.next;
return val;
}
#Override
public boolean hasPrevious() {
return false;
}
#Override
public E previous() {
return null;
}
#Override
public int nextIndex() {
return 0;
}
#Override
public int previousIndex() {
return 0;
}
#Override
public void remove() {
}
#Override
public void set(E e) {
}
#Override
public void add(E e) {
}
/* For version older than java 1.8
public void remove(){
throw new UnsupportedOperationException();
}
public void forEachRemaining(){};
*/
} // inner class (can only be accessed by the outer class)
class Node<E> {
E data;
Node<E> next;
public Node(E obj){
data = obj;
next = null;
}
} // inner class (can only be accessed by the outer class)
private Node<E> head;
private Node<E> tail;
private int currentSize;
public LinkedList(){
head = null;
tail = null;
currentSize = 0;
}
public void addFirst(E obj){
Node<E> node = new Node<E>(obj);
// The order of these 2 lines is fundamental
node.next = head;
head = node;
currentSize++;
}
public void addFirstWithTail(E obj){
Node<E> node = new Node<E>(obj);
if(head == null){
head = tail = node;
return;
}
// The order of these 2 lines is fundamental
node.next = head;
head = node;
currentSize++;
}
// O(n)
public void slowAddLast(E obj){
Node<E> node = new Node<E>(obj);
if(head == null){
head = tail = node;
currentSize++;
return;
}
Node<E> tmp = head;
while(tmp.next != null){
tmp = tmp.next;
}
tmp.next = node;
currentSize++;
}
// O(1)
public void fasterAddLast(E obj){
Node<E> node = new Node<E>(obj);
if(head == null){
head = tail = node;
currentSize++;
return;
}
tail.next = node;
tail = node;
currentSize++;
}
public E removeFirst(){
if(head == null){
return null;
}
E tmp = head.data;
if(head == tail){
head = tail = null;
} else {
head = head.next;
}
currentSize--;
return tmp;
}
public E removeLast(){
if(head == null){
return null;
}
if(head == tail){
return removeFirst();
}
Node<E> current = head; // Can also write Node<E> current = head, previous = null;
Node<E> previous = null;
while(current != tail){
// The order is crucial
previous = current;
current = current.next;
}
previous.next = null;
tail = previous;
currentSize--;
return current.data;
}
public E findAndRemove(E obj){
Node<E> current = head, previous = null;
// In an empty list current = null so we skip to the last line
while(current != null){
if(((Comparable<E>)obj).compareTo(current.data) == 0){
// Beginning or single element
if(current == head)
return removeFirst();
// Ending of the list
if(current == tail)
return removeLast();
currentSize--;
// Removing the reference to the node to delete
previous.next = current.next;
return current.data;
}
previous = current;
current = current.next;
}
// Node not found
return null;
}
public boolean contains(E obj){
Node<E> current = head;
while(current != null) {
if(((Comparable<E>) obj).compareTo(current.data) == 0)
return true;
current = current.next;
}
return false;
}
public E peekFirst(){
if(head == null)
return null;
return head.data;
}
public E peekLast(){
if(tail == null)
return null;
return tail.data;
}
}
The interface methods hasPrevious, next, etc... have been moved into the IteratorHelper class which implements Iterator. The LinkedList class has an iterator() method because it implements Iterable. Now you can instantiate a LinkedList object and iterate over it in a for-loop:
package linkedlists;
public class Tester {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
int n = 10;
for (int i = 0; i < n; i++)
list.addFirstWithTail(i);
int removedFirst = list.removeFirst();
int removedLast = list.removeLast();
for(int x : list){
System.out.println(x);
}
}
}
Here's a handy chart to remind you which class should have which functions:
More on Iterable vs Iterator

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);
}
}

Not adding all of the elements into the linkedlist

Why isn't my insertLast(T data) method adding all of the elements into the list?
public class Tester {
public static void main(String[] args){
LinkedList<Integer> myList = new LinkedList<Integer>();
myList.insertLast(1);
myList.insertLast(2);
myList.insertLast(3);
myList.insertLast(4);
myList.insertLast(5);
myList.insertLast(6);
myList.displayList();
}
}
It adds only 6. What could be the problem with the code?
public class Node<T> {
public T data;
public Node<T> next;
public Node(T data, Node<T> n){
this.data = data;
this.next = n;
}
public void display(){
System.out.print(this.data + " ");
}
}
class LinkedList<T> implements Iterable<T>{
private Node<T> head;
private int size;
public LinkedList(){
this.head = new Node<T>(null, null);
this.size = 0;
}
public boolean isEmpty(){
return (head.next == null);
}
public void displayList(){
Node<T> current = head;
while(current != null){
current.display();
current = current.next;
}
}
public void insert(T data){
head = new Node<T>(data, null);
size++;
}
public void insertLast(T data){
Node<T> newNode = new Node<T>(data, null);
if(isEmpty()){
head = new Node<T>(data, null);
size++;
}
else{
Node<T> current = head;
while(current.next != null){
current = current.next;
}
current.next = newNode;
size++;
}
}
}
Every time you call insertLast, isEmpty returns true, because head.next is null. head.next is only ever set to non-null if isEmpty returns false.
This check:
if(isEmpty()){
head = new Node<T>(data, null);
size++;
}
initializes head to have a null next, so isEmpty() returns true everytime it's called. You need to check head itself to be null or not in isEmpty(), and in your constructor, instead of:
this.head = new Node<T>(null, null);
you should initialize it to:
this.head = null;
Hai Minor change in your program .You don't have to initialize head.next as null
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
public class Node<T> {
public T data;
public Node<T> next;
public Node(T data, Node<T> n){
this.data = data;
this.next = n;
}
public void display(){
System.out.print(this.data + " ");
}
}
class LinkedList<T> implements Iterable<T>{
private Node<T> head;
private int size;
public LinkedList(){
// this.head = new Node<T>(null, null);
this.size = 0;
}
public boolean isEmpty(){
return (head == null);
}
public void displayList(){
Node<T> current = head;
while(current != null){
current.display();
current = current.next;
}
}
public void insert(T data){
head = new Node<T>(data, null);
size++;
}
public void insertLast(T data){
Node<T> newNode = new Node<T>(data, null);
if(isEmpty()){
head = new Node<T>(data, null);
size++;
}
else{
Node<T> current = head;
while(current.next != null){
current = current.next;
}
current.next = newNode;
size++;
}
}
public void forEach(Consumer<? super T> arg0) {
// TODO Auto-generated method stub
}
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
public Spliterator<T> spliterator() {
// TODO Auto-generated method stub
return null;
}
}

Endless iterator loop on doubly linked list using Java

I have created a doubly-linked list and implemented my own iterator.
However, I have done something wrong and my iterator results in endless loop.
Have been struggling to find the error, so any feedback is much appreciated. Thanks in advance. I apologise for the wall of code, I think the error lies within my iterator of the way I have created my Node class.
My Code:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyDoubleEndedLinkedList<T extends Comparable<T>> implements
Iterable<T> {
// initialising Nodes including the two sentinal nodes
private Node<T> head;
private Node<T> tail;
private Node<T> current;
private int currentsize;
MyDoubleEndedLinkedList() {
head = new Node<T>();
tail = new Node<T>();
head.setNext(tail);
tail.setPrevious(head);
current = head;
currentsize = 0;
// Methods used to help loop and iterate through the list
public boolean isEmpty() {
return (current == head && current == tail);
}
public boolean endList() {
return (current != tail);
}
public void resetCurrent() {
current = head;
}
public void nextCurrent() {
current = current.getNext();
}
public T getCurrent() {
return current.getData();
}
public int size() {
return this.currentsize;
}
#Override
public Iterator<T> iterator() {
return new LinkedListIterator<T>();
}
// Node class for doublyLinkedList
public class Node<E> {
private Node<E> previous;
private Node<E> next;
private E data;
Node() {
previous = null;
next = null;
data = null;
}
Node(Node<E> newPrevious, Node<E> newNext, E newData) {
previous = newPrevious;
next = newNext;
data = newData;
}
// set previous node
public void setPrevious(Node<E> newPrevious) {
previous = newPrevious;
}
// set Next node
public void setNext(Node<E> newNext) {
next = newNext;
}
public void setData(E newData) {
data = newData;
}
public Node<E> getPrevious() {
return previous;
}
public Node<E> getNext() {
return next;
}
public E getData() {
return data;
}
}
class LinkedListIterator<E> implements Iterator<T> {
private Node<T> current;
private Node<T> previous;
private Node<T> previous2;
private boolean removeCalled;
public LinkedListIterator() {
current = head;
previous = null;
previous2 = null;
removeCalled = false;
}
public boolean hasNext() {
return (current != null);
}
public T next() {
if (hasNext()) {
T temp = current.getData();
previous2 = previous;
previous = current;
current = current.next;
removeCalled = false;
return temp;
}
throw new NoSuchElementException();
}
public void remove() {
if (previous == null || removeCalled) {
throw new IllegalStateException();
}
if (previous2 == null) {
head = current;
} else {
previous2.setNext(current);
previous = previous2;
}
removeCalled = true;
throw new UnsupportedOperationException();
}
}}
So i can't find the bug in your code but here is a simpler implementation of a basic linked list in Java. If you show me how you're adding elements to the list it would be easier to track down.
import java.util.Iterator;
public class MyLinkedList<T> implements Iterable<T> {
private Node head = null;
private Node tail = null;
public static void main(String[] args) {
MyLinkedList<String> li = new MyLinkedList<>();
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
for (String s : li) {
System.out.println(s);
}
}
public void add(T data) {
if (head == null) {
head = new Node(data, null);
tail = head;
} else {
Node n = new Node(data, tail);
tail.next = n;
tail = n;
}
}
#Override
public Iterator<T> iterator() {
return new Iterator<T>() {
Node current = head;
#Override
public boolean hasNext() {
return current != null;
}
#Override
public T next() {
T data = current.data;
current = current.next;
return data;
}
#Override
public void remove() {
}
};
}
class Node {
final T data;
Node prev = null;
Node next = null;
Node(T data, Node prev) {
this.data = data;
this.prev = prev;
}
}
}

what is wrong with this deque implementation

I want to write implementation of Deque
I have written code below but unfortunately I have a problem with it
when I use addFirst and addLast or even removeFirst every thing is ok
but when I use removelast the programs throws NullPointerException
I dont know what is the problem exactly so I got really confused
Can Anyone plaese Help me??
Thanks in advance for your attention
Node Class::
public class Node<E>{
E element;
Node<E> prev , next;
public Node(E element, Node<E> prev, Node<E> next) {
this.element = element;
this.prev = prev;
this.next = next;
}
public Node() {
this(null, null, null);
}
public void setNext(Node next)
{
this.next = next;
}
public void setPrev(Node prev)
{
this.prev = prev;
}
public Node getNext()
{
return next;
}
public Node getPrev()
{
return prev;
}
}
here is Deque Interface::
public interface DQ<E> {
public int size();
public boolean isEmpty();
public E getFirst();
public E getLast();
public void addFirst (E element);
public void addLast (E element);
public E removeFirst();
public E removeLast();
}
and finally here is MyDQ Class which implements DQ class::
public class MyDQ<E> implements DQ<E>{
Node<E> head , tail;
int size = 0;
#Override
public int size() {
return size;
}
#Override
public boolean isEmpty() {
return size == 0;
}
#Override
public E getFirst() {
if(head == null)
return null;
return head.element;
}
#Override
public E getLast() {
if(head == null)
return null;
return tail.element;
}
#Override
public void addFirst(E element) {
Node<E> n = new Node<>(element, null, null);
if(head == null)
head = tail = n;
else
{
head.setPrev(n);
n.setNext(head);
head = n;
}
size++;
}
#Override
public void addLast(E element) {
Node<E> n = new Node<>(element, null, null);
if(head == null)
head = tail = n;
else
{
tail.setNext(n);
n.setPrev(head);
tail = n;
}
size++;
}
#Override
public E removeFirst() {
if(head == null)
return null;
Node<E> n = head;
head = head.getNext();
head.setPrev(null);
n.setNext(null);
size --;
return n.element;
}
#Override
public E removeLast() {
if(head == null)
return null;
Node<E> n = tail;
tail = tail.getPrev();
tail.setNext(null);
n.setPrev(null);
size --;
return n.element;
}
}
I created a simple Person object to use:
public class Person {
String name;
Integer age;
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
}
I tested this using the following unit test:
#Test
public void testOneElementDeque() {
MyDQ<Person> deq = new MyDQ<>();
Person p1 = new Person("John", 12);
Person p2 = new Person("Eric", 45);
deq.addLast(p1);
assertEquals(p1, deq.getLast());
assertEquals(p1, deq.getFirst());
deq.removeLast();
}
This throws a null pointer at this line(line 78):
tail.setNext(null);
By this point in the call tail has been set to null by this line:
tail = tail.getPrev();
To correct this I rewrote the implementaion to null check tail at this point:
public E removeLast() {
if(tail == null)
return null;
Node<E> n = tail;
tail = tail.getPrev();
if(tail != null)
tail.setNext(null);
n.setPrev(null);
size --;
return n.element;
}

Categories