patternjavaMinor
Custom linked list implementing Iterable used as stack
Viewed 0 times
stackusedlinkedcustomiterablelistimplementing
Problem
I have created a custom linked list that implements the
```
package collections.customCollections.linkedList;
import java.util.Iterator;
public class LinkedList implements Iterable{
private Link head;
private Link tail;
private int size;
public int getSize() {
return size;
}
public LinkedList(){
head = null;
tail = null;
size=0;
}
public void addAtEnd(T data){
Link newNode = new Link(data);
//Insert as first element
if(head == null){
head = newNode;
tail = newNode;
}
else{
newNode.previous = tail;
tail.next = newNode;
tail = newNode;
}
size++;
}
public void addAtStart(T data){
Link newNode = new Link(data);
if(head == null){
head = newNode;
tail = newNode;
}
else{
newNode.next = head;
head.previous = newNode;
head = newNode;
}
size++;
}
//prints the entire linked list
public void print(){
Link traversalNode = head;
if(head == null){
System.out.println("Empty Linked List");
}
else{
while(traversalNode != null){
System.out.print(traversalNode.data +"->");
traversalNode = traversalNode.next;
}
}
}
public T remove(int index){
int i = 1;
Link traversalNode = head;
while(i nodeToRemove = tail;
tail = nodeToRemove.previous;
tail.next = null;
size--;
return nodeToRemove.data;
}
public T removeFromStart(){
Link nodeToRemove = head;
head = nodeToRemove.next;
head.previous = null;
size--;
return nodeToRemove.data;
}
public T get(int index)
{ Link returnedNode = head;
int i = 1;
while(i {
private T data;
private Link next = null;
private Link previous = null;
public Link(T data){
this.data = data;
}
}
Iterable interface. I am then using this linked list to implement my custom stack. I have also thrown a custom exception while removing elements.LinkedList class:```
package collections.customCollections.linkedList;
import java.util.Iterator;
public class LinkedList implements Iterable{
private Link head;
private Link tail;
private int size;
public int getSize() {
return size;
}
public LinkedList(){
head = null;
tail = null;
size=0;
}
public void addAtEnd(T data){
Link newNode = new Link(data);
//Insert as first element
if(head == null){
head = newNode;
tail = newNode;
}
else{
newNode.previous = tail;
tail.next = newNode;
tail = newNode;
}
size++;
}
public void addAtStart(T data){
Link newNode = new Link(data);
if(head == null){
head = newNode;
tail = newNode;
}
else{
newNode.next = head;
head.previous = newNode;
head = newNode;
}
size++;
}
//prints the entire linked list
public void print(){
Link traversalNode = head;
if(head == null){
System.out.println("Empty Linked List");
}
else{
while(traversalNode != null){
System.out.print(traversalNode.data +"->");
traversalNode = traversalNode.next;
}
}
}
public T remove(int index){
int i = 1;
Link traversalNode = head;
while(i nodeToRemove = tail;
tail = nodeToRemove.previous;
tail.next = null;
size--;
return nodeToRemove.data;
}
public T removeFromStart(){
Link nodeToRemove = head;
head = nodeToRemove.next;
head.previous = null;
size--;
return nodeToRemove.data;
}
public T get(int index)
{ Link returnedNode = head;
int i = 1;
while(i {
private T data;
private Link next = null;
private Link previous = null;
public Link(T data){
this.data = data;
}
}
Solution
Package declaration
Formatting
Yes, the boring stuff. You are a bit inconsistent with spacing and I would recommend changing things like this:
Into:
What happened here with the formatting?
Tidying it up:
Another question is regarding The Link class inside the LinkedList class.I get a warning saying "The type parameter T is hiding the type T" where i declare the private class link.I searched about it but couldn't understand the answers i found.
This is because your
package collections.customCollections.linkedList;- By convention, packages should be all lower-case.
- It should be something more uniquely identifiable to you. If you own a domain, you can start with
com.mydomain
- It probably does not need to end with
.linkedlist
- My suggestion would be:
com.mydomain.collectionsorcom.stackexchange.codereview.ishansoni.collectionsif you don't own a domain (or you can probably just make a domain up, usecom.ishansonifor example).
Formatting
Yes, the boring stuff. You are a bit inconsistent with spacing and I would recommend changing things like this:
if(list.getSize() <= 0){Into:
if (list.getSize() <= 0) {What happened here with the formatting?
public T get(int index)
{ Link returnedNode = head;
int i = 1;
while(i < index){
returnedNode = returnedNode.next;
i++;
}
return returnedNode.data;
}Tidying it up:
public T get(int index) {
Link returnedNode = head;
int i = 1;
while (i < index) {
returnedNode = returnedNode.next;
i++;
}
return returnedNode.data;
}Another question is regarding The Link class inside the LinkedList class.I get a warning saying "The type parameter T is hiding the type T" where i declare the private class link.I searched about it but couldn't understand the answers i found.
This is because your
Link class is not static. Non-static inner classes can access all properties and methods of their parent class, therefore the compiler warns you that if you use Link you won't be able to access the ` from LinkedList. Making the class static will solve this warning, and will effectively tell the compiler that you're just not interested in the outside class.
i had to use LinkedList.this.remove(position); inside the remove method of the anonymous iterator class while i could simply call removeFromEnd(). The answer i got for this was that the compiler cannot differentiate due to the same name (Even thou they have different number of arguments) since the anonymous class is still a completely different class. Does that mean if i have a class that implements two different interfaces have methods that have same name but different arguments i'll get the same error?
When using inner classes, this can be an issue yes. But when implementing two different interfaces, this will not be an issue.
Then the custom exception that i created is an unchecked exception since i don't want users to explicitly handle it when they use my stack or list .But i am throwing this exception in my stack impl. If another class uses my linked list they would get a null pointer exception if say they invoked the remove method on an empty list. Should i do it at the list level ?
Yes, you should handle that in your LinkedList class. It is good to make your classes as re-usable as possible. The remove method is part of your LinkedList class so it should handle all aspects of that method properly. You might want to throw a NoSuchElementException` if remove is called on an empty list.Code Snippets
package collections.customCollections.linkedList;if(list.getSize() <= 0){if (list.getSize() <= 0) {public T get(int index)
{ Link<T> returnedNode = head;
int i = 1;
while(i < index){
returnedNode = returnedNode.next;
i++;
}
return returnedNode.data;
}public T get(int index) {
Link<T> returnedNode = head;
int i = 1;
while (i < index) {
returnedNode = returnedNode.next;
i++;
}
return returnedNode.data;
}Context
StackExchange Code Review Q#67131, answer score: 6
Revisions (0)
No revisions yet.