Stack and queue are two kinds of special linear tables, their logical structure and linear table are the same, but their operation rules are more limited than the linear table, so they call them the linear table with limited operation.
LinkedList data structure is a two-way chain structure, each object in addition to the data itself, there are two references, respectively, pointing to the previous element and the latter element, and array of sequential storage structure (such as: ArrayList) compared to the insertion and deletion more convenient, but slower.
The definition of the stack
Stack is a linear table that restricts inserting and deleting operations at one end of a table.
(1) It is commonly said that the insertion and deletion of this end is the top of the stack, the other end is called the Stack Bottom (Bottom).
(2) when there is no element in the table, it is called an empty stack.
(3) The stack is a linear table of LIFO (last in first), abbreviated as a LIFO table.
The stack changes are based on the LIFO principle. Every time the removal (stack) of the current stack is always the "latest" element, that is, the last inserted (into the stack) of the elements, and the first inserted is placed at the bottom of the stack, to the last to be deleted.
Implementation code:
Package com.gc.list;
Import java.util.*;
public class Mystack {
private linkedlist ll=new linkedlist ();
public void push (Object o) {
ll.addfirst (o);
}
Public Object POPs () {return
ll.removefirst ();
}
Public Object Peek () {return
ll.getfirst ();
}
public Boolean empty () {return
ll.isempty ();
}
public static void Main (string[] args) {
mystack ms=new mystack ();
Ms.push ("Zhangsan");
Ms.push ("Lisi");
Ms.push ("Wangwu");
System.out.println (Ms.pop ());
System.out.println (Ms.peek ());
System.out.println (Ms.pop ());
System.out.println (Ms.empty ());
}
Queue definition
Queues are linear tables that allow only inserts at one end and restricted operations at the other end
(1) The end of the allowed deletion is called the team Head (Front).
(2) The one end allowed to be inserted is called the team tail (rear).
(3) When there is no element in the queue, it is called an empty queue.
(4) The queue is also referred to as a linear table of FIFO (first-in-out).
Use the Removefirst () method to return the first data in the queue and then remove it from the queue
Implementation code:
Package com.gc.list;
Import java.util.*;
public class Myqueue {
private linkedlist ll=new linkedlist ();
public void put (Object o) {
ll.addlast (o);
}
Use the Removefirst () method to return the first data in the queue, and then remove it from the queue with public
Object get () {return
ll.removefirst ();
}
public Boolean empty () {return
ll.isempty ();
}
public static void Main (string[] args) {
myqueue mq=new myqueue ();
Mq.put ("Zhangsan");
Mq.put ("Lisi");
Mq.put ("Wangwu");
System.out.println (Mq.get ());
System.out.println (Mq.get ());
System.out.println (Mq.get ());
System.out.println (Mq.empty ());
}