使用数组创建单链接列表菜单系统

时间:2018-09-15 17:26:05

标签: java arrays arraylist nodes singly-linked-list

我正在尝试使用数组大小​​为5的单链接列表菜单系统,用户可以在其中插入元素。我已经使用Nodes和ArrayList创建了一个解决方案,但是在使用数组创建相同的解决方案时遇到了麻烦。

使用节点的解决方案:

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;

        }                       
      }
   }
}

使用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;

        }   
    }
  }
}

我想构建相同的程序,但使用的数组大小为5。我真的不知道该怎么做。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要跟踪当前使用的数组索引。在循环之前声明一个int,并相应地对其进行递增和递减。然后可以使用该索引在给定位置删除或插入元素。当然,您应该添加范围检查,以确保没有在索引位置5或更大的位置添加元素。

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++;
}

变量名后的++和-运算符首先返回该值,然后对其进行更改。

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

此解决方案当然会导致旧值被覆盖。如果要在两者之间插入,则必须相应地移动其他对象。