0

So i am seriously in need of help from someone or everyone on improving my program and any corrections if needed. I have listed my codes below and i am hoping to receive input, suggestions, corrections, and advice. Please remember, i am new to programming so please for those of you who have experience, do go to hard on me =(.

package data_structures;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedListDS<E> implements ListADT<E> {
    class Node<E> {
    E data;
    Node<E> next; 
    }

    public Node(E data) {
        this.data = data;
        next = null;
        }

    private Node<E> head, tail;
    private int currentSize;

    public LinkedListDS() {
        head = tail = null;
        currentSize = 0;
        }

    public void addFirst(E obj) {
        if(head == null)
            head = tail = newNode;
        else 
            newNode.next = head;
            head = newNode;
            currentSize++;
            }   

// Adds the Object obj to the end of the list

public void addLast(E o) {
    if(head == null)
        head = newNode;
        tail = newNode;
        currentSize++;
        return;
        {
        tail.next = newNode;
        tail = newNode;
        currentSize++;
        }
}   

// Removes the first Object in the list and returns it.
// Returns null if the list is empty.

public E removeFirst() {
    if(head == null) 
        return null;
        E tmp = head.data;
        head = head.next;
        {
        if (head == null)
    tail = null;
    currentSize++;
    return tmp;
    }
    }

// Removes the last Object in the list and returns it.
// Returns null if the list is empty.

public E removeLast() {
    Node<E> previous = null;
    Node<E> current = head;
        if (current == null) 
            return null;
        while(current.next != null) {
            previous = current;
            current = current.next;
        }
        if(previous = null)
            return removeFirst;
            previous.next = null;
            tail = previous;
            currentSize--;
        return current.data;
        }

// Returns the first Object in the list, but does not remove it.
// Returns null if the list is empty.

public E peekFirst() {
    if(head == null)
        return null;
    return head.data;
    }

// Returns the last Object in the list, but does not remove it.
// Returns null if the list is empty.

public E peekLast() {
    if(tail == null)
        return null;
    return tail.data;
    }

// Removes the specific Object obj from the list, if it exists.
// Returns true if the Object obj was found and removed, otherwise false

public boolean remove(E obj) {
    if(head == null)
        return false;
        {
    if(head.data.equals(obj))
        head = head.next;
        return true;
        }
Node<E> current = head;
while(current.next != null)
{
    if(current.next.data.equals(obj))
    {
        current.next = current.next.next;
        return true;
        }
        current = current.next;
        }
        currentSize--;
        return false,
    }

// The list is returned to an empty state.

public void makeEmpty() {
    head = tail = null;
    currentSize = 0;
    }

// Returns true if the list contains the Object obj, otherwise false

public boolean contains(E obj) {
    Node<E> current = head;
    while(current != null)
    {
        if(current.data.equals(obj)
            return true;
            }
        current = current.next;
            return false;
            }

// Returns true if the list is empty, otherwise false

public boolean isEmpty() {
    return = null;
    }

// Returns true if the list is full, otherwise false

public boolean isFull() {
    return false;
    }

public int size() { return currentSize; }

public iterator<E> iterator() { return new iterator; }

class IteratorHelper implements Iterator<E> {
    Node<E> iteratorPointer;
    public IteratorHelper() {
        iteratorPointer = head;
        }
    public boolean hasNext() {
        return iteratorPointer != null;
        }
    public E next() {
        if(!hasNext())
            throw new NoSuchElementException();
        E tmp = iteratorPointer.data;
        iteratorPointer = iteratorPointer.next;
        return tmp;
        }
    public void remove() {
        throw new UnsupportedOperationException();

    }

}

The code below is a Stack Method that i created to go with my LinkedList method, wondering what i might do in order to make it better, what i might/should consider to take out or add. I receive compiler errors as well with this code:

package data_structures;

import java.util.Iterator;

public class Stack<E> implements Iterable<E> {
    private ListADT<E> list;

    public Stack() {
        list = new LinkedListDS<E>( );
        }

    public void push(E obj) {
        list.addFirst(obj);
        }

    public E pop() {
        return list.removeFirst();
        }

    public int size() {}

    public boolean isEmpty() {}

    public boolean isFull() {}

    public E peek() {}

    public boolean contains(E obj) {}   

    public void makeEmpty() {}

    public Iterator<E> iterator() {  
        return list.iterator();                 
        }
    class IteratorHelper implements Iterator<E> {
        Node<E> iteratorPointer;
        public IteratorHelper() {
            iteratorPointer = head;
            }
        public boolean hasNext() {
            return iteratorPointer != null;
            }
        public E next() {
            if(!hasNext())
                throw new NoSuchElementException();
            E tmp = iteratorPointer.data;
            iteratorPointer = iteratorPointer.next;
            return tmp;
            }
        public void remove() {
            throw new UnsupportedOperationException();

}

Now the last method that i have listed below is my Queue method, again looking for advice, input, and correction:

package data_structures;
import java.util.Iterator;

public class Queue<E> implements Iterable<E> {
    private ListADT<E> list;

    public Queue() {
        data = new LinkedList<E>(); 
        }

    public void enqueue(E obj) { 
        data.addLast(value);
        }

    public E dequeue() {
        return data.removeFirst();
        }

    public int size() {}

    public boolean isEmpty() {}

    public boolean isFull() {}

    public E peek() {}

    public boolean contains(E obj) {}

    public void makeEmpty() {} 

    public Iterator<E> iterator() {  
        return data.iterator();
        }

    class IteratorHelper implements Iterator<E> {
        Node<E> iteratorPointer;

    public IteratorHelper() { 
        iteratorPointer = head;
        }

    public boolean hasNext() { 
        return iteratorPointer != null;
        }

    public E next() {
        if(!hasNext())
            throw new NoSuchElementException();
        E tmp = iteratorPointer.data;
        iteratorPointer = iteratorPointer.next;
        return tmp;
        }
    public void remove() {
    throw new UnsupportedOperationException();
    }

}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

I haven't read all that, but it certainly looks like pretty good code.
One point: you use equals when looking for a given object in the list. Are you sure that's what you mean? Equals may return true when the object is a different object but the writers of that Object overrode equals (eg two different String objects will return true if they contain the same sequence of characters)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.