栈的实现---java

来源:互联网 发布:foxit editor mac 编辑:程序博客网 时间:2024/06/11 09:01
package stack; 23 public interface IStack<E> { 4//1.判断空栈 5public boolean isEmpty(); 67 //2.判断栈满 8public boolean isMax(); 910 //3.入栈11public boolean push(E e);1213 //4.出栈14public E pop();1516 //5.返回栈顶17public E peek();1819 //6.返回元素在栈中的位置20public int getIndex(E e);2122 //7.返回栈的实际长度23public int size();2425 //8.返回栈容量26public int getStackSize();2728 //9.打印栈29public void display();3031 32 33 }
复制代码

//顺序栈

复制代码
  1 package stack;  2   3 //我的栈数据结构  4 public class MyStack<E> implements IStack<E>{  5     private Object[] data = null; //数据域  6     private int top = -1;           //栈顶指针初始化为-1  7     private int maxSize = 0;      //栈最大容量  8       9     //默认设置栈容量为10 10     MyStack(){ 11         this(10); 12     } 13      14     public MyStack(int initialSize){ 15         if(initialSize >= 0){ 16             this.data = new Object[initialSize]; //初始化数组 17             this.maxSize = initialSize; //设置栈最大容量 18             this.top = -1; 19         } 20     } 21      22     public boolean isEmpty() { 23         return top == -1 ? true : false; //根据栈顶值判断,如果栈顶指针没有更新,则为空栈 24     } 25  26     public boolean isMax() { 27         return top >= maxSize-1 ? true : false; //根据栈顶值判断,如果栈顶指针大于最大容量,则为满栈 28     } 29  30     public boolean push(E e) { 31         if(isMax()){ 32             System.err.println("对不起,栈已满,无法入栈"); 33             return false; 34         } 35         top ++; //更新栈顶下标 36         data[top] = e; //将元素添加到表中 37 //        System.out.println("添加" + e + "成功"); 38         return true; 39     } 40  41     @SuppressWarnings("unchecked") 42     public E pop() { 43         if(isEmpty()){ 44             System.err.println("对不起,目前是空栈,没有元素可以出栈"); 45             return null; 46         } 47         E e = (E) data[top]; //返回当前的栈顶元素 48         top--; //更新栈顶 49         return e; 50     } 51  52     @SuppressWarnings("unchecked") 53     public E peek() { 54         if(isEmpty()){ 55             System.err.println("对不起,目前是空栈,无法返回栈顶元素"); 56             return null; 57         } 58         return (E) data[top]; //返回栈顶元素 59     } 60  61     public int getIndex(E e) { 62         //根据栈顶和栈底(-1)遍历栈 63         while(top != -1){ 64             //peek()返回当前栈顶 65             if(peek().equals(e)){ 66                 return top; 67             } 68             top --; 69         } 70          71         return -1; 72     } 73  74     public int size() { 75         return this.top+1; //栈顶值+1,为栈元素的实际个数 76     } 77  78     public int getStackSize() { 79         return this.maxSize; //返回栈实际长度 80     } 81  82     public void display() { 83         //根据栈顶和栈底(-1)遍历 84         while(top != -1){ 85             System.out.println(top); 86             top --; 87         } 88     } 89      90     public static void main(String[] args) { 91         MyStack<Integer> stack = new MyStack<Integer>(); 92         //入栈 93         for (int i = 0; i < 10; i++) { 94             stack.push(i);  95         } 96         //出栈 97 //        stack.pop(); 98         //返回栈顶 99 //        System.out.println(stack.peek());100         //求长101 //        System.out.println(stack.size());102         103     }104     105 }
复制代码


//链栈

复制代码
 1 package stack; 2  3 //链栈是栈的链式存储结构,由多个节点组成。在链栈中的栈顶为头节点,节点由数据域和指针域组成 4 //对链栈的操作都是间接的通过栈顶(头节点)完成。 5 //顺序栈是一种特殊的顺序表 6 //链栈是一种特殊链表 7 public class LinkedStack{ 8     //定义节点类 9     private class Node{10         public Object data = null; //数据域11         public Node next = null;   //指针域12         13         //构造函数初始化14         @SuppressWarnings("unused")15         public Node(){}16         17         public Node(Object data, Node next){18             this.data = data;19             this.next = next;20         }21 22         @Override23         public String toString() {24             return "Node [data=" + data + ", next=" + next + "]";25         }26     }/*Node*/27     28     private Node top = null; //定义栈顶29     private int size = 0;    //定义栈节点数量30     31     //判断栈空32     public boolean isEmpty(){33         return size == 0 ? true : false;34     }35         36     //压栈37     public boolean push(Object obj){38         //更新头节点,让新节点指向原来的头节点39         System.out.println("压栈成功:" + obj + "指向->" + top);40         top = new Node(obj, top); //压栈是将节点插入到栈顶之前。也就是更新头节点。改变指针指向41         size ++; //栈长度++42         return true;43     }44     45     //出栈46     public Object pop(){47         if(isEmpty()){48             System.out.println("对不起,目前是空栈,不能出栈");49             return null;50         }51         Node temp = top;     //头节点引用52         top = top.next;        //更新头节点53         temp.next = null;     //释放引用,删除指针指向54         size-- ;             //栈节点数量-155         return temp.data;   //出栈56     }57     58     //返回栈顶元素,但不弹出栈59     public Object peek(){60         return this.top.data; //直接返回栈顶元素61     }62     63     //遍历栈并打印64     public void display(){65         //从栈顶节点开始到栈底节点null遍历66         while(top != null){67             System.out.println(top.data);68             top = top.next;69         }70     }71     72     //返回元素在栈中的位置73     public int getIndex(Object obj){74         int i = 0;75         while(top != null){76             if(peek().equals(obj)){77                 return i; 78             }79             top = top.next;80             i++;81         }82         return -1;83     }84     85     //返回栈的长度86     public int getSize(){87         return this.size;88     }89     90     public static void main(String[] args) {91         LinkedStack stack = new LinkedStack();92         for (int i = 0; i < 10; i++) {93             stack.push(i);94         }95 //        stack.display();96         System.out.println(stack.getIndex(9));97     }98     99 }
复制代码

明天更新队列和树,然后是图的深度优先,广度优先,各种算法。

 

 

 

 

  

 

 

  

原创粉丝点击