数据结构--(栈)Java实现

,栈的数组实现(Java)

/*

 * 栈的实现

 * */

public class MyStack {

                               //大小

                               private int size;

                               //栈的最大容量

                               private int maxSize;

                               //默认最大容量10

                               private static final int DEFAULT_MAX_SIZE =10;

扫描二维码关注公众号,回复: 2663888 查看本文章

                               //数组(用来保存数据)

                               String[] stack;

                              

                               //有参初始化,设置栈的最大容量

                               public MyStack(int maxSize) {

                                      this.maxSize=maxSize;

                                      stack =new String[maxSize];

                               }

                              

                               //无参初始化,设置栈的默认容量

                               public MyStack() {

                                      this(DEFAULT_MAX_SIZE);

                               }

                              

                               //进行入栈操作

                               public void push(String s) {

                                      if(size >=  maxSize) {

                                              throw new IndexOutOfBoundsException("栈已经满了!");

                                      }

                                      stack[size] =s;

                                      size++;

                                      look();

                               }

                              

                               //出栈操作

                               public String pop() {

                                      String rs=stack[size-1];

                                      //从后往前将做高位的值赋为null

                                      stack[size-1]=null;

                                      size--;

                                      look();

                                      return rs;

                               }

                              

                               //清栈操作

                               public void clear() {

                                      for(int i=0;i<size;i++) {

                                              stack[i] =null;

                                      }

                                      size =0;

                                      look();

                               }

                              

                               //获取栈的长度

                               public int getSize() {

                                      return size;

                               }

                              

                               //动态显示栈中的数据

                               private void look() {

                                      System.out.println("");

                                      for(int i=0;i<maxSize;i++) {

                                              System.out.print(stack[i] + " ");

                                      }

                                        }

                                        }

测试:

 

 

,栈的链表实现(Java)

package com.deng.statck;

 

 

/*

 * 栈的实现

 * */

public class MyStackNode {

                               //大小

                               private int size;

                               //栈的最大容量

                               private int maxSize;

                               //默认最大容量10

                               private static final int DEFAULT_MAX_SIZE =10;

                               //栈顶的节点

                               private Node top;

                              

                               //有参初始化,设置栈的最大容量

                               public MyStackNode(int maxSize) {

                                      this.maxSize=maxSize;

                               }

                              

                               //无参初始化,设置栈的默认容量

                               public MyStackNode() {

                                      this(DEFAULT_MAX_SIZE);

                               }

                              

                               //进行入栈操作

                               public void push(String s) {

                                      if(size >=  maxSize) {

                                              throw new IndexOutOfBoundsException("栈已经满了!");

                                      }

                                      Node node=new Node(s,top);

                                      top=node;

                                      look();

                                      size++;

                               }

                              

                               //移除栈顶的元素

                               public String pop() {

                                      if(top == null)

                                              return null;

                                      Node oldTop=top;

                                      Node newTop=top.next;

                                      String rs=oldTop.s;

                                      oldTop.next=null;

                                      oldTop.s=null;

                                      oldTop=null;

                                      top=newTop;

                                      look();

                                      size--;

                                      return rs;

                               }

                              

                               //清栈操作

                               public void clear() {

                                      Node node=top;

                                      while(node !=null) {

                                              Node newTop=node.next;

                                              node.next=null;

                                              node.s=null;

                                              node =newTop;

                                      }

                                      look();

                                      size =0;

                               }

                              

                               //添加一个Node内部类

                               class Node{

                                      public String s;

                                      public Node next;

                                     

                                      public Node(String s,Node next) {

                                              this.s=s;

                                              this.next=next;

                                      }

                                     

                                      public void setNext(Node next) {

                                              this.next=next;

                                      }

                               }

                              

                               //获取栈的长度

                               public int getSize() {

                                      return size;

                               }

                              

                               //动态显示栈中的数据

                               private void look() {

                                      System.out.println("");

                                      Node node=top;

                                      while(node !=null) {

                                              System.out.print(node.s + " ");

                                               node=node.next;

                                      }

                               }

                               public static void main(String[] args) {

                                      MyStackNode stack=new MyStackNode(4);

                                      //MyStack stack=new MyStack();

                                      stack.push("");

                                      stack.push("");

                                      stack.push("");

                                      stack.push("");

                                      stack.pop();

                                      stack.pop();

                                      stack.push("money");

                                      stack.push("money");

                                      System.out.println("当前栈的容量为:" + stack.getSize());

                                      stack.clear();

                               }

}

测试:

猜你喜欢

转载自blog.csdn.net/fath_kevin/article/details/81452217