使用泛型在 Java 中创建链表



我已经浏览了该网站,找不到任何至少与我正在做的事情相当的东西(请不要链接任何堆栈溢出响应或我已经寻找了一个小时的问题),所以我决定问这个问题。我正在尝试创建一个使用泛型的链表(不使用任何类型的库),这是所有相关代码。

public class LList<T> 
{
MyNode<T> head = null;
MyNode<T> tail = null;
MyNode<T> temp = null;
int count = 0;;
Scanner scan = new Scanner(System.in);
//puts a new value at the edn of the list
public void add(T i)
{
    if(head == null)
    {
        head = new MyNode<T>();
        head.data = i;
        head.next = tail;
        tail = head;
    }
    else
    {
        tail.next = new MyNode<T>();
        tail = tail.next;
        tail.data = i;
    }
    count ++;
}
//inserts a new value at a given index
public void insert(int index, T item)
{
    if(index == size())
    {
        add(item);
    }
    else if(index == 0)
    {
        MyNode<T> temp = new MyNode<T>();
        temp.data = item;
        temp.next = head;
        head.previous = temp;
        head = temp;
        count++;
    }
    temp = head;
    for(int i = 0; i < index-1; i++)
    {
        temp = temp.next;
        MyNode<T> myNode = new MyNode<T>();
        myNode.data = item;
        myNode.next = temp.next;
        temp.next = myNode;
        count++;
    }
}
//returns the number of values in the list
public int size()
{
    return count;
}
//returns the value at a given index
public T get(int index)
{
    if(head == null || index > count -1)
    {
        return null;
    }
    if(index < 0 || index >= count)
    {
        System.out.println("This does not exist");
    }
    MyNode<T> p = head;
    int size = 0;
    while(size < index && p.next != null)
    {
        p = p.next;
        size++;
    }
    if(count != index)
    {
        return null;
    }
    else
    {
        return p.data;
    }
}
//removes the returns the first value in the list
public T remove()
{
    head = head.next;
    head.previous = null;
    count--;
    return (T) head;
}
//removes and returns the value at a given index
public T removeAt(T elem)
{
    temp = head;
    MyNode<T> two = null;
    if(head.data.equals(elem))
    {
        head = head.next;
        head.previous = null;
        count--;
        return elem;
    }
    else if(tail.data.equals(elem))
    {
        tail = tail.previous;
        tail.next = null;
        count--;
        return elem;
    }
    while(temp != null && !temp.data.equals(elem))
    {
        two = temp;
        temp = temp.next;
    }
    if(temp == null)
    {
        return null;
    }
    two.next = temp.next;
    T spare = temp.data;
    temp = null;
    count--;
    return spare;
}
//removes and returns the last value in the list
public T removeLast()
{
    temp = tail;
    tail = tail.previous;
    temp = null;
    count--;
    return (T) tail;
}
//creates a string representation of all the values in the list
public String toString()
{
    String result = null;
    for(int i = 0; i < count; i++)
    {
        result =  i + " : " + get(i).toString(); 
    }
    return result;
}
//removes all the values in the list
public void clear()
{
    for(int i = count -1; i >= 0; i++)
    {
        removeAt(i);
    }
}
//searches for a value in the list and returns the first index of that
//value when found
public int search(T find)
{
    if(head == null)
    {
        return -10;
    }
    MyNode<T> p = head;
    do
    {
        if(find.compareTo(p.data) == 0)
        {
            return 0;
        }
        else
        {
            return -1;
        }
        p = p.next;
    }while(p != null);
}
public void itemChosen(int choice, LLMenu[] menu)
{
    LLMenu m = menu[choice-1];
    switch(m)
    {
    case ADD:
        System.out.println("What value would you like to add?");
        T addThis = scan.nextInt();
        add(addThis);
        break;
    case INSERT:
        System.out.println("What index would you like to replace?");
        T replace = scan.nextInt();
        System.out.println("What number would you like to insert?");
        int val = scan.nextInt();
        insert(val, replace);
        break;
    case SIZE:
        size();
        break;
    case GET:
        System.out.println("What index would you like to look at?");
        int thisOne = scan.nextInt();
        get(thisOne);
        break;
    case REMOVE:
        remove();
        break;
    case REMOVEAT:
        System.out.println("What index would you like to remove?");
        T thisHere = scan.nextInt();
        removeAt(thisHere);
        break;
    case REMOVELAST:
        removeLast();
        break;
    case TOSTRING:
        toString();
        break;
    case CLEAR:
        clear();
        break;
    case SEARCH:
        System.out.println("What value would you like to search for?");
        T searchForMe = scan.nextInt();
        search(searchForMe);
        break;
    }
    }
 }

和MyNode:

public class MyNode<T>
{
T data;
MyNode<T> next;
MyNode<T> previous;
}

我真正遇到问题的地方是在 LList 中的 switch 语句中,我正在扫描应该设置为泛型的项目,显然没有使用扫描仪读取泛型的方法。所以问题 1,我如何读入并将它们设置为泛型,以及问题 2,在 LList 中的清晰方法中,当它需要泛型时,我如何在使用 removeAt 时发送变量 i?请保持所有答案的相关性,并感谢您的时间!

编辑在我的搜索方法中还有一个问题,虽然有一个 if 语句说 if(find.compareTo(p.data) == 0) 我如何更改它以使其工作?我真的不确定该放什么,所以我只是把我的想法写下来。

#1.您可以自己实现通用控制台输入法 - 下面是一个示例:改进我的 Java 通用控制台输入法?

我相信

你的itemChosen代码应该在main方法中。

您显然需要声明 T 类型。

public void main(String[] args)
{
LList<Integer> myLList = new LList<>(); 
}

现在添加所需的机箱切换代码。

相关内容

  • 没有找到相关文章

最新更新