Implementing a Linked List (java) - java

hello im trying to implement a Linked list in java.
As this is a homework assignment I am not allowed to use the built in LinkedList from java.
Currently I have implemented my Node class
public class WordNode
{
private String word;
private int freq;
private WordNode next;
/**
* Constructor for objects of class WordNode
*/
public WordNode(String word, WordNode next )
{
this.word = word;
this.next = next;
freq = 1;
}
/**
* Constructor for objects of class WordNode
*/
public WordNode(String word)
{
this(word, null);
}
/**
*
*/
public String getWord()
{
return word;
}
/**
*
*/
public int getFreq(String word)
{
return freq;
}
/**
*
*/
public WordNode getNext()
{
return next;
}
/**
*
*/
public void setNext(WordNode n)
{
next = n;
}
/**
*
*/
public void increment()
{
freq++;
}
}
and my "LinkedList"
public class Dictionary
{
private WordNode Link;
private int size;
/**
* Constructor for objects of class Dictionary
*/
public Dictionary(String L)
{
Link = new WordNode(L);
size = 1;
}
/**
* Return true if the list is empty, otherwise false
*
*
* #return
*/
public boolean isEmpty()
{
return Link == null;
}
/**
* Return the length of the list
*
*
* #return
*/
public int getSize()
{
return size;
}
/**
* Add a word to the list if it isn't already present. Otherwise
* increase the frequency of the occurrence of the word.
* #param word The word to add to the dictionary.
*/
public void add(String word)
{
Link.setNext(new WordNode(word, Link.getNext()) );
size++;
}
Im having trouble with implementing my add method correctly, as it has to check the list, for wether the word allready exists and if do not exists, add it to the list and store it alphabetic.
Ive been working on a while loop, but cant seem to get it to work.
Edit: Ive been trying to print the list but it wont print more than the first added word
public void print()
{
WordNode wn = Link;
int size = 0;
while(wn != null && size <= getSize()){
System.out.println(wn.getWord()+","+wn.getFreq(wn.getWord()));
size++;
}
}
Any help is appreciated

Your add method is wrong. You're taking the root node and setting its next value to the new node. So you will never have any more than 2 nodes. And if you ever have 0, it will probably crash due to a null pointer.
What you want to do is set a current value to the root node, then continue getting the next node until that node is null. Then set the node.
WordNode current = Link;
// Check if there's no root node
if (current == null) {
Link = new WordNode(word);
} else {
// Now that the edge case is gone, move to the rest of the list
while (current.getNext() != null) {
/* Additional checking of the current node would go here... */
current = current.getNext();
}
// At the last element, at then new word to the end of this node
current.setNext(new WordNode(word));
}
You need to keep the instance of the previous node so you can set the next value. Since this would cause a problem if there are no nodes to begin with, there needs to be some extra logic to handle a root node differently. If you'll never have 0 nodes, then you can remove that portion.
If you also need to check the values of the variables in order to see if it's there, you can add something to the while loop that looks at the current value and sees if it is equal to the current word you're looking for.

WordNode current = Link;
while (current != null) {
//check stuff on current word
current = current.getNext();
}

Without your loop code, it will be hard to help you with your specific problem. However, just to give you a bit of a hint, based on the instructions you don't actually have to search every node to find the word. This will give you the opportunity to optimize your code a bit because as soon as you hit a word that should come after the current word alphabetically, then you can stop looking and add the word to the list immediately before the current word.
For instance, if the word you're adding is "badger" and your words list are
apple-->avocado-->beehive-->...
You know that beehive should come after badger so you don't have to keep looking. What you will need to implement is a method that can do alphabetical comparison letter-by-letter, but I'll leave that to you ;-)

Assuming that you always insert in alphabetical order it should look something like this:
public void add(String word){
WordNode wn = Link;
WordNode prevNode = null;
while(wn != null){
if(word.equals(wn.getWord())){
//match found, increment frequency and finish
wn.increment();
return;
}else if(word.compareTo(wn.getWord) < 0){
//the word to add should come before the
//word in the current WordNode.
//Create new link for the new word,
//with current WordNode set as the next link
WordNode newNode = new WordNode(word, wn)
//Fix next link of the previous node to point
//to the new node
if(prevNode != null){
prevNode.setNext(newNode);
}else{
Link = newNode;
}
//increase list size and we are finished
size++;
return;
}else{
//try next node
prevNode = wn;
wn = wn.getNext();
}
}
//If we got here it means that the new word
//should be added to the end of the list.
WordNode newNode = new WordNode(word);
if(prevNode == null){
//if list was originally empty
Link = newNode;
}else{
//else append it to the end of existing list
prevNode.setNext(newNode);
}
//increment size and finish
size++;
}

Use this code :
class NodeClass {
static Node head;
static class Node {
int data;
Node next;
//constractor
Node(int d) {
data=d;
next=null;
}
}
static void printList(Node node ) {
System.out.println("Printing list ");
while(node!=null){
System.out.println(node.data);
node=node.next;
}
}
static void push(int data) {
Node node = new Node(data);
node.next = head;
head = node;
}
static void addInLast(int data) {
Node node = new Node(data);
Node n = head;
while(n.next!=null){
n= n.next;
}
n.next = node;
node.next = null;
}
public static void main (String[] args) {
NodeClass linklistShow = new NodeClass();
linklistShow.head = new Node(1);
Node f2 = new Node(2);
Node f3 = new Node(3);
linklistShow.head.next = f2;
f2.next =f3;
printList(linklistShow.head);
push(6);
addInLast(11);
printList(linklistShow.head);
}
}

Related

Implementation of the 'set' method for a linked List?

I am trying to implement the set method where you pass in the position in a linked list that you want and the value and the set function adds that value into the position specified in the linked list. I have implemented the set function but for some reason The last element disappears in my implementation. I would greatly appreciate any help. Thanks in advance. I would appreciate any expert eyes that will see what I am missing.
/**
* A basic singly linked list implementation.
*/
public class SinglyLinkedList<E> implements Cloneable, Iterable<E>, List<E> {
//---------------- nested Node class ----------------
/**
* Node of a singly linked list, which stores a reference to its
* element and to the subsequent node in the list (or null if this
* is the last node).
*/
private static class Node<E> {
E value;
Node<E> next;
public Node(E e)
{
value = e;
next = null;
}
}
//----------- end of nested Node class -----------
// instance variables of the SinglyLinkedList
private Node<E> head = null; // head node of the list (or null if empty)
private int size = 0; // number of nodes in the list
public SinglyLinkedList() {
} // constructs an initially empty list
// access methods
/**
* Returns the number of elements in the linked list.
*
* #return number of elements in the linked list
*/
public int size() {
return size;
}
/**
* Adds an element to the end of the list.
*
* #param e the new element to add
*/
public void addLast(E e) {
// TODO
}
/**
* Tests whether the linked list is empty.
*
* #return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() {
return size == 0;
}
#Override
public E get(int i) throws IndexOutOfBoundsException {
Node<E> a = head;
if(i<=this.size()) {
int count = 0;
while(count < i) {
count ++;
a = a.next;
}
return a.value;
}
return null;
}
#Override
public E set(int i, E e) throws IndexOutOfBoundsException {
Node<E> current = head;
Node<E> setNode = new Node<E>(e);
if(i==0) {
this.addFirst(e);
}
else if(i==this.size){
this.addLast(e);
}
else {
for(int j=0; current != null && j < (i-1);j++) {
current = current.next;
}
Node<E> temp = current.next;
current.next = setNode;
setNode.next = temp;
}
return setNode.value;
}
// update methods
/**
* Adds an element to the front of the list.
*
* #param e the new element to add
*/
public void addFirst(E e) {
Node<E> first = new Node<>(e);
first.next = this.head;
this.head = first;
this.size++;
}
#SuppressWarnings({"unchecked"})
public boolean equals(Object o) {
// TODO
return false; // if we reach this, everything matched successfully
}
#SuppressWarnings({"unchecked"})
public SinglyLinkedList<E> clone() throws CloneNotSupportedException {
// TODO
return null;
}
/**
* Produces a string representation of the contents of the list.
* This exists for debugging purposes only.
* #return
*/
public String toString() {
for(int i=0;i<this.size();i++) {
System.out.println(this.get(i));
}
return "end of Linked List";
}
public static void main(String[] args) {
SinglyLinkedList <Integer> ll =new SinglyLinkedList <Integer>();
ll.addFirst(5);
ll.addFirst(4);
ll.addFirst(3);
ll.addFirst(2);
ll.set(1,0);
System.out.println(ll);
}
}
Assumptions
addLast method is missing in the code
The error could be there too
Known cause
This code is not incrementing the size inside the set method's for loop.The size is incremented in addLast(possibly) and addFirst and not incremented in other case (final else part)
It is not clear what is planned to do with set method. The way it is implemented now, it behaves more like insert, meaning that it will add new node, bit it does not increase the total count of elements (size).
It the set method is changing the value of the node, which name indicates, then this part is wrong:
else {
for(int j=0; current != null && j < (i-1);j++) {
current = current.next;
}
Node<E> temp = current.next;
current.next = setNode;
setNode.next = temp;
}
It should replace the value instead of adding the new one:
else {
for(int j=0; current != null && j < (i-1);j++) {
current = current.next;
}
current.value=e
}
Also, it looks like the index i is 1-based, while everything else is 0 based. I didn't check the code above, but the concept should be like shown.

How to remove a specific value from linked list in Java?

How to remove a specific value from a linked list java?
I tried to make it in my implementation, but it wasn't easy..
Here is what I'm trying to make:
//How to do this...;<..
int remove(Item item) {
Node cur = first.next;
Node prev = first;
while (cur !=null) {
if (cur.item.equals(item)) {
item = dequeue();
}
cur = cur.next;
// TODO
}
return 0;
}
These are the pre-setup:
public class LinkedQueue<Item> implements Iterable<Item> {
private int N; // number of elements on queue
private Node first; // beginning of queue
private Node last; // end of queue
// helper linked list class
private class Node {
private Item item;
private Node next;
}
/**
* Initializes an empty queue.
*/
public LinkedQueue() {
first = null;
last = null;
N = 0;
assert check();
}
public Item dequeue() {
if (isEmpty()) throw new NoSuchElementException("Queue
underflow");
Item item = first.item;
first = first.next;
N--;
if (isEmpty()) last = null; // to avoid loitering
assert check();
return item;
}
And the main function:
public static void main(String[] args) {
LinkedQueue<String> q = new LinkedQueue<String>();
q.enqueue("a");
q.enqueue("b");
q.enqueue("c");
q.enqueue("a");
q.enqueue("b");
q.enqueue("d");
q.enqueue("b");
q.enqueue("abba");
q.enqueue("a");
q.enqueue("z");
q.enqueue("a");
System.out.println(q);
System.out.println("Remove some of elements.");
q.remove("a");
q.remove("f");
q.remove("c");
System.out.println(q);
}
}
And I have a result like this. It doesn't change at all..
a b c a b d b abba a z a
Remove some of elements.
a b d b abba a z a
It only erase value c. I don't know why.
Since Java 8 there is removeIf(Predicate<? super E> filter) method where you can put your own condition.
list.removeIf(cur -> cur.item.equals(item));
As per the details of question,i assume you are fairly new in Java.
What you are asking and the details you are showing are totally different.
LinkedQueue<String> q = new LinkedQueue<String>();
is only applicable if LinkedQueue is a genreic class not a specific impl for Item type class. i.e you are not creating object of LinkedQueue<Item> class. LinkedQueue<String> and LinkedQueue<Item> is different.
cur.equals(item) lack of knowledge of equal contract and difference in == vs equal. i.e you are comparing a two totally different object. One is a Node and other is Item class object.
Suggestion: clear basics, Read book by cathy Sierra.Scjp Sun Certified Programmer for Java 6
As for answer, you are literally not calling remove from main (test it via a print
statement in remove method). That is why you keep getting same answer.
Note: Your really not can't digest real solution even if we tell.
Following code snippet contains various remove() methods, taken from one of my LinkedList implementation, written in Java.
Code
LinkedList.java (partly)
private int size; // node count,
private LinkedListNode<T> head; // first node,
private LinkedListNode<T> end; // last node,
/**
* Remove by index.
*
* #param k index, start from 0,
* #return value of removed node, or null if not removed,
*/
#Override
public T remove(int k) {
checkElementIndex(k);
// find target node, and remember previous node,
LinkedListNode<T> preNode = null;
LinkedListNode<T> node = head;
while (k-- > 0) {
preNode = node;
node = node.next;
}
T result = (T) node.value; // keep return value,
removeNode(node, preNode); // remove
return result;
}
/**
* Remove by value, only remove the first occurrence, if any.
*
* #param v
* #return whether removed,
*/
#Override
public boolean removeValue(T v) {
// find target node, and remember previous node,
LinkedListNode<T> preNode = null;
LinkedListNode<T> node = head;
while (true) {
if (node == null) return false;// not found,
if (node.getValue().compareTo(v) == 0) break; // value found,
preNode = node;
node = node.next;
}
removeNode(node, preNode); // remove
return true;
}
/**
* Remove by value, remove all occurrences.
*
* #param v
* #return count of nodes removed,
*/
#Override
public int removeAllValue(T v) {
int rc = 0;
// find target node, and remember previous node,
LinkedListNode<T> preNode = null;
LinkedListNode<T> node = head;
while (true) {
if (node == null) return rc; // reach end,
if (node.getValue().compareTo(v) == 0) { // value found,
rc++;
if (removeNode(node, preNode)) break; // remove, break if it's end,
continue; // recheck this node, since it become the next node,
}
preNode = node;
node = node.next;
}
return rc;
}
/**
* Remove given node, which guarantee to exists. Also reduce the size by 1.
*
* #param node node to delete,
* #param preNode previous node, could be null,
* #return indicate whether removed node is end,
*/
protected boolean removeNode(LinkedListNode node, LinkedListNode preNode) {
LinkedListNode nextNode = node.next; // next node,
boolean isEnd = (nextNode == null);
if (isEnd) { // target is end,
if (preNode == null) { // target is also head,
head = null;
} else { // target is not head, thus preNode is not null,
preNode.next = null;
}
end = preNode;
} else { // target is not end,
// replace target with next node,
node.next = nextNode.next;
node.value = nextNode.value;
}
size--; // reduce size by 1,
return isEnd;
}
/**
* Remove head node,
*
* #return
*/
#Override
public T removeHead() {
return remove(0);
}
/**
* Remove end node,
*
* #return
*/
#Override
public T removeEnd() {
return remove(size - 1);
}
LinkedListTest.java (partly)
(unit test, via TestNG)
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* LinkedList test.
*
* #author eric
* #date 1/28/19 6:03 PM
*/
public class LinkedListTest {
private int n = 10;
private LinkedList<Integer> llist; // linked list,
private LinkedList<Integer> dupEvenLlist; // linked list, with duplicated even values,
#BeforeMethod
public void init() {
// init llist,
llist = new LinkedList(); // create linked list,
Assert.assertTrue(llist.isEmpty());
LinkedList.appendRangeNum(llist, 0, n); // append range,
// init dupEvenLlist,
dupEvenLlist = new LinkedList(); // create linked list,
LinkedList.appendRangeNum(dupEvenLlist, 0, n); // append range,
LinkedList.appendRangeNum(dupEvenLlist, 0, n, 2); // append range, again, with step as 2 (only even numbers),
Assert.assertEquals(dupEvenLlist.size(), n + n / 2);
}
// non-remove related test cases ... are deleted,
// remove(k) - remove by index,
#Test
public void testRemoveByIndex() {
for (int i = 0; i < n; i++) {
Assert.assertEquals(llist.removeEnd().intValue(), n - 1 - i); // remove by end, in turn it remove by index,
Assert.assertEquals(llist.size(), n - 1 - i);
}
Assert.assertTrue(llist.isEmpty());
}
// remove(v) - remove by value,
#Test
public void testRemoveByValue() {
Assert.assertFalse(llist.removeValue(n)); // not exists,
for (int i = n - 1; i >= 0; i--) {
Assert.assertTrue(llist.removeValue(i)); // remove by value,
Assert.assertEquals(llist.size(), i);
}
Assert.assertTrue(llist.isEmpty());
Assert.assertFalse(llist.removeValue(0)); // empty,
// remove from list with duplicated value,
for (int i = 0; i < n; i++) {
Assert.assertTrue(dupEvenLlist.removeValue(i));
}
Assert.assertFalse(dupEvenLlist.isEmpty());
Assert.assertEquals(dupEvenLlist.size(), n / 2);
}
// removeAll(v) - remove all occurrences by value,
#Test
public void testRemoveAllByValue() {
Assert.assertEquals(dupEvenLlist.removeAllValue(n), 0); // not exists,
int remainSize = dupEvenLlist.size();
for (int i = 0; i < n; i++) {
int rc = dupEvenLlist.removeAllValue(i); // remove all by value,
Assert.assertEquals(rc, i % 2 == 0 ? 2 : 1);
remainSize -= rc;
Assert.assertEquals(dupEvenLlist.size(), remainSize);
}
Assert.assertTrue(dupEvenLlist.isEmpty());
Assert.assertEquals(dupEvenLlist.removeAllValue(0), 0); // empty,
}
}
All test cases would pass.
Explanation
Methods:
T remove(int k), remove by index.
Steps:
* loop to target node,
* for each step,
record:
* previous node,
* this node,
* get next node, of target node,
* get value of target node,
as return value later,
* if target is end,
* if also head,
head = null;
* if not head,
preNode.next = null;
* end = preNode;
* if targe is not end,
replace it with its next node,
logic:
* node.value = nextNode.value;
* node.next = nextNode.next;
* return previously tracked value of target node,
boolean removeValue(T v), remove by value, only remove first occurrence, if any.
The logic is similar as remove by index.
The differences are:
At initial search, compare element instead of loop to index, to find target.
Return boolean that indicate whether removed, instead of removed value,
int removeAllValue(T v), remove all by value, remove all occurrences.
This is similar as remove by value.
Differences:
[inside while()]
It will search all occurrence till end.
After removing an occurrence, it "continue" to recheck the current node.
Because the current node has actual replaced by it's next.
If removed node is end, then return.
This relay on the return value of removeNode().
It record count of removed occurrence.
[return value]
It return count of removed occurrence, instead of boolean.
boolean removeNode(LinkedListNode node, LinkedListNode preNode), remove by node, with preNode given.
Remove given node, which is guaranteed to exists, with previous node given, which might be null.
Return value indicate whether removed node is end, it's mainly used to support removeAllValue().
T removeHead(), T removeEnd(), remove head / end.
Simply calls remove by index, with corresponding index 0 and size - 1 passed.
Tips:
LinkedList represent linkedlist, with fields size, head, end, and generic type T (for value type in node), it's not thread-safe.
checkElementIndex() method, check given index, and throw exception if out of range.
LinkedListNode, represent the node in linked list. with fields value, next.
Complexity
Remove single: O(k)
Remove all by value: O(n)
Where:
k, is the index of target.
n, is size of linkedlist.
In your if statement you are checking if the cur Node is equal to the Item passed in: if (cur.equals(item)).
I think you should be checking if the Item stored in the cur Node is equal to the Item passed into your function: if (cur.item.equals(item)).

Create new head for a list with data

Sorry for a noob question, but the syntax is a bit confusing here. I am asked to fill in the function to insert new element onto the front of a linked list. The code:
class LinkedList
{
public LinkedList ()
{
this.head = null;
}
/* returns true, if the list is empty, else false */
public boolean isEmpty ()
{
return head == null;
}
/* returns the first element of the list, assuming that the list is not empty */
public int front ()
{
return head.data;
}
/* prints the list */
public void print ()
{
System.out.print("(");
for (ListNode i = head; i != null; i = i.next)
System.out.print(i.data + " ");
System.out.println(")");
}
/* inserts x into the beginning of the list */
public void insertFront (int x)
{
/* FILL HERE!!! */
}
}
The code for the nodes:
class ListNode
{
public ListNode()
{
this.data = 0;
this.next = null;
}
public int data;
public ListNode next;
}
I figured I need to create a new node, assign the value of the current head for the next operator, and set the value equal to x. And then finally set the node to be the new head.
Can someone show me how to perform those basic commands.
Just declare your new element as head and let it point to the previous head, which is now the second element.
/* inserts x into the beginning of the list */
public void insertFront(int x)
{
// Temporarily memorize previous head
ListNode previousHead = this.head;
// Create the new head element
ListNode nextHead = new ListNode();
nextHead.data = x;
// Let it point to the previous element which is now the second element
nextHead.next = previousHead;
// Update the head reference to the new head
this.head = nextHead;
}
Here is a small illustration of the process:

Removing a specific node from a single linked list

I posted a question yesterday about an issue I was having overriding the toString() for this program, but now I have a different problem. The removeItem() method is supposed to remove the node with the given data value (in this case a String name). I'm getting a NullPointerException on line 64 and I can't seem to figure it out for whatever reason. My code is below, and thanks in advance for any help.
public class StudentRegistration<E>
{
private static class Node<E>
{
/** The data value. */
private E data;
/** The link */
private Node<E> next = null;
/**
* Construct a node with the given data value and link
* #param data - The data value
* #param next - The link
*/
public Node(E data, Node<E> next)
{
this.data = data;
this.next = next;
}
/**
* Construct a node with the given data value
* #param data - The data value
*/
public Node(E data)
{
this(data, null);
}
public Node getNext()
{
return next;
}
public E getData()
{
return data;
}
public void setNext(Node append)
{
next = append;
}
}
/** A reference to the head of the list */
private Node<E> head = null;
/** The size of the list */
private int size = 0;
/** Helper methods */
/** Remove the first occurance of element item.
#param item the item to be removed
#return true if item is found and removed; otherwise, return false.
*/
public void removeItem(E item)
{
Node<E> position = head;
Node<E> nextPosition1,
nextPosition2;
while (position != null)
{
if(position.getNext().getData() == item) //NullPointerException
{
nextPosition1 = position.getNext();
nextPosition2 = nextPosition1.getNext();
position.setNext(nextPosition2);
}
else
{
position = position.getNext();
}
}
}
/** Insert an item as the first item of the list.
* #param item The item to be inserted
*/
public void addFirst(E item)
{
head = new Node<E>(item, head);
size++;
}
/**
* Remove the first node from the list
* #returns The removed node's data or null if the list is empty
*/
public E removeFirst()
{
Node<E> temp = head;
if (head != null)
{
head = head.next;
}
if (temp != null)
{
size--;
return temp.data;
} else
{
return null;
}
}
/** Add a node to the end of the list
*#param value The data for the new node
*/
public void addLast(E value)
{
// location for new value
Node<E> temp = new Node<E>(value,null);
if (head != null)
{
// pointer to possible tail
Node<E> finger = head;
while (finger.next != null)
{
finger = finger.next;
}
finger.setNext(temp);
} else head = temp;
}
#Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("[");
Node<E> aux = this.head;
boolean isFirst = true;
while(aux != null)
{
if(!isFirst)
{
sb.append(", ");
}
isFirst = false;
sb.append(aux.data.toString());
aux=aux.next;
}
return sb.append("]").toString();
}
}
Until you are practised in "visualizing" data structures in your head, a good way to understand what is going on is to get a piece of paper and draw a "boxes and pointers" diagram representing the nodes in your data structure (and the relevant fields) ... the local variables in your diagram. Then "hand execute" using a pencil and eraser1.
Don't worry. Linked list insertion and deletion is notoriously tricky for beginners. (That's why it is commonly set as a class exercise in introductory Java and algorithms classes.)
1 - Note careful avoidance of International English faux-pas :-)
You have an exception when you reach the end and there is no next value.
You should be checking like this:
while (position.getNext() != null)
also use equals() instead of == operatoor:
if(position.getNext().getData().equals(item))

When i deep copy a linklist with a constructor, it returns null, when in fact, it isn't null. Java

This is a homework assignment and im just stomped. Can anyone tell me what i am doing wrong?
I am trying to make a copy of a linked List but it says the new list is null.
I try debugging the code with appropriate prints, 1 printing the current node's data to be copyed, and the second to print the newList's last linked node to make sure it is being linked. They seem to be printing the right info, but the list is still null.
run:
h1 is 123456789
Expected: h1 is 123456789
1
2
tail digit:1
3
tail digit:2
4
tail digit:3
5
tail digit:4
6
tail digit:5
7
tail digit:6
8
tail digit:7
9
tail digit:8
h3 is null
Expected: h3 is 123456789
BUILD SUCCESSFUL (total time: 0 seconds)
UPDATE:
I did some further testing with this, and its actually not null and print the correct data,
if(newList.head != null || newList != null)
{
System.out.println ("not null :D");
System.out.println(newList.toString());
}
so that means h3 != newList that i made in the constructor. how do i set it so
HugeNumber h3 = new HugeNumber(h1);
h3 = newList throught the constructor.
public static void main(String[] args)
{
// Create a HugeNumber that is 123456789
HugeNumber h1 = new HugeNumber();
for (int i=9; i>=1; i--)
{
h1.addDigit(i);
}
System.out.println("h1 is " + h1.toString());
System.out.println("Expected: h1 is 123456789");
// Make a copy of h1
HugeNumber h3 = new HugeNumber(h1);
System.out.println("h3 is " + h3.toString());
System.out.println("Expected: h3 is 123456789");
}
class HugeNumber
{
/**
* Inner class to store a digit within a node
*/
private class DigitNode
{
private int digit=0; // Value for this digit
private DigitNode next=null; // Reference to next digit
private DigitNode prev=null; // Reference to previous digit
/**
* DigitNode constructor, initializes number
*/
public DigitNode(int d)
{
digit = d;
}
/* Accessor and mutator methods */
public int getDigit()
{
return digit;
}
public void setDigit(int d)
{
digit = d;
}
public DigitNode getNext()
{
return next;
}
public void setNext(DigitNode nextNode)
{
next = nextNode;
}
public DigitNode getPrev()
{
return prev;
}
public void setPrev(DigitNode prevNode)
{
prev = prevNode;
}
}
// Variable declarations
private DigitNode head = null; // Head points to the most significant digit in the list
private DigitNode tail = null; // Tail points to the least significant digit in the list
/**
* Constructors
*/
public HugeNumber()
{
}
/**
* addDigit adds a new digit, d, as a new most significant
* digit for the list.
* #param d new digit to add as the MSD
*/
public void addDigit(int d)
{
DigitNode nodes = new DigitNode(d);
if (head != null)
{
head.setPrev(nodes);
nodes.setNext(head);
}
head = nodes;
if(tail == null)
{
tail = nodes;
}
}
/**
* Resets the HugeNumber to a null, empty value
*/
public void resetValue()
{
head = null;
tail = null;
}
/**
* #return String The HugeNumber converted to a string
*/
public String toString() {
if (head == null)
{
return null;
}
String print = "";
DigitNode temp = head;
while (temp != null) {
print = print + temp.getDigit();
temp = temp.getNext();
}
return print;
}
/**
* Deep copy constructor
* #param newVal Input HugeNumber to copy to this HugeNumber
*/
This constructor is where i make a copy of it.
public HugeNumber(HugeNumber newVal)
{
// Traverse the input HugeNumber, copying each node to a new list
//
HugeNumber newList = new HugeNumber ();
newList.head = newVal.head;
newList.tail = newList.head;
DigitNode currentNode = new DigitNode(1);
DigitNode OldListNode = newVal.head;
//to make sure the head is copied
System.out.println(newList.head.digit);
while(currentNode != newVal.tail)
{
OldListNode = OldListNode.next;
currentNode = OldListNode;
//to make sure the currentNode is the one we want to copy and it is being transverse
System.out.println(currentNode.digit);
currentNode.setPrev(newList.tail);
newList.tail.setNext(currentNode);
newList.tail = currentNode;
// to make sure the list is linked
System.out.println("tail digit:" + newList.tail.prev.digit);
}
if(newList.head == null || newList == null )
{
System.out.println("--------------------------newList is null :(");
}
}
}
You just create new local variable newList in the constructor method to hold the data, and it will not exist after the invocation of constructor method. You should use this key word
The problem is in your overloaded constructor where you are creating a new object , which is unnecessary. You dont need to explicitly create an object there.The object will be created for you and you can access that using this reference.
Here is your modified constructor.
public HugeNumber(HugeNumber newVal) {
// Traverse the input HugeNumber, copying each node to a new list
//
//HugeNumber this = new HugeNumber();
this();
this.head = newVal.head;
this.tail = this.head;
DigitNode currentNode = new DigitNode(1);
DigitNode OldListNode = newVal.head;
// to make sure the head is copied
System.out.println(this.head.digit);
while (currentNode != newVal.tail) {
OldListNode = OldListNode.next;
currentNode = OldListNode;
// to make sure the currentNode is the one we want to copy and it is
// being transverse
System.out.println(currentNode.digit);
currentNode.setPrev(this.tail);
this.tail.setNext(currentNode);
this.tail = currentNode;
// to make sure the list is linked
System.out.println("tail digit:" + this.tail.prev.digit);
}
if (this.head == null || this == null) {
System.out.println("--------------------------newList is null :(");
}
}

Categories