In the process of learning sometimes encounter a data structure is the stack, here to do a detailed understanding of the stack. In the process of perfecting this blog, I mainly refer to these two posts: Http://www.cnblogs.com/QG-whz/p/5170418.html#_label1_1 and http://blog.csdn.net/javazejian/ article/details/53362993 here to indicate the source, the code involved in the Java implementation of my. The first modification date is the 2017.8.02 afternoon stack profile stack features

Stacks (stack) is a linear storage structure, it can also be called a special linear table with limited operation, and its insert and delete operations are allowed only at one end of the linear table, in general, the one end of the allowable operation is called the top of the stack, and the Bottom end is called the Stack Bottom (a). The top of the stack is the end of the linear sequence.

The summary has the following two features:

1, the data elements in the stack adhere to the principle of "LIFO" (last in-out first), referred to as the LIFO structure.

2, the limit can only be inserted on the top of the stack and delete operations. Stack of related concepts

1, the top of the stack and the bottom of the stack: Allow elements to insert and delete the end of the stack is called the top, the other end is called the Stack Bottom (BOTTOM).

2, press stack: Stack insert operation, called into the stack, also known as pressure stack, into the stack (PUSH).

3, stack: Stack delete operation, also called Make stack (POP). Basic operations of Stacks

1, into the stack (push)

In the process of pressing the stack, the top position of the stack has been moving upward, while the bottom of the stack is fixed.

2, out stack (POP)

The order of the stack is 3, 2, 1, and the order is the opposite of the stack, which is called "back-first out".

In the stack process, the stack top position has been "downward" movement, and the bottom of the stack has remained unchanged.

3, to find the size of the stack

4, to determine whether the stack is empty (isempty)

5, get the top of the stack of the value of the stack of the implementation of Java in the Stack class use

/** * Stack class * stacks: bucket or box data type, LIFO, relative heap heap to two fork tree type, can quickly locate and operate * STACK<E>, support generics * Public CL Ass stack<e> extends vector<e> * Stack method call vector method, which is synchronized decorated for thread safety (Vector also) * Stack meth ODS: * Push: Press the item onto the top of the stack and return the object as the value of this function * Pop: Removes the object at the top of the stack and returns the object as the value of this function * Peek: View the object at the top of the stack, and return the object as the value of this function , but does not remove it from the stack * Empty: Test stack is empty * search: Returns the position of the object in the stack, with a base of 1 * Size: Returns the size of the stack */

Package ca.map;
Import Java.util.Stack;
public class Stackx {public static void main (string[] args) {Stackmethod ();
//stack operate public static void Stackmethod () {//define a stack of integer generics
stack<integer> stack = new stack<integer> (); System.out.println ("is the new stack stack empty:" + (Stack.empty ()?)
Null ": Stack.size ()));
Push: Pushes the item onto the top of the stack, returning the type specified by the value Generic//Here pushes 1 to 5 into the stack Stack.push (1);
Stack.push (2);
Stack.push (3);
Stack.push (4);
Stack.push (5);
System.out.println ("1 to 5 in order into the stack after the": "+stack); Empty: Test stack is empty, size () = = 0, return value boolean System.out.println ("Stack is null for 1~5:" + (Stack.empty ()?)
Null ": Stack.size ()));
Search: Returns the position of the object in the stack, in 1 radix, parameter: Search (Object o), the return value int int ostack = Stack.search (3); System. OUT.PRINTLN ("Find the location of object 3 in stack stack elementid:" +ostack);
Peek: View the object at the top of the stack, but not remove it from the stack, return the type int topelement =stack.peek () specified by the value generic;
System.out.println ("View stack top element as:" +topelement);
SYSTEM.OUT.PRINTLN ("Peek Operation Stack is:" +stack);
Pop: Removes the object at the top of the stack and returns the object as the value of this function, returning the type int oremove = Stack.pop () specified by the value generic type.
SYSTEM.OUT.PRINTLN ("The element that removes the top of the stack stack is:" +oremove);
System.out.println ("After the pop operation removes the stack top element:" +stack); }
}

Test results

Whether the new stack stack is empty: Empty
1 to 5 into the stack in order: [1, 2, 3, 4, 5]
value is 1~5 stack is empty: 5
Find the location of object 3 in stack stacks ElementID: 3
View stack top elements are: 5
peek Operation Stack is: [1, 2, 3, 4, 5]
the elements that remove the top of the stack stack are: 5
pop operations remove stack top elements: [1, 2, 3, 4]

If the collection is empty, return []

If the set is not empty [plus the iteration element Plus, the last set has no elements plus] eg:[1, 2, 3, 4]

Stacks are implemented in two ways according to the storage structure. Array implementation

Stack creation and common method code:

Package stack;
Import java.util.EmptyStackException; public class Mystackarray<t> {public t[] array = null;//storage element's array public int initalsize = 5;//Stack's capacity size, default to Ten public int top =-1;
The top of the stack pointer,-1 indicates an empty stack because the array subscript starts at 0. public int cursize;
Indicates how large the current actual capacity of the stack is public mystackarray () {this (5);
@SuppressWarnings ("unchecked") public mystackarray (int Size) {//constructor, for initializing stack size if (size >= 0) { This.initalsize= Size; The capacity of the stack is the value array = (t[]) new object[size]; The value of the size of the stack element array is also top =-1;
If the top of the stack points to-1, indicates an empty stack, cursize = 0;
else {throw new RuntimeException ("initialization size cannot be less than 0:" + size);
}//Determine if the stack is empty public boolean isempty () {return top = 1 true:false;//If the stack pointer points to-1, indicates empty stack}
Capacity of the expansion stack @SuppressWarnings ("unchecked") public void expandcapacity () {//If the capacity needed to expand is smaller than the current array capacity, no expansion is required
T[] old = array; Array = (t[]) neW object[cursize * 2 + 1];
Initalsize = cursize * 2 + 1;
copy element for (int i = 0; i < top; i++) array[i] = Old[i]; ///stack, first determine whether the stack is full of public void push (T data) {if (top = initalSize-1) {expandcapacity (); If the stack is full, expand capacity}//add element array[++top from top of stack] = data;
It's not full. The next element that top points to is the element, and the array expands cursize++;
}//out stack, first to determine whether it is empty stack, for NULL report exception Public T pop () {if (IsEmpty ()) new Emptystackexception ();
cursize--; return array[top--];
Returns the element to be deleted}//view top element of stack without removing public T peek () {if (IsEmpty ()) new Emptystackexception ();
return array[top];
//Returns the position of the object on the stack, with 1 cardinality public int search (T data) {int i = top;
while (top!=-1) {if (Peek ()!= data) {top--;
}else{break;
int result = Top+1; top = i; Top returnStack top position return result;
}
}

Test code:

package stack;
public class Stack {public
static void Main (string[] args) {
mystackarray<integer> s = new mystackarray< Integer> ();
S.push (1);
S.push (5);
S.push (3);
S.push (4);
System.out.println ("Stack capacity" + s.initalsize);
System.out.println ("Stack top element" +s.peek ());
System.out.println ("Position of the search element" +s.search (5));
System.out.println ("Out stack element is" +s.pop ());
System.out.println ("Out stack once after stack top" +s.peek ());
S.push (8);
S.push (9);
System.out.println ("Stack top element" +s.peek ());
System.out.println ("Stack capacity" + s.initalsize);
S.push (ten);
System.out.println ("Stack top element" +s.peek ());
SYSTEM.OUT.PRINTLN ("Capacity of stack" + s.initalsize);
}

Test results

Stack capacity 5
stack top element 4
search element position 2
out stack element is 4 out stack
once after stack top 3 stack
top element 9 stack
capacity 5 //note, in this place due to the increased element exceeding the total capacity, So
the capacity of the stack top element is automatically enlarged 11

Single Linked list implementation

Linked List Class Code

package stack;
public class Node<t> {public
T val;
Public node<t> next = null;
Public node (T Val, node<t> next) { //constructor method, create a new node
this.val = val;
This.next = next;
}

Stack code consisting of a single linked list

Package stack;
public class Mystacklist<t> {public node<t> top-level;//stack top element public int cursize;
Determine if the stack is empty public boolean isempty () {return cursize = = 0? true:false; //Stack, first determine if stack is full public void push (T val) {if (val = = null) {System.out.println ("new node data cannot
Is null "); } if (top = null) {top = new node<t> (val, null);//If the current is an empty stack, no node, then the incoming node is the first node} else if (Top.val = null) {top.val = val;//If the current top is not NULL, but the data in it is null, then just assign the current data to the stack and it is ok} else {node<t> newn
Ode = new Node<t> (val, top); top = NewNode;
Update stack top} cursize++;
}//out stack, first to determine whether it is empty stack, for NULL report exception Public T pop () {if (IsEmpty ()) {System.out.println ("stack is empty");
} T olddata = Top.val;
top = Top.next;
cursize--;
return olddata; //view top of stack element without removing public T peek () {if (IsEMpty ()) {System.out.println ("stack is empty");
return top.val;
//Returns the position of the object on the stack, with 1 cardinality public int search (T data) {int count = Cursize;
node<t> temp = top;
while (temp!= null) {if (Temp.val = = data) return count;
count--;
temp = Temp.next;
} return-100000;
}
}

Test code

package stack;
public class Liststack {public
static void Main (string[] args) {
mystacklist<integer> s = new MYSTACKLIST&L T;integer> ();
S.push (1);
S.push (5);
S.push (3);
S.push (3);
System.out.println ("Stack capacity" + s.cursize);
S.pop ();
System.out.println ("Stack capacity" + s.cursize);
System.out.println ("Stack top element" +s.peek ());
System.out.println ("Position of the search element" +s.search (1));
System.out.println ("Out stack element is" +s.pop ());
System.out.println ("Out stack two times after stack top" +s.peek ());
S.push (8);
S.push (9);
System.out.println ("Stack top element" +s.peek ());
System.out.println ("Stack capacity" + s.cursize);
S.push (ten);
System.out.println ("Stack top element" +s.peek ());
SYSTEM.OUT.PRINTLN ("Capacity of stack" + s.cursize);
}

Test results

Stack capacity 4
stack capacity 3
stack top element 3
search element position 1
out stack element is 3
out stack two times after stack top 5 stack top element 9 stack
of capacity 4 stack
top element
Capacity of the Stack 5

Comparison of time complexity of two implementations

Sequential stack complexity is as follows:

The complexity of the chain stack is as follows:

So the main operation of the stack can be completed in constant time, mainly because the stack is only one end of the operation, and the operation is only the top of the stack element. LinkedList implementation Stack

Import java.util.LinkedList;
/**
* Based on the LinkedList implementation stack
* in the LinkedList power only select part of the implementation of the stack-based interface * * Public
class Stacklist<e> {
Private linkedlist<e> LL = new linkedlist<e> ();
into stack public
void push (E e) {
ll.addfirst (e);
}
View the top element of the stack without removing public
E Peek () {return
ll.getfirst ();
}
Out Stack public
E POPs () {return
ll.removefirst ();
}
Null-
Empty public boolean () {return
ll.isempty ();
}
Print stack element public
String toString () {return
ll.tostring ();
}
}