双链表

import java.util.LinkedList;

System.out.println(lList);
}
}

*************结果*****************

[1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]

**************************************************************************************************************************

import java.util.Stack;
public class HelloWorld {
public static void main(String []args) {
int tmp=0;
Stack<Integer> astack = new Stack<Integer>();
// 将10, 20, 30 依次推入栈中
astack.push(10);
astack.push(20);
astack.push(30);

// 将“栈顶元素”赋值给tmp,并删除“栈顶元素”
tmp = astack.pop();
System.out.printf("tmp=%d\n", tmp);

// 只将“栈顶”赋值给tmp,不删除该元素.
tmp = (int)astack.peek();
System.out.printf("tmp=%d\n", tmp);

astack.push(40);
while(!astack.empty()) {
tmp = (int)astack.pop();
System.out.printf("tmp=%d\n", tmp);
}
}

}

 *************结果*****************

tmp=30
tmp=20
tmp=40
tmp=20
tmp=10

 

**************************************************************************************************************************

import java.util.LinkedList;
import java.util.Queue;

public class HelloWorld {
public static void main(String []args) {
//add()和remove()方法在失败的时候会抛出异常(不推荐)
Queue<String> queue = new LinkedList<String>();
//添加元素
queue.offer("a");
queue.offer("b");
queue.offer("c");
queue.offer("d");
queue.offer("e");
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("poll="+queue.poll()); //返回第一个元素,并在队列中删除
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("element="+queue.element()); //返回第一个元素
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("peek="+queue.peek()); //返回第一个元素
for(String q : queue){
System.out.println(q);
}
}
}

 *************结果*****************

a
b
c
d
e
===
poll=a
b
c
d
e
===
element=b
b
c
d
e
===
peek=b
b
c
d
e

 

相关文章:

  • 2020-06-26
  • 2021-12-08
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-03-05
  • 2022-02-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-08
  • 2021-09-28
  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
相关资源
相似解决方案