1

I am trying to make a Singly Linked List menu system using an array size of 5, in which it lets the user insert the elements. I have created a solution using Nodes, and an ArrayList, but I am having trouble creating the same solution using an array.

Solution using Nodes:

public class MenuSystem {

public static void main(String[] args) {

    SingLinkedList nodeList = new SingLinkedList();

    Scanner input = new Scanner(System.in);

    boolean done = false;
    while(done == false) 
    {

        System.out.println("");
        System.out.println("Select an Option"); 
        System.out.println("1. Insert an element at the head");
        System.out.println("2. Insert an element at the tail");
        System.out.println("3. Insert an element at a position");
        System.out.println("4. Delete an element at a position");
        System.out.println("5. Check if empty");
        System.out.println("6. Get the size of the list");
        System.out.println("7. Print the contents of the list");
        System.out.println("8. Quit");
        System.out.println("");

        int selection=input.nextInt();

        switch(selection) 
        {
        case 1: //Insert at Front
            System.out.println("Enter Data: ");
            int frontData = input.nextInt();

            nodeList.insertAtFront(frontData);
            break;

        case 2://Insert at tail
            System.out.println("Enter Data: ");
            int tailData = input.nextInt();

            nodeList.insertAtTail(tailData);
            break;

        case 3://Insert at position
            System.out.println("Select Position: ");
            int insertPosition = input.nextInt();

            System.out.println("Enter Data: ");
            int insertData = input.nextInt();

            nodeList.insertAtPosition(insertPosition, insertData);
            break;

        case 4://Delete at position
            System.out.println("Select Position: ");
            int deletePosition = input.nextInt();

            nodeList.deleteAtPos(deletePosition - 1);
            break;

        case 5://Check if empty
            if(nodeList.isEmpty()) 
            {
                System.out.println("List is empty");
            }
            else 
            {
                System.out.println("List is not empty. Contains "+nodeList.getSize()+" elements.");
            }
            break;

        case 6://Get Size of List
            System.out.println("Size is: "+nodeList.getSize());
            break;


        case 7://Print List
            nodeList.printList();
            break;

        case 8:
            System.out.println("The program will now close.");
            done=true;
            break;

        }                       
      }
   }
}

Solution using an ArrayList:

public class MenuSystem {

public static void main(String[] args) {

    ArrayList<Integer> arrayList = new ArrayList<Integer>();

    Scanner input = new Scanner(System.in);

    boolean done = false;
    while(done == false) 
    {

        System.out.println("");
        System.out.println("Select an Option"); 
        System.out.println("1. Insert an element at the head");
        System.out.println("2. Insert an element at the tail");
        System.out.println("3. Insert an element at a position");
        System.out.println("4. Delete an element at a position");
        System.out.println("5. Check if empty");
        System.out.println("6. Get the size of the list");
        System.out.println("7. Print the contents of the list");
        System.out.println("8. Quit");
        System.out.println("");

        int selection=input.nextInt();

        switch(selection) 
        {
        case 1: //Insert at Front
            System.out.println("Enter Data: ");
            int frontData = input.nextInt();

            arrayList.add(0, frontData);
            break;

        case 2://Insert at tail
            System.out.println("Enter Data: ");
            int tailData = input.nextInt();

            arrayList.add(tailData);
            break;

        case 3://Insert at position
            System.out.println("Select Position: ");
            int insertPosition = input.nextInt();

            System.out.println("Enter Data: ");
            int insertData = input.nextInt();

            arrayList.set(insertPosition, insertData);
            break;

        case 4://Delete at position
            System.out.println("Select Position: ");
            int deletePosition = input.nextInt();

            arrayList.remove(deletePosition - 1);
            break;

        case 5://Check if empty
            if(arrayList.isEmpty()) 
            {
                System.out.println("List is empty");
            }
            else 
            {
                System.out.println("List is not empty. Contains "+arrayList.size()+" elements.");
            }
            break;

        case 6://Get Size of List
            System.out.println("Size is: " + arrayList.size());
            break;


        case 7://Print List
            for (int i = 0; i < arrayList.size(); i++) 
            {
                int value = arrayList.get(i);
                System.out.println(value);
            };
            break;

        case 8:
            System.out.println("The program will now close.");
            done = true;
            break;

        }   
    }
  }
}

I'd like to build the same exact program but using an array size of 5. I don't really know how to go about it. Any help would be appreciated.

j.doe
  • 11
  • 2
  • At which point do you struggle? You need to keep track of the currently used index in the array. Simply declare an integer before the loop and increment and decrement it accordingly. This index you can then use to delete or insert elements at the given position. – Kilian Sep 15 '18 at 17:30
  • 1
    The title is contradictory in and of itself... you either use a singly linked list or an array... but not both. – Turing85 Sep 15 '18 at 17:33

1 Answers1

1

You need to keep track of the currently used index of the array. Declare an int before the loop and increment and decrement it accordingly. This index you can then use to delete or insert elements at the given position. Of course you should add range checks to ensure you are not adding an element at index position 5 or greater.

int[] array = new int[5];
int index = 0;

while(loop) {
    if add
        array[index++] = addItem
    if remove
        array[index--] = null
    if insertAtPosition
        array[position] = item
        index = position++;
}

The ++ and -- operators after a variable name first return the value and alter it afterwards.

index = 0
array[index++] = x;     ->      array[0] = x; index += 1;

This solution will of course lead to old values being overwritten. If you want to insert in between you have to move the other objects accordingly.

Kilian
  • 1,540
  • 16
  • 28
  • So how would the user insert an element at the front or the end? I'd like the program to be similar to the two I posted, in terms of the menu system and allowing the user to choose to input/delete elements. – j.doe Sep 15 '18 at 20:09
  • the add section already shows you how to add an element to the end of the array.. if you want to add one at the beginning `array[0] = item` will do this, but beforehand you need to shift every other item to the right. take a look at `System.arraycopy()` – Kilian Sep 15 '18 at 20:21