栈(类)
public class stack {
public static void main(String[] args) {
Stack st=new Stack<Integer>();
st.push(1);//将元素入栈
st.push(2);
st.push(3);
st.push(31);
st.push(32);
st.push(33);
st.push(331);
// st.push(40);
st.empty();//测试此堆栈是否为空
st.peek();//查看此栈顶元素
st.pop();//删除此堆栈顶部的对象,并将该对象作为此函数的值返回。
st.search(3);//返回一个对象在此堆栈上的基于1的位置。
System.out.println(st);
System.out.println(st.search(3));
}
}
队列(接口)
public class queue {
public static void main(String[] args) {
//接口
Queue queue=new LinkedList<Integer>();
//如果在不违反容量限制的情况下立即执行,则将指定的元素插入到此队列中。
queue.offer(1);
//add()将指定的元素插入到此队列中,如果可以立即执行此操作,而不会违反容量限制,
// true在成功后返回 IllegalStateException如果当前没有可用空间,
// 则抛出IllegalStateException。
queue.add(2);
queue.element();//检索,但不删除这个队列的头
queue.peek();//检索,但不删除这个队列的头,如果此队列为空,则返回null.
queue.poll();//检索并删除此队列的头,如果此队列为空,则返回null.
queue.remove();//检索并删除此队列的头
System.out.println(queue.element());
}
}
相互联系: