【问题标题】:Create Singly Linked List Menu System using Arrays使用数组创建单链表菜单系统
【发布时间】:2019-02-20 03:55:14
【问题描述】:

我正在尝试使用大小为 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 的数组。我真的不知道该怎么做。任何帮助,将不胜感激。

【问题讨论】:

  • 你在什么时候挣扎?您需要跟踪数组中当前使用的索引。只需在循环之前声明一个整数并相应地递增和递减它。然后,您可以使用此索引在给定位置删除或插入元素。
  • 标题本身就是矛盾的……你要么使用单链表,要么使用数组……但不能同时使用。

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


【解决方案1】:

您需要跟踪当前使用的数组索引。在循环之前声明一个 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;

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

【讨论】:

  • 那么用户如何在前面或末尾插入一个元素呢?我希望程序在菜单系统和允许用户选择输入/删除元素方面与我发布的两个程序相似。
  • 添加部分已经向您展示了如何将元素添加到数组的末尾。如果您想在开头添加一个元素 array[0] = item 会这样做,但事先您需要移动每个右侧的其他项目。看看System.arraycopy()
猜你喜欢
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
相关资源
最近更新 更多