Recently in the review of the Java Foundation, looked at the Java Foundation, in the see the collection when suddenly found that the interview has a written question: simulation of a stack or queue data structure, was not done, today to write, the first to understand the stack and the data structure of the queue
Stack: Advanced later out
Queue: Advanced First Out
LinkedList is just the AddFirst (), AddLast () method, now write a simple tool class package under the OH
public class Demo4 {public
static void Main (string[] args) {
tools tools = new tools ();
Tools.add ("a");
Tools.add ("B");
Tools.add ("C");
Tools.add ("D");
Tools.romove ();
}
Class tools{
private linkedlist<string> ll;
Public Tools () {
ll = new linkedlist<string> ();
}
public void Add (String str) {
ll.addfirst (str);
}
public void Romove () {for
(int i=0;i<ll.size (); i++) {
String content = Ll.get (i);
SYSTEM.OUT.PRINTLN (content);}}
Print results:
D
C
B
A
This is the advanced, this is the data structure of the stack, the queue is simple, as long as the AddFirst () method to AddLast () method on the line
The code is as follows:
public class Demo4 {public
static void Main (string[] args) {
tools tools = new tools ();
Tools.add ("a");
Tools.add ("B");
Tools.add ("C");
Tools.add ("D");
Tools.romove ();
}
Class tools{
private linkedlist<string> ll;
Public Tools () {
ll = new linkedlist<string> ();
}
public void Add (String str) {
ll.addlast (str);
}
public void Romove () {for
(int i=0;i<ll.size (); i++) {
String content = Ll.get (i);
SYSTEM.OUT.PRINTLN (content);}}
The result is not printed,