public class MyStack <T>{
private int max_len;//栈能存放的最大节点数量
private int len;//现在栈存放的节点的数量
private StackNode<T> head;//栈顶节点
public MyStack() {
max_len = 100;
len = 0;
head = null;
}
public MyStack(int max){
max_len = max;
len = 0;
head = null;
}
}
节点类
package book_1_3;
public class StackNode <T> {
private StackNode pre_stackNode;
private StackNode nex_StackNode;
private T data;
public StackNode(T d) {
pre_stackNode = null;
data = d;
}
public StackNode getPre_stackNode() {
return pre_stackNode;
}
public void setPre_stackNode(StackNode pre_stackNode) {
this.pre_stackNode = pre_stackNode;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
public boolean is_fill(){
if(len < max_len && len >= 0 && max_len >= 0){
return false;
}
return true;
}
获取栈顶元素不出栈
public T peek(){
//判空
if(is_empty()){
System.out.println("当前栈为空");
return null;
}
return head.getData();
}
获取栈顶元素并出栈
public T pop(){
//判空
if(is_empty()){
System.out.println("当前栈为空");
return null;
}
T res = head.getData();
head = head.getPre_stackNode();
len --;
return res;
}
将元素入栈
public boolean push(T data){
//判满
if(is_fill()){
System.out.println("当前栈已满");
return false;
}
StackNode<T> newStackNode = new StackNode<T>(data);
newStackNode.setPre_stackNode(head);
head = newStackNode;
len++;
return true;
}
获取在栈中与当前数据相同的第一个元素的索引
public int get_dataIndex(T data){
//判空
if(is_empty()){
System.out.println("当前栈为空");
return -1;
}
StackNode<T> v = head;
int index = 1;
int last = -1;
while(v != null){
if(v.getData().toString().equals(data.toString())){
last = index;
}
index++;
v = v.getPre_stackNode();
}
if(last == -1){
return last;
}
return len - last + 1;
}
将栈清空
public void clear(){
head = null;
len = 0;
}
打印栈内所有元素
public void prinStack(){
StackNode v = head;
while(v != null){
System.out.println(v.getData().toString());
v = v.getPre_stackNode();
}
}
完整代码
package book_1_3;
//节点类
public class StackNode <T> {
private StackNode pre_stackNode;
private StackNode nex_StackNode;
private T data;
public StackNode(T d) {
pre_stackNode = null;
data = d;
}
public StackNode getPre_stackNode() {
return pre_stackNode;
}
public void setPre_stackNode(StackNode pre_stackNode) {
this.pre_stackNode = pre_stackNode;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
//栈类
public class MyStack <T>{
private int max_len;//栈能存放的最大节点数量
private int len;//现在栈存放的节点的数量
private StackNode<T> head;//栈顶节点
public MyStack() {
max_len = 100;
len = 0;
head = null;
}
public MyStack(int max){
max_len = max;
len = 0;
head = null;
}
//获取栈内元素的个数
public int get_eleNum() {
return len;
}
//判断当前栈是否为空
public boolean is_empty(){
if(len == 0){
return true;
}
return false;
}
//判断当前栈是否已满
public boolean is_fill(){
if(len < max_len && len >= 0 && max_len >= 0){
return false;
}
return true;
}
//获取栈顶元素不出栈
public T peek(){
//判空
if(is_empty()){
System.out.println("当前栈为空");
return null;
}
return head.getData();
}
//获取栈顶元素并出栈
public T pop(){
T res = peek();
head = head.getPre_stackNode();
len --;
return res;
}
//将元32.2素入栈
public boolean push(T data){
//判满
if(is_fill()){
System.out.println("当前栈已满");
return false;
}
StackNode<T> newStackNode = new StackNode<T>(data);
newStackNode.setPre_stackNode(head);
head = newStackNode;
len++;
return true;
}
//获取在栈中与当前数据相同的第一个元素的索引
public int get_dataIndex(T data){
//判空
if(is_empty()){
System.out.println("当前栈为空");
return -1;
}
StackNode<T> v = head;
int index = 1;
int last = -1;
while(v != null){
if(v.getData().toString().equals(data.toString())){
last = index;
}
index++;
v = v.getPre_stackNode();
}
if(last == -1){
return last;
}
return len - last + 1;
}
//将栈清空
public void clear(){
head = null;
len = 0;
}
//打印栈内所有元素
public void prinStack(){
StackNode v = head;
while(v != null){
System.out.println(v.getData().toString());
v = v.getPre_stackNode();
}
}
}