HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

LinkedList to be used in a stack or queue

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
usedqueuestacklinkedlist

Problem

This all works. Just wanted to make sure I didn't miss anything. Coming from C++ and working my way through Algorithms, 4th ed. Here is my LinkedList class:

```
/*
Linked List Implementation
Based on Algorithms, 4th Edition, Section 1.3
*/

import java.util.Iterator;
import java.util.NoSuchElementException;
/**
*

* @author Taylor
*/
public class LinkedList implements Iterable {

private String name; //Holds the name of the list, if applicable
private Node head;
private Node tail = head;

//Constructor with name
public LinkedList(String name) {
this.name = name;
head = null;
tail = head;
}

//Constructor without name
public LinkedList() {
this.name = "N/A";
head = null;
tail = head;
}

//The nodes for the list
private class Node {

Item data; //The generic data the nodes contain
Node next;
Node prev;

//Node constructor
public Node(Item newData) {
data = newData;
next = null;
prev = null;
}

public String toString() {
return data.toString();
}

//Unnecessary?
private void deleteNode() {
if (this.prev != null) {
this.prev.next = this.next;
}
if (this.next != null) {
this.next.prev = this.prev;
}
}
}

//Checks if the list is empty
private boolean isEmpty() {
return head == null;
}

//Adds node to end of list, for a push
public void addToEnd(Item newData) {
if (isEmpty()) { //If it's the first (or all of the nodes have been removed)
head = new Node(newData);
tail = head;
} else {
tail.next = new Node(newData);
tail.next.prev = tail;
tail = tail.next;
}
}

//Removes node from end of list, for a pop or dequeue
public Item remove

Solution

Naming

Variables should be named using camelCase so e.g

LinkedList StackList;


should be

LinkedList stackList;


Comments

Comments should describe why something is done, not what the code is doing. Also redunant comments should be deleted, because sometimes they are just missleading like

//Removes node from end of list
public Item removeFromFront()


Instead of

//Constructor with name
public LinkedList(String name) {
    this.name = name;
    head = null;
    tail = head;
}

//Constructor without name
public LinkedList() {
    this.name = "N/A";
    head = null;
    tail = head;
}


use constructor chaining

//Constructor without name
public LinkedList() {
    this("N/A");
}

//Constructor with name
public LinkedList(String name) {
    this.name = name;
    head = null;
    tail = head;
}

Code Snippets

LinkedList StackList;
LinkedList stackList;
//Removes node from end of list
public Item removeFromFront()
//Constructor with name
public LinkedList(String name) {
    this.name = name;
    head = null;
    tail = head;
}

//Constructor without name
public LinkedList() {
    this.name = "N/A";
    head = null;
    tail = head;
}
//Constructor without name
public LinkedList() {
    this("N/A");
}

//Constructor with name
public LinkedList(String name) {
    this.name = name;
    head = null;
    tail = head;
}

Context

StackExchange Code Review Q#59683, answer score: 4

Revisions (0)

No revisions yet.