使用泛型在 Java 中创建链表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14557554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating A Linked List In Java Using Generics
提问by user1861162
I've looked through the site and can't find anything that is at least comparable to what I'm doing (please do not link any stackoverflow responses or questions I've been looking for an hour), so I've decided to ask this question. I'm trying to create a linked list (not using libraries of any kind) that uses generics, here is all the relevant code.
我浏览了该网站,但找不到任何至少与我正在做的事情相当的内容(请不要链接我找了一个小时的任何 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;
}
}
}
and MyNode:
和我的节点:
public class MyNode<T>
{
T data;
MyNode<T> next;
MyNode<T> previous;
}
Where I'm really having problems is at the switch statement in LList where I am scanning in items that are supposed to be set to Generics and obviously there is no method using Scanners to read in Generics. So question1, how do I read in and set these to Generics, and question2, in my clear method in LList how do I send variable i when using removeAt when it is expecting a Generic? Please keep all answers relevant and thanks for your time!
我真正遇到问题的地方是 LList 中的 switch 语句,我正在扫描应该设置为泛型的项目,显然没有使用扫描仪读取泛型的方法。所以问题 1,我如何读入并将这些设置为泛型,以及问题 2,在我在 LList 中的 clear 方法中,我如何在使用 removeAt 时发送变量 i 当它期待泛型时?请保持所有答案相关并感谢您的时间!
EditOne more question in my search method inside the do-while there is an if statement that says if(find.compareTo(p.data) == 0) how can I change this so that it works? I wasn't really sure of what to put there so I kind of just wrote my thought down.
编辑在 do-while 中我的搜索方法中的另一个问题,有一个 if 语句表示 if(find.compareTo(p.data) == 0) 如何更改它以使其正常工作?我真的不知道该放什么,所以我只是把我的想法写下来。
回答by Pawe? Piecyk
#1. You can implement generic console input method on your own - here is an example: Improvement/s to my Java generic console input method?
#1. 您可以自己实现通用控制台输入法 - 这是一个示例:对我的 Java 通用控制台输入法的改进?
回答by Mythul
I believe your itemChosen
code should be in the main
method.
我相信你的itemChosen
代码应该在main
方法中。
You clearly need to declare the T type.
您显然需要声明 T 类型。
public void main(String[] args)
{
LList<Integer> myLList = new LList<>();
}
Now add your desired case-switch code.
现在添加您想要的大小写切换代码。