Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Everything that I have tried hasn't worked when trying to create a deep copy of this linked list that is created. Whenever I try to use the basic implementation from my textbook it doesn't work because the int can't be dereferenced.

What I have tried:

I have tried to use the basic implementation that my book gives me(listed below) and change the variables to match but it still will not work. It throws errors saying that my LLwithNodeLab class can't contain the arguments that I am giving it.

Java
import java.io.*;
public class LLwithNodeLab implements Cloneable {
    private Node head;
    private int listCount;

    public static void main(String[] args) {
        LLwithNodeLab list = new LLwithNodeLab();
        String data = "LLLabFall2020";
        
        for (int x=0; x<data.length(); x++){
            list.add(data.substring(x,x+1));
        }
        
        System.out.println("Original list:");
        System.out.println(list);
        
        int index = ((int)(Math.random()*10))%list.size() + 1;
        System.out.println("Getting item at index " + index);
        System.out.println(list.get(index)+ " retrieved");
        
        index = ((int)(Math.random()*10))%list.size() + 1;
        System.out.println("Removing item at index " + index);
        list.remove(index);
        System.out.println("Revised list:");
        System.out.println(list);
        
        System.out.println("Looking for " + data.charAt(index));
        System.out.println("Found at index: " + list.listSearch(""+data.charAt(index)));
        System.out.println("Looking for " + "X");
        System.out.println("Found at index: " + list.listSearch("X"));
        
        System.out.println("Removing first item:");
        list.remove(1);
        System.out.println("Revised list:");
        System.out.println(list);
        
        System.out.println("Removing last item");
        list.remove(list.size());
        System.out.println("Revised list:");
        System.out.println(list);
        
        index = ((int)(Math.random()*1000))%list.size() + 1;
        System.out.println("Adding new item at position " + index);
        list.add(""+Math.random(), index);
        System.out.println("Revised list:");
        System.out.println(list);
        
        System.out.println("Testing listPart:");
        int first = ((int)(Math.random()*10))%list.size() + 1;
        int second = ((int)(Math.random()*10))%list.size() + 1;
        while (first >= second) second++;
        while (second > list.size()) {
            second--;
            first--;
        }
        System.out.println("Copying list from index " + first + " to index " + second);
        LLwithNodeLab fragment = list.listPart(first,second);
        System.out.println("Result:");
        System.out.println(fragment);
        
       
        if (fragment != null) {
            System.out.println("Testing clone on partial list:");
            LLwithNodeLab copy = fragment.clone();
            System.out.println("Result:");
            System.out.println(copy);
        }

    }

        

    public LLwithNodeLab() {
        // this is an empty list, so the reference to the head node
        // is set to a new node with no data
        head = new Node(null);
        listCount = 0;
    }

    public void add(Object data)    {
        // post: appends the specified element to the end of this list.
        Node temp = new Node(data);
        Node current = head;
        while(current.getNext() != null) {
            current = current.getNext();
        }
        current.setNext(temp);
        temp.setBack(current);
        listCount++;// increment the number of elements variable
    }

    public void add(Object data, int index) {
        // post: inserts the element at the specified position in this list.
        Node temp = new Node(data);
        Node current = head;
        for(int i = 1; i < index && current.getNext() != null; i++) {
            current = current.getNext();
        }
        temp.setNext(current.getNext());
        temp.setBack(current);
        current.getNext().setBack(temp);
        current.setNext(temp);
        listCount++;// increment the number of elements variable
    }

    public Object get(int index) {
        // post: returns element at specified position (must be >=1) in this list.
        if(index <= 0)
            return null;
        Node current = head.getNext();
        for(int i = 1; i < index; i++) {
            if(current.getNext() == null)
                return null;
            current = current.getNext();
        }
        return current.getData();
    }      

    public int size() {  // post: returns number of elements in list.    
        return listCount;
    }

    public String toString() {
        Node current = head.getNext();
        String output = "|||";
        while(current != null) {
            output += "<=[" + current.getData().toString() + "]=>";
            current = current.getNext();
        }
        output = output + "|||";
        return output;
    }

    private class Node {
        // reference to next node in chain, or null if there isn't one.
        Node next;
        // reference to previous node, or null if there isn't one
        Node back;
        // data carried by this node.
        Object data;
        public Node(Object _data) {
            next = null;
            back = null;
            data = _data;
        }

        public Node(Object _data, Node _next, Node _back) {
            next = _next;
            back = _back;
            data = _data;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object _data) {
            data = _data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node _next) {
            next = _next;
        }

        public Node getBack() {
            return back;
        }

        public void setBack(Node _back) {
            back = _back;
        }
    } // end of inner Node class

    
    // Modify this method so that it works properly for
    // a doubly-linked list; ensure that the node at 
    // index + 1 (if it exists) points back to current
    // before the method returns
    public boolean remove(int index)    {
        // post: removes element at specified position in this list.
        if(index < 1 || index > size())
            return false;
        Node current = head;
        for(int i = 1; i < index; i++)  {
            if(current.getNext() == null)
                return false;
            current = current.getNext();
        }
        current.setNext(current.getNext().getNext());
        if(current.getNext() != null){
            current.getNext().setBack(current);
        }
        listCount--; // decrement the number of elements variable
        return true;
    }

    
    // returns the index where the target item is located or -1 if
    // not found
    public int listSearch(Object target) {
        Node cursor = head.getNext();
        Node checkafter = cursor.getNext();
        int index;
        boolean isFound = false;
        while(isFound = false){
        for(index=1; cursor!=null && cursor.getData().equals(target);index++){
            isFound = true;
            cursor = cursor.getNext();
            return index;
        }
    }
        return -1;
    }
    
       
    

    // returns a copy of the portion of the list from index start to
    // index end; preconditions: start < end, start >= 1, end <= size()
    public LLwithNodeLab listPart (int start, int end) {
        System.out.println("listSearch not done yet");
        return null;
        
    }

    // returns a deep copy of the calling object
     public LLwithNodeLab clone() {
         return null;
    }

  }


Basic implementation from my book:

public static IntNode listCopy(IntNode souce)
    {
IntNode copyHead;
IntNode copyTail;
// Handle the special case of an empty list.
if (source == null)
return null;
// Make the first node for the newly created list.
copyHead = new IntNode(source.data, null);
copyTail = copyHead;
// Make the rest of the nodes for the newly created list.
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
// Return the head reference for the new list.
return copyHead;
}
Posted
Comments
[no name] 16-Nov-20 0:57am    
The "list" has been lost in the details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900