DoublyLinkedListDummy Start

This commit is contained in:
RustyRaptor committed 2019-10-03 16:33:27 -06:00
1 parent 2a7520c861
commit 0f603a4dcf
2 files changed
+284

No files matched your search

+156
View File
@@ -0,0 +1,156 @@
/******************************************************************************
* DIntNode.java
*
* A DIntNode provides a node for a doubly linked list with
* integer data in each node.
*
* Lists of nodes can be made of any length, limited only by the amount of
* free memory in the heap. But beyond Integer.MAX_VALUE
*
* @author Huiping Cao
*
*
******************************************************************************/
public class DIntNode
{
private int data; //The integer value for the node
private DIntNode next = null; //The next node of the current one
//For the last node of a list, this is either null
//or is a reference to the dummy tail node
private DIntNode prev = null; //The previous node of the current one
//For the first node of a list, this is either null
//or is a reference to the dummy head node
/**
* No argument constructor
*/
public DIntNode(){;}
/**
* Initialize a node with a specified initial data
* @param initialData: the initial data of this new node
* @postcondition This node contains the specified data;
* The prev and next are all null references.
**/
public DIntNode(int initialData)
{
data = initialData;
}
/**
* Initialize a node with a specified initial data and link to the next and prev
* nodes. Note that the initialNext and initialPrev may be null,
* which indicates that the new node has nothing before and/or after it.
* @param initialData: the initial data of this new node
* @param initialPrev: the previous node reference of this node
* @param initialNext: the next node reference of this node
**/
public DIntNode(int initialData,DIntNode initialPrev,DIntNode initialNext)
{
data = initialData;
prev = initialPrev;
next = initialNext;
}
/**
* toString method convert this node's information to a String
*/
public String toString()
{
String str="";
//include previous node's information
if(prev==null) str+="(null)";
else str += "("+prev.data+")";
//include the current data
str += data;
//include the next node's information
if(next==null) str+="(null)";
else str += "("+next.data+")";
return str;
}
/**
* Accessor method to get data from this node.
* @param - none
* @return the data from this node
*/
public int getData( )
{
return data;
}
/**
* Accessor method to get a reference to the next node after this node.
* @param - none
* @return a reference to the node after this node (or the null reference if there
* is nothing after this node)
**/
public DIntNode getNext( )
{
return next;
}
/**
* Accessor method to get a reference to the previous node before this node.
* @param - none
* @return a reference to the node before this node (or the null reference if there
* is nothing before this node)
**/
public DIntNode getPrev( )
{
return prev;
}
/**
* Modification method to set the data in this node.
* @param newData: the new data to place in this node
* @postcondition The data of this node has been set to newData.
**/
public void setData(int newData)
{
data = newData;
}
/**
* Modification method to set the next link to newnext.
* @param newnext: a reference to the node that should appear after this node in the linked
* list (or the null reference if there is no node after this node)
* @postcondition The link to the node after this node has been set to newnext.
**/
public void setNext(DIntNode newnext)
{
next = newnext;
}
/**
* Modification method to set the previous link to newprev.
* @param newprev a reference to the node that should appear before this node in the linked
* list (or the null reference if there is no node before this node)
* @postcondition The link to the node before this node has been set to newprev.
*/
public void setPrev(DIntNode newprev)
{
prev = newprev;
}
/**
* This method increase the value of data by doubling its value
* This can be used for testing the clone method
*/
public void dataDouble()
{
data = 2*data;
}
}
+128
View File
@@ -0,0 +1,128 @@
public class DoublyLinkedListDummy {
private DIntNode head;
private DIntNode tail;
public DoublyLinkedListDummy() {
this.head.setNext(tail);
this.tail.setPrev(head);
}
/**
* Adds a Node to the end of the list. (after the tail)
* @param element The data to add.
*
* Time Complexity: O(1)
*/
public void addEnd(int element) {
this.tail = new DIntNode(element, this.tail,
this.tail.getPrev());
}
/**
* Removes the first node in this list. (The head)
* Time Complexity: 0(1)
*/
public void removeFromHead() {
this.setHead(this.getHead().getNext());
this.getHead().setPrev(null);
}
@Override
public String toString() {
return "DoublyLinkedListDummy{" +
"head=" + head +
", tail=" + tail +
'}';
}
/**
* Returns the count of a given data
* @param e the data we want to count occurences of.
* @return the count of the data
*
* Note: In this case the input itself is constant. The real input to
* base the time complexity on is the size of the list in the object
* it's being called from.
*
* Time Complexity: O(n)
*/
public int countOccurrence(int e) {
int count = 0;
DIntNode cursor = this.head;
while (cursor.getNext() != null){
if (cursor.getData() == e) {
count++;
}
cursor = cursor.getNext();
}
return count;
}
/**
* Removes all elements that match a given integer from the list.
* @param e an integer to remove from the list.
* @return true or false if one was found and removed.
* Time Complexity: O(n) where n is the size of the array we are
* searching in.
*/
public boolean removeAll(int e) {
DIntNode cursor = this.head;
boolean result = false;
while (cursor.getNext() != null){
if (cursor.getData() == e) {
cursor.getPrev().setNext(cursor.getNext());
cursor.getNext().setPrev(cursor.getPrev());
result = true;
}
cursor = cursor.getNext();
}
return result;
}
/**
* returns a sublist of the current list
* @param beginIdx The starting index of the sublist
* @param endIdx The ending index of the sublist
* @return a new List being a sublist of this list.
*
* Time Complexity: O(n) where n is the number of elements from the
* start of the host list to endIdx
*
*/
public DoublyLinkedListDummy subList(int beginIdx, int endIdx){
DoublyLinkedListDummy newList = new DoublyLinkedListDummy();
DIntNode cursor = this.head;
for (int i = 0; i <= endIdx; i++){
if(i == beginIdx){
newList.setHead(cursor);
} else if (i > beginIdx) {
newList.addEnd(cursor.getData());
}
cursor = cursor.getNext();
}
return newList;
}
// public void printStatistics() {
//
// }
public DIntNode getHead() {
return head;
}
public void setHead(DIntNode head) {
this.head = head;
}
public DIntNode getTail() {
return tail;
}
public void setTail(DIntNode tail) {
this.tail = tail;
}
}