2

I am trying to implement a linked list in java using arrays as the underlying structure. However, I am not sure how to do insert an element in the array after an element and shift the array down by one

    class linkedList{
    char data[];
    int next;
    //constructor
    public linkedList(int MAX){
        data = new char[MAX];
    }

    public void insertFirst(char d){
        if(data[next]==0){
        data[next] = d;
        next++;
        }
        else{
            System.out.println("list is full");
        }
    }




    public void insertAfter (char after ,char value){
        next=0;
        while(data[next] !=after){
            next++;
        }
        char temp = data[next+1];
        data[next+1] = value;

    }

    public void printList(){
        for(int i=0;i<data.length;i++){
            System.out.print(data[i]);
        }
    }
}





public class myLinkedList {

    public static void main(String args[]) {
        linkedList list = new linkedList(9);
        list.insertFirst('T');
        list.insertFirst('H');
        list.insertFirst('L');
        list.insertAfter('H', 'z');
        list.printList();
    }

}

Also would this be considered a linked list?

9
  • Do you really want to do a linked list? Why not just use the ArrayList? Commented Dec 6, 2017 at 18:24
  • It's an assignment Commented Dec 6, 2017 at 18:31
  • You want to see an example of how the insertAt() method would work ? Commented Dec 6, 2017 at 18:34
  • Your assignment doesn't make sense to me -- are you sure that you're reading or interpreting it correctly? A linked-list should not be array-based as it seems to defeat the purpose of using a linked list in the first place. An home-rolled ArrayList, fine, but not a linked list. Commented Dec 6, 2017 at 18:39
  • here is the instruction Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. display (print) the elements of the linked list in order. 4. An method to check if the list is "empty" Commented Dec 6, 2017 at 18:43

1 Answer 1

1

This is not a linked list. What you have is similar to an ArrayList, in that an array is used as the underlying data structure. A linked list is composed of a series of nodes, with each node linked to the next. The linked list is traversed by calling something like node.next() on the current node until the target or the end of the list is reached.

If you want to insert another element into your list structure after reaching the size limit, you will need to create a new array, copy the contents of the old array over, and insert the new element into the array. You can use System.arraycopy() to perform the copying or to shift items within the array.

Sign up to request clarification or add additional context in comments.

5 Comments

How can I create a linked list using arrays ?
using two arrays one for the next[] element and another for data[]
You should create a node class that contains the data[] array and a reference to the next node. Add new nodes by either traversing the list and setting the last next to the new node, or set the next field of the new node to the current head and the head to the new node. In this case, the data[] array is only used for storing the data within the node, not the nodes themselves.
so each array would only contain one element?
Please review this article on linked lists: en.wikipedia.org/wiki/Linked_list

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.