1.java使用linkedlish实现栈结构
class LinkedStack<T>{
private LinkedList<T> list=new LinkedList<T>();
public void push(T v){
list.addFirst(v);
}
public T peek(){
return list.getFirst();
}
public T pop(){
return list.removeFirst();
}
}
2.java栈实现
Stack<String> strStack=new Stack<String>();
// strStack.push("1");
// strStack.push("2");
// strStack.push("3");
// strStack.push("4");
// strStack.push("5");
// strStack.push("6");
// strStack.push("7");
// strStack.push("8");
// strStack.push("9");
Collections.addAll(strStack,"1 2 3 4 5 6 7 8 9 10 11".split(" "));
strStack.pop();
strStack.pop();
while(!strStack.isEmpty()){
System.out.println(strStack.pop());
}
3.java队列实现
Queue<Character> queue=new LinkedList<Character>();
for(Character c:"sljjoiaflajfoajlawasfj".toCharArray()){
queue.offer(c);
}
queue.poll();
queue.remove();
while(!queue.isEmpty()){
System.out.println("----"+queue.poll());
}
队列几个方法的说明:
offer():Queue相关方法,将元素插入到队尾或者返回false
peek():不移除的情况下返回队头,当队列为空的时候返回null
element():不移除的情况下返回队头,队列为空时抛 NoSuchElementException异常
poll():移除并且返回队头,队为空返回null
remove():移除并且返回队头,队为空抛出NoSuchElementException异常
4.java优先队列实现
String str="EDUCATION SHOULD ESCHEW OBFUSCATION";
java.util.List<String> strings = Arrays.asList(str.split(""));
PriorityQueue<String> stringPQ=new PriorityQueue<String>(strings);
queuePring(stringPQ);
stringPQ=new PriorityQueue<String>(strings.size(),Collections.reverseOrder());
stringPQ.addAll(strings);
queuePring(stringPQ);