-1

我浏览了该网站,找不到任何至少与我正在做的事情相媲美的东西(请不要链接我一直在寻找一个小时的任何 stackoverflow 回复或问题),所以我决定问这个问题。我正在尝试创建一个使用泛型的链接列表(不使用任何类型的库),这是所有相关代码。

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

和我的节点:

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

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

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

4

2 回答 2

2

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

于 2013-01-28T07:59:59.357 回答
0

我相信您的itemChosen代码应该在main方法中。

您显然需要声明 T 类型。

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

}

现在添加您想要的大小写切换代码。

于 2013-01-28T08:03:07.000 回答