One, stack
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. Always delete (back stack) in the current stack
The newest element, the last element to insert (into the stack), and the first to be placed at the bottom of the stack,
You will not be able to delete until the last.
2, the basic operation of the stack
(1) To determine whether the stack is empty
Boolean IsEmpty ();
(2) Empty stack
void Clear ();
(3) Length of stack
int length ();
(4) data into the stack
Boolean push (T data);
(5) data out of the stack, the stack to delete
T pop ();
(6) Data out of the stack, the stack does not delete
T Peek ();
Second, code writing
1. Interface class
Package com.lin.stack;
/**
* Feature Summary: Stack interface class
*
* @author Linbingwen
* @since August 29, 2015/public
interface Mystack <T> {
/**
* Determine if stack is empty
* @author Linbingwen
* @since August 29, 2015
* @return
* *
Boolean IsEmpty ();
/** *
Empty stack
* @author Linbingwen
* @since August 29, 2015
/Void Clear ();
/**
* Stack length
* @author Linbingwen
* @since August 29, 2015
* @return
/int length ();
/** *
data into stack
* @author Linbingwen
* @since August 29, 2015
* @param data
* @return
* *
Boolean push (T data);
/**
* Data out stack, delete in stack
* @author Linbingwen
* @since August 29, 2015
* @return
/T pop ();
/**
* Data out stack, the stack does not delete
* @author Linbingwen
* @since August 29, 2015
* @return * * * *
peek (); c56/>}
2. Array implementation stack
Package com.lin.stack;
/**
* Feature Overview: Array implementation Stack
*
* @author Linbingwen
* @since August 29, 2015/public
class Myarraystack <T> implements mystack<t> {
private object[] Objs = new OBJECT[16];
private int size;
@Override Public
Boolean IsEmpty () {return
size = = 0;
}
@Override public
void Clear () {for
(int i = 0; i < objs.length; i++) {
objs[i] = null;
size--
}
}
@Override public
int Length () {return
size;
}
@Override public
Boolean push (T data) {
if (size = = objs.length-1) {
object[] temp = new object[ OBJS.LENGTH*2];
for (int i = 0; i < objs.length; i++) {
temp[i]=objs[i];
}
objs= temp.
Objs[size++]=data;
return true;
}
@Override
@SuppressWarnings ("unchecked") Public
T POPs () {return
size = = 0?null: (T) objs[(size--) -1];< c41/>}
@Override
@SuppressWarnings ("unchecked") public
T-Peek () {return
size = = 0?null: (T) objs[ Size];
}
3, the Chain list realization stack
Package com.lin.stack;
/**
* Feature Summary:
* *
@author linbingwen
* @since August 29, 2015/public
class Myliststack<t > Implements mystack<t>{
private int size;
Private Node top;
Class node{
T data;
Node pre;
}
@Override Public
Boolean IsEmpty () {return
size = = 0;
}
@Override public
void Clear () {top
= null;
}
@Override public
int Length () {return
size;
}
@Override public
Boolean push (T data) {
node node = new Node ();
Node.data=data;
if (null = top) {Top
= node;
} else {
node.pre = top;
Top =node;
}
size++;
return true;
}
@Override public
T pop () {
if (size = 0) {return
null;
}
T data = Top.data;
top = Top.pre;
size--;
return data;
}
@Override Public
T Peek () {
if (size = = 0) {return
null;
}
T data = Top.data;
return data;
}
4, testing
Package com.lin.stack;
/**
* Function Summary:
* *
@author linbingwen
* @since August 29, 2015/public
class Stacktest {
/* * *
@author linbingwen
* @since August 29, 2015
* @param args
/public static void main ( String[] args {
mystack<integer> myStack1 = new myarraystack<integer> ();
mystack<integer> MyStack2 = new myliststack<integer> ();
for (int i =0;i<30;i++) {
mystack1.push (i);
Mystack2.push (i);
}
System.out.println ("The stack output of the array implementation is as follows");
for (int J =0;j<30;j++) {
System.out.print (Mystack1.pop () + " ");
}
System.out.println ();
System.out.println ("The stack output of the linked list is as follows");
for (int k =0;k<30;k++) {
System.out.print (Mystack2.pop () + " ");
}
}
}
The output results are as follows:
Or look here:
The stack output for the
array implementation is the following
28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0;
The stack output of the list implementation is as follows
28 27 26 p;24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0;