Stacks and queues:
It is generally used as a programmer's tool to assist the idea algorithm, the life cycle is shorter, the runtime is created;
Access is restricted, at a particular time, only one data can be read or deleted;
is an abstract structure, the internal implementation mechanism, not visible to users, such as the use of arrays, linked lists to implement the stack.
Analog stack structure
at the same time, only one data is allowed to be accessed, LIFO first out
For the stack and the time complexity of the Stack is O (1), that is, do not rely on the number of data items in the stack, the operation is relatively fast
example, using arrays as storage structures for stacks
public class Stacks<t> {private int max;
Private t[] ary; private int top;
Pointer to the subscript public StackS (int size) {This.max = size, pointing to the top element of the stack;
ary = (t[]) new Object[max];
top =-1;
}//into stack public void push (T data) {if (!isfull ()) ary[++top] = data;
}//out Stack public T pop () {if (IsEmpty ()) {return null;
return ary[top--];
//view stack Top Public T peek () {return ary[top];
}//Stack is null public boolean IsEmpty () {return top = = 1;
}//Stack is full public boolean isfull () {return top = = Max-1;
//size public int size () {return top + 1;
public static void Main (string[] args) {stacks<integer> stack = new stacks<integer> (3);
for (int i = 0; i < 5; i++) {Stack.push (i);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ()); for (int i = 0; i < 5; i++) {Integer peek = Stack.peek ();
System.out.println ("Peek:" + peek);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
for (int i = 0; i < 5; i++) {Integer pop = Stack.pop ();
System.out.println ("Pop:" + Pop);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
} System.out.println ("----");
for (int i = 5; i > 0; i--) {Stack.push (i);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
for (int i = 5; i > 0; i--) {Integer peek = Stack.peek ();
System.out.println ("Peek:" + peek);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
for (int i = 5; i > 0; i--) {Integer pop = Stack.pop ();
System.out.println ("Pop:" + Pop);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
}
}
}
The above example, there is a maxsize rule, because the array is to specify the size, if you want to unrestricted, you can use other structures to do storage, of course, you can also new an array of length.
example, using LinkedList storage to implement stacks
public class Stackss<t> {private linkedlist<t> datas;
Public Stackss () {datas = new linkedlist<t> ();
}//into stack public void push (T data) {datas.addlast (data);
}//out Stack public T pop () {return datas.removelast ();
//view stack Top Public T peek () {return datas.getlast ();
}//Stack is null public boolean IsEmpty () {return datas.isempty ();
//size public int size () {return datas.size ();
public static void Main (string[] args) {stacks<integer> stack = new stacks<integer> (3);
for (int i = 0; i < 5; i++) {Stack.push (i);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
for (int i = 0; i < 5; i++) {Integer peek = Stack.peek ();
System.out.println ("Peek:" + peek);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
for (int i = 0; i < 5; i++) {Integer pop = Stack.pop (); System.out.pRintln ("Pop:" + Pop);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
} System.out.println ("----");
for (int i = 5; i > 0; i--) {Stack.push (i);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
for (int i = 5; i > 0; i--) {Integer peek = Stack.peek ();
System.out.println ("Peek:" + peek);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
for (int i = 5; i > 0; i--) {Integer pop = Stack.pop ();
System.out.println ("Pop:" + Pop);
SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());
}
}
}
example, the word reverse, using the STATCK structure
public class Wordreverse {public
static void Main (string[] args) {
reverse ("Corporation");
}
static void reverse (String word) {
if (word = = null) return;
stackss<character> stack = new stackss<character> ();
char[] Chararray = Word.tochararray ();
int len = chararray.length;
for (int i = 0; I <len; i++) {
stack.push (chararray[i]);
}
StringBuilder sb = new StringBuilder ();
while (!stack.isempty ()) {
sb.append (Stack.pop ());
}
System.out.println ("Reverse:" + sb.tostring ());
}
Print:
After inversion: social type strains
simulate queues (general queues, two-terminal queues, priority queues)
queues:
Advanced first out, dealing with problems like queues, first row, first treatment, back row, etc. before the process is finished, then deal with
The time complexity for the insert and remove Operations is O (1), inserted from the back, and the
two-terminal queue is removed from the front:
that is, you can insert and remove:insertleft at both ends of the queue, Insertright,removeleft, Removeright
contains stack and queue functions, such as the removal of Insertleft, Removeleft, which is the same as the stack; if you get rid of Insertleft, removeright, it's the same as the queue.
General frequency of use is low, time complexity O (1)
Priority queue:
internally maintains a sequence sorted by priority. When inserting, you need to compare the location of the lookup insertion, time complexity O (N), delete O (1)
* * The queue is advanced first out, a pointer indicates the position of the insertion, a pointer indicates the location of the data item/public class Queueq<t> {private int max;
Private t[] ary; private int front; Team Head pointer indicates the location of the data item to be removed private int rear; The team tail pointer indicates the position of the inserted private int nitems;
The number of actual data items public queueq (int size) {This.max = size;
ary = (t[]) new Object[max];
Front = 0;
Rear =-1;
Nitems = 0;
//Insert Team tail public void insert (T t) {if (rear = max-1) {//has been to the actual team tail, start from scratch rear =-1;
} Ary[++rear] = t;
nitems++;
//Remove Team Head public T remove () {T temp = ary[front++];
if (front = max) {//queued to the end, start from scratch front = 0;
} nitems--;
return temp;
//View Team head public T-Peek () {return ary[front];
public Boolean IsEmpty () {return nitems = 0;
public Boolean isfull () {return nitems = = max;
public int size () {return nitems; public static void Main (string[] args) {queueq<integer> queUE = new queueq<integer> (3);
for (int i = 0; i < 5; i++) {Queue.insert (i);
SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());
for (int i = 0; i < 5; i++) {Integer peek = Queue.peek ();
System.out.println ("Peek:" + peek);
SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());
for (int i = 0; i < 5; i++) {Integer remove = Queue.remove ();
System.out.println ("Remove:" + Remove);
SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());
} System.out.println ("----");
for (int i = 5; i > 0; i--) {Queue.insert (i);
SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());
for (int i = 5; i > 0; i--) {Integer peek = Queue.peek ();
System.out.println ("Peek:" + peek);
SYSTEM.OUT.PRINTLN ("Size:" + queue.size ());
for (int i = 5; i > 0; i--) {Integer remove = Queue.remove ();
System.out.println ("Remove:" + Remove); System.out.prinTLN ("Size:" + queue.size ());
}
}
}
* * * Dual-end queue <span style= "White-space:pre" > </span> Insert, delete/public class
queueqt<t> {private linkedlist<t> list;
Public queueqt () {list = new linkedlist<t> ();
}//Insert team Head public void Insertleft (t) {List.addfirst (t);
///Insert Team tail public void Insertright (t) {list.addlast (t);
//Remove Team Head public T Removeleft () {return List.removefirst ();
//Remove Team Tail public T removeright () {return list.removelast ();
//View Team Head public T Peekleft () {return List.getfirst ();
//View Team Tail public T peekright () {return list.getlast ();
public Boolean IsEmpty () {return list.isempty ();
public int size () {return list.size (); }
}
* * Priority Queue queue in priority order, is an ordered queue/public class QUEUEQP {private int max;
Private int[] ary; private int nitems;
The number of actual data items public queueqp (int size) {This.max = size;
ary = new Int[max];
Nitems = 0;
///Insert team tail public void insert (int t) {int J;
if (Nitems = = 0) {ary[nitems++] = t; else {for (j = nItems-1 J >= 0; j--) {if (T > ary[j]) {ary[j + 1] = ary[j];//Previous
Assigned to the latter small after the equivalent of the insertion of a sort, the given sequence is originally ordered, so the efficiency O (N)} else {break;
} ary[j + 1] = t;
nitems++;
} System.out.println (arrays.tostring (ary));
///Remove team Head public int remove () {return ary[--nitems];//Remove priority}//view team tail lowest priority public int peekmin () {
return ary[nitems-1];
public Boolean IsEmpty () {return nitems = 0;
public Boolean isfull () {return nitems = = max; public int size () {return nitems;
public static void Main (string[] args) {QUEUEQP queue = new QUEUEQP (3);
Queue.insert (1);
Queue.insert (2);
Queue.insert (3);
int remove = Queue.remove ();
System.out.println ("Remove:" + Remove);
}
}