* add(E element)向链表末尾添加节点
* add(int index,E element)向链表index处添加值为element节点
* clear()清空链表
* remove(int index)删除index节点
* remove(E element)删除首次出现含有数据element的节点
* get(int index)得到链表指定处的节点的值
* indexOf(E element)返回首次出现含有数据element的节点,没有则返回-1
* lastIndexOf(E element)返回最后一次次出现含有数据element的节点,没有则返回-1
* set(int index,E elemnt)将index处的值该为element
* size()这个就不说了
* contains(E element)判断链表中是否有element
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
public class Main {
public static void main(String []args){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
/**
* add(E element)向链表末尾添加节点
* add(int index,E element)向链表index处添加值为element节点
* clear()清空链表
* remove(int index)删除index节点
* remove(E element)删除首次出现含有数据element的节点
* get(int index)得到链表指定处的节点的值
* indexOf(E element)返回首次出现含有数据element的节点,没有则返回-1
* lastIndexOf(E element)返回最后一次次出现含有数据element的节点,没有则返回-1
* set(int index,E elemnt)将index处的值该为element
* size()这个就不说了
* contains(E element)判断链表中是否有element
*/
LinkedList<Integer> list = new LinkedList<Integer>();
int n,index,value;
n=in.nextInt();
for(int i=0;i<n;i++){
int a;
a=in.nextInt();
list.add(a);
}
//输出链表的值
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+" ");
}
System.out.println("");
//向链表index处添加值为element节点
index=in.nextInt();
value=in.nextInt();
list.add(index,value);
//再次输出链表的值
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+" ");
}
System.out.println("");
//删除index节点
index=in.nextInt();
list.remove(index);
//再次输出链表的值
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+" ");
}
System.out.println("");
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
输出结果:
in:
10
1 2 、3 4 5 6 7 8 9 10
out:
1 2 3 4 5 6 7 8 9 10
in:
5 11
out:
1 2 3 4 5 11 6 7 8 9 10
in:
5
out:
1 2 3 4 5 6 7 8 9 10