Skip to content

Commit

Permalink
Circular LinkedList Assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
hadiali6 committed Nov 28, 2023
1 parent c4d77ff commit bde8257
Show file tree
Hide file tree
Showing 4 changed files with 727 additions and 0 deletions.
223 changes: 223 additions & 0 deletions Assignment04/AbstractList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* The abstract class provides basic structure of the circular
* linked list with the tail and size references
*
* @author Varik Hoang <varikmp@uw.edu>
*
* @param <Type> is any object type
*/
public abstract class AbstractList<Type>
implements List<Type>
{
/**
* The reference to the last element
*/
protected ListNode<Type> tail;

/**
* The size of the list
*/
protected int size;

/**
* The constructor that initiate the tail and size references
*/
public AbstractList()
{
tail = null;
size = 0;
}

@Override
public int getSize()
{
return size;
}

@Override
public boolean isEmpty()
{
return size == 0;
}

@Override
public int getIndex(final Type value)
{
if (value == null)
throw new NullPointerException("The value could not be null");

if (tail == null)
return -1;

ListNode<Type> current = tail.next; // point to head
for (int index = 0; index < size; index++)
{
if (current.data.equals(value))
return index;
current = current.next;
}

return -1;
}

@Override
public String toString()
{
if (size == 0)
return "[]";

StringBuilder builder = new StringBuilder();
builder.append("[");

ListNode<Type> current = tail.next; // head node
while (current != tail)
{
builder.append(current.data).append(",").append(" ");
current = current.next;
}
builder.append(tail.data);

builder.append("]");
return builder.toString();
}

/**
* Returns an iterator for this list.
*
* @return an iterator for the list.
*/
public Iterator<Type> iterator()
{
return new LinkedIterator();
}

/**
* Represents a list node.
*
* @author Building Java Programs 3rd ed.
* @param <Type> is of any object type
*/
protected static class ListNode<Type>
{
/**
* Data stored in this node.
*/
public final Type data;

/**
* Link to next node in the list.
*/
public ListNode<Type> next;

/**
* Constructs a node with given data and a null link.
*
* @param data assigned
*/
public ListNode(Type data)
{
this(data, null);
}

/**
* Constructs a node with given data and given link.
*
* @param data assigned
* @param next assigned
*/
public ListNode(Type data, ListNode<Type> next)
{
this.data = data;
this.next = next;
}

/**
* The method returns the generic data type as string
*/
public String toString()
{
return this.data.toString();
}
}

/**
* The iterator class for the list.
*
* @author modified from BuildingJavaPrograms 3rd Edition
*/
public class LinkedIterator implements Iterator<Type>
{
/**
* Location of current value to return.
*/
private ListNode<Type> current;

/**
* The flag that tells if the first node has been visited.
*/
private boolean visited;

/**
* Constructs an iterator for the given list.
*/
public LinkedIterator()
{
reset();
}

/**
* Returns whether there are more list elements.
*
* @return true if there are more elements left, false otherwise
* @see java.util.Iterator#hasNext()
*/
public boolean hasNext()
{
if (tail != null)
return !(current == tail.next && visited);
else
return false;
}

/**
* Returns the next element in the iteration.
*
* @throws NoSuchElementException if no more elements.
* @return the next element in the iteration.
* @see java.util.Iterator#next()
*/
public Type next()
{
if (!hasNext())
{
throw new NoSuchElementException();
}
Type result = current.data;
current = current.next;
visited = true;
return result;
}

/**
* This method is not supported
*/
public void remove()
{
throw new IllegalStateException("This iterator does not support remove operation");
}

/**
* The method resets back to the beginning.
*/
public final void reset()
{
if (tail != null)
current = tail.next;
visited = false;
}
}

}
106 changes: 106 additions & 0 deletions Assignment04/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import java.util.Iterator;

/**
* Represents List interface.
*
* @author Varik Hoang
* @version Sep 26, 2016
* @param <Type> is of any object type.
*/
public interface List<Type>
{
/**
* The method returns the current number of elements in the list.
*
* @return the current number of elements in the list greater than or equal 0
*/
public int getSize();

/**
* The method returns whether the list is empty.
*
* @return true if list is empty, false otherwise.
*/
public boolean isEmpty();

/**
* The method returns whether value is in the list.
*
* @param value the value is assigned
* @return true if value in the list, false otherwise.
*/
public boolean contains(Type value);

/**
* The method inserts an element into the list.
*
* @param value the value is assigned
*/
public void insert(Type value);

/**
* The method clears the list.
*/
public void clear();

/**
* The method returns a string representation of list contents.
*
* @return a string representation of list contents.
* @see Object#toString()
*/
@Override
public String toString();

/**
* /**
* The method removes first element occurrence from the list.
*
* @param value the value is assigned
* @return the removed value
*/
public Type remove(Type value);

/**
* The method returns the index of value.
*
* @param value the value is assigned.
* @return the index of value if in the list, -1 otherwise.
*/
public int getIndex(Type value);

/**
* The method removes value at the given index.
*
* @param index the index must be in range of 0 and size
* @return the removed value
* @throws IndexOutOfBoundsException if index less than 0 or index greater than
* or equal size
*/
public Type removeAtIndex(int index);

/**
* The method replaces the value at the given index with the given value.
*
* @param index the index must be in range of 0 and size
* @param value the value is assigned
* @throws IndexOutOfBoundsException if index less 0 or index greater than size
*/
public void set(int index, Type value);

/**
* Returns the value at the given index in the list.
*
* @param index the index must be in range of 0 and size
* @throws IndexOutOfBoundsException if index less than 0 or greater size
* @return the value at the given index in the list.
*/
public Type get(int index);

/**
* The method returns an iterator for this list.
*
* @return an iterator for the list.
*/
public Iterator<Type> iterator();
}
Loading

0 comments on commit bde8257

Please sign in to comment.