package l6;
/**
* 实现栈的数据结构
*/
import java.util.LinkedList;
public class MyStack {
LinkedList mylist = new LinkedList();
public void push(Object o) {
mylist.addFirst(o);
}
public Object pop() {
return mylist.removeFirst();
}
public Object peek() {
return mylist.getFirst();
}
public boolean empty() {
return mylist.isEmpty();
}
public static void main(String[] args) {
MyStack ms = new MyStack();
ms.push("1");
ms.push("2");
ms.push("3");
ms.push("4");
System.out.println(ms.pop());
System.out.println(ms.pop());
System.out.println(ms.peek());
System.out.println(ms.empty());
}
}
package l6;
import java.util.LinkedList;
/**
* 实现队列的数据结构
*
*/
public class MyQueue {
private LinkedList ll = new LinkedList();
public void put(Object o) {
ll.addLast(o);
}
public Object get() {
return ll.removeFirst();
}
public Object peek() {
return ll.getFirst();
}
public boolean empty() {
return ll.isEmpty();
}
/**
* @param args
*/
public static void main(String[] args) {
MyQueue mq = new MyQueue();
mq.put("1");
mq.put("2");
mq.put("3");
mq.put("4");
mq.put("5");
mq.put("6");
System.out.println(mq.get());
System.out.println(mq.get());
System.out.println(mq.get());
System.out.println(mq.get());
System.out.println(mq.peek());
System.out.println(mq.empty());
}
}
posted on 2011-01-13 22:48
冯占科 阅读(244)
评论(0) 编辑 收藏