zoukankan      html  css  js  c++  java
  • 栈的数组和链表实现(Java实现)

    我以前用JavaScript写过栈和队列,这里初学Java,于是想来实现栈,基于数组和链表。

    下面上代码:

      1 import java.io.*;
      2 //用接口来存放需要的所有操作
      3 interface stack<T>{
      4     boolean isEmpty(); //判空
      5     void clear(); //清空
      6     T pop();   //弹栈
      7     boolean push(T data); //入栈
      8     int length(); //返回长度
      9     T peek();  //查看栈顶值
     10     int search(T t); //查找元素位置
     11     void display(); //输出所有的元素
     12 }
     13 
     14 //用数组实现栈
     15 class ArrayStack<T> implements stack<T>{
     16     public ArrayStack(){}
     17     private T[] array = (T[])new Object[16];
     18     private int size = 0; 
     19     
     20     public int length(){
     21         for(int i=0;i<array.length;i++){
     22             if(array[i] != null) size++;
     23         }
     24         return size;
     25     }
     26     
     27     public boolean isEmpty(){
     28         return (size == 0);
     29     }
     30     
     31     public void clear(){
     32         for(int i=0;i<array.length;i++){
     33             array[i] = null;
     34         }
     35         size = 0;
     36     }
     37     
     38     public T pop(){
     39         if(size == 0) return null;
     40         else{
     41             T temp = array[size-1];
     42             array[size-1] = null;
     43             size--;
     44             return temp;
     45         }
     46     }
     47     
     48     public boolean push(T data){
     49         if(size >= array.length) {
     50             resize(); //重新分配一个两倍大小的数组
     51             array[size++] = data;
     52         }
     53         else{
     54             array[size++] = data;
     55         }
     56         return true;
     57     }
     58     
     59     public void resize(){
     60         T[] temp = (T[])new Object[array.length*2];
     61         for(int i=0;i<array.length;i++){
     62             temp[i] = array[i];
     63         }
     64         for(int i=array.length;i<array.length*2;i++){
     65             temp[i] = null; 
     66         }
     67         array = temp;
     68         temp = null;
     69     }
     70     
     71     public T peek(){
     72         if(size == 0) return null;
     73         else return array[size-1];
     74     }
     75     
     76     public int search(T t){
     77         for(int i=0;i<size;i++){
     78             if(array[i] == t) return i+1;
     79         }
     80         return 0;
     81     }
     82     
     83     public void display(){
     84         for(int i=0;i<size;i++){
     85             System.out.println("data: " + array[i]);
     86         }
     87     }
     88     
     89 }
     90 
     91 //用链表实现栈
     92 
     93 class LinkStack<T> implements stack<T>{
     94     public LinkStack(){
     95         this.top = null;
     96         this.size = 0;
     97     }
     98     //存放数据的结点
     99     class Node{
    100         private T data;
    101         private Node pre;
    102     }
    103     private Node top; //栈顶指针
    104     private int size; //栈的大小
    105     
    106     public boolean isEmpty(){
    107         if(size == 0) return true;
    108         else return false;
    109     }
    110     
    111     
    112     
    113     public void clear(){
    114         top = null;
    115         size = 0;
    116     }
    117     
    118     
    119     public T pop(){
    120         if(top != null){
    121             T temp = top.data;
    122             top = top.pre;
    123             size--;
    124             return temp;
    125         }
    126         return null;
    127     }
    128     
    129     
    130     public boolean push(T data){
    131         Node node = new Node();
    132         node.data = data;
    133         node.pre = null;
    134         if(top == null){
    135             top = node;
    136             node = null;
    137             size++;
    138             return true;
    139         }
    140         else{
    141             node.pre = top;
    142             top = node;
    143             node = null;
    144             size++;
    145             return true;
    146         }
    147     }
    148     
    149     
    150     public int length(){
    151         return size;
    152     }
    153     
    154     public T peek(){
    155         return top.data;
    156     }
    157     
    158     public int search(T t){
    159         int num = size;
    160         while(top.pre != null)
    161         {
    162             if(top.data == t)
    163                 return (num-1);
    164             else
    165             {
    166                 top = top.pre;
    167                 num--;
    168             }
    169         }
    170         return 0;
    171     }
    172     
    173     public void display(){
    174         Node node = top;
    175         while(top != null)
    176         {
    177             System.out.println("data: " + top.data);
    178             top = top.pre;
    179         }
    180         top = node;
    181         node = null;
    182     }
    183         
    184 }
    185 
    186 
    187 public class test{
    188     public static void main(String[] args){
    189         ArrayStack<String> a = new ArrayStack();
    190         a.push("hello,world...");
    191         a.push("my name is: ");
    192         a.push("jeavenwong");
    193         a.display();
    194         
    195         LinkStack<String> b = new LinkStack();
    196         b.push("how are you?");
    197         b.push("i am fine...");
    198         b.push("and you?...");
    199         b.display();
    200     }
    201 }

    下面是我的运行结果:

    如有不对,欢迎批评指正。

  • 相关阅读:
    vue_ajax-axios的使用
    laravel验证码扩展包gregwar/captcha的使用
    如何让你的网页变为黑白色
    laravle Str::random(num)函数
    laravel_日志查看-logViewer工具的使用
    如何自定义css的鼠标样式
    公鸡3块钱1只,母鸡5块钱1只,小鸡1块钱3只,用100块买100只鸡,一共多少种买法,分别是什么?
    假设某人有100000现金。 每经过一次路口需要进行一次交费。 交通规则为为当他现金大于50000时每次需要交5%如果现金小于等于50000时每次交5000。 请写一程序计算此人可以经过多少次这个路口
    本周总结
    本周总结
  • 原文地址:https://www.cnblogs.com/jeavenwong/p/8087259.html
Copyright © 2011-2022 走看看