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%0A%0D%0A++%CA%B5%CF%D6%B4%FA%C2%EB%A3%BA%0D%0A%0D%0A++package+com.weisou.dataStruct%3B%0D%0A%0D%0A++import+java.util.LinkedList%3B%0D%0A%0D%0A++++%40SuppressWarnings%28%22unchecked%22%29%0D%0A%0D%0A++public+class+MyStack+%7B%0D%0A%0D%0A++LinkedList+linkList+%3D+new+LinkedList%26lt%3BObject%26gt%3B%28%29%3B%0D%0A%0D%0A++public+void+push%28Object+object%29+%7B%0D%0A%0D%0A++linkList.addFirst%28object%29%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++public+boolean+isEmpty%28%29+%7B%0D%0A%0D%0A++return+linkList.isEmpty%28%29%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++public+void+clear%28%29+%7B%0D%0A%0D%0A++linkList.clear%28%29%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A%2F%2F+%D2%C6%B3%FD%B2%A2%B7%B5%BB%D8%B4%CB%C1%D0%B1%ED%B5%C4%B5%DA%D2%BB%B8%F6%D4%AA%CB%D8%0D%0A%0D%0A++public+Object+pop%28%29+%7B%0D%0A%0D%0A++if+%28%21linkList.isEmpty%28%29%29%0D%0A%0D%0A++return+linkList.removeFirst%28%29%3B%0D%0A%0D%0A++return+%22%D5%BB%C4%DA%CE%DE%D4%AA%CB%D8%22%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++public+int+getSize%28%29+%7B%0D%0A%0D%0A++return+linkList.size%28%29%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++public+static+void+main%28String%5B%5D+args%29+%7B%0D%0A%0D%0A++MyStack+myStack+%3D+new+MyStack%28%29%3B%0D%0A%0D%0A++myStack.push%282%29%3B%0D%0A%0D%0A++myStack.push%283%29%3B%0D%0A%0D%0A++myStack.push%284%29%3B%0D%0A%0D%0A++System.out.println%28myStack.pop%28%29%29%3B%0D%0A%0D%0A++System.out.println%28myStack.pop%28%29%29%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++%7D%0D%0A%0D%0A++%B6%D3%C1%D0%B6%A8%D2%E5%0D%0A%0D%0A++%B6%D3%C1%D0%A3%A8Queue%A3%A9%CA%C7%D6%BB%D4%CA%D0%ED%D4%DA%D2%BB%B6%CB%BD%F8%D0%D0%B2%E5%C8%EB%A3%AC%B6%F8%D4%DA%C1%ED%D2%BB%B6%CB%BD%F8%D0%D0%C9%BE%B3%FD%B5%C4%D4%CB%CB%E3%CA%DC%CF%DE%B5%C4%CF%DF%D0%D4%B1%ED%0D%0A%0D%0A++%A3%A81%A3%A9%D4%CA%D0%ED%C9%BE%B3%FD%B5%C4%D2%BB%B6%CB%B3%C6%CE%AA%B6%D3%CD%B7%A3%A8Front%A3%A9%A1%A3%0D%0A%0D%0A++%A3%A82%A3%A9%D4%CA%D0%ED%B2%E5%C8%EB%B5%C4%D2%BB%B6%CB%B3%C6%CE%AA%B6%D3%CE%B2%A3%A8Rear%A3%A9%A1%A3%0D%0A%0D%0A++%A3%A83%A3%A9%B5%B1%B6%D3%C1%D0%D6%D0%C3%BB%D3%D0%D4%AA%CB%D8%CA%B1%B3%C6%CE%AA%BF%D5%B6%D3%C1%D0%A1%A3%0D%0A%0D%0A++%A3%A84%A3%A9%B6%D3%C1%D0%D2%E0%B3%C6%D7%F7%CF%C8%BD%F8%CF%C8%B3%F6%A3%A8First+In+First+Out%A3%A9%B5%C4%CF%DF%D0%D4%B1%ED%A3%AC%BC%F2%B3%C6%CE%AAFIFO%B1%ED%A1%A3%0D%0A%0D%0A++%CA%B5%CF%D6%B4%FA%C2%EB%A3%BA%0D%0A%0D%0A++package+com.weisou.dataStruct%3B%0D%0A%0D%0A++import+java.util.LinkedList%3B%0D%0A%0D%0A++%2F%2A%2A%0D%0A%0D%0A++%2A%0D%0A%0D%0A++%2A+%40author+gf%0D%0A%0D%0A++%2A+%40date+2009-11-13%0D%0A%0D%0A++%2A%2F%0D%0A%0D%0A++public+class+MyQueue+%7B%0D%0A%0D%0A++LinkedList+linkedList+%3D+new+LinkedList%28%29%3B%0D%0A%0D%0A++%2F%2F%B6%D3%CE%B2%B2%E5%0D%0A%0D%0A++public+void+put%28Object+o%29%7B%0D%0A%0D%0A++linkedList.addLast%28o%29%3B%0D%0A%0D%0A%2F%2F%B6%D3%CD%B7%C8%A1+%C8%A1%CD%EA%B2%A2%C9%BE%B3%FD%0D%0A%0D%0A++public+Object+get%28%29%7B%0D%0A%0D%0A++if%28%21linkedList.isEmpty%28%29%29%0D%0A%0D%0A++return+linkedList.removeFirst%28%29%3B%0D%0A%0D%0A++else%0D%0A%0D%0A++return+%22%22%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++public+boolean+isEmpty%28%29%7B%0D%0A%0D%0A++return+linkedList.isEmpty%28%29%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++public+int+size%28%29%7B%0D%0A%0D%0A++return+linkedList.size%28%29%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++public+void+clear%28%29%7B%0D%0A%0D%0A++linkedList.clear%28%29%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++%2F%2A%2A%0D%0A%0D%0A++%2A+%40param+args%0D%0A%0D%0A++%2A%2F%0D%0A%0D%0A++public+static+void+main%28String%5B%5D+args%29+%7B%0D%0A%0D%0A++MyQueue+myQueue%3D+new+MyQueue%28%29%3B%0D%0A%0D%0A++myQueue.put%281%29%3B%0D%0A%0D%0A++myQueue.put%282%29%3B%0D%0A%0D%0A++myQueue.put%283%29%3B%0D%0A%0D%0A++System.out.println%28myQueue.get%28%29%29%3B%0D%0A%0D%0A++%7D%0D%0A%0D%0A++%7D

网友(2):

你好,代码你可以参考一下:
import java.util.LinkedList;

public class Test {
private LinkedList queue = new LinkedList() ;
public void fallIn(String s){
System.out.println(s + "入队");
queue.add(s) ;

}
public void fallOut(){
int len = queue.size() ;
for(int i=0;i System.out.println( queue.removeFirst() + "出队");
}
}
public void print(){
System.out.print("元素有:") ;
for(String list : queue){
System.out.print(list + " ");
}
}

public static void main(String[] args) {
Test t = new Test() ;
t.fallIn("a") ;
t.fallIn("l") ;
t.fallIn("l") ;
t.fallOut() ;
t.fallIn("i") ;
t.fallOut() ;
t.fallIn("u") ;
t.fallIn("g") ;
t.fallIn("l") ;
t.print() ;
}
}