用数组实现堆栈和队列

  1. package com.sms;
  2. /**
  3.  * 
  4.  * @author Administrator
  5.  *
  6.  */
  7. public class Stack {
  8.    public static  int MAX_SIZE=10;
  9.    private Object[] stack = new Object[MAX_SIZE];
  10.    int currentPos=-1;
  11.    public void push(Object obj) throws Exception
  12.    {
  13.        print("push:"+obj);
  14.        if(currentPos<MAX_SIZE-1)
  15.        {
  16.            stack[++currentPos]=obj;
  17.        }
  18.        else
  19.        {
  20.            stack= appendArray(stack);
  21.            stack[++currentPos]=obj;
  22.            MAX_SIZE=stack.length;
  23.        }
  24.     }
  25.    
  26.    private Object[] appendArray(Object[] stack2) {
  27.     // TODO Auto-generated method stub
  28.     Object[] stack3 = new Object[stack2.length+MAX_SIZE];
  29.     for(int i=0;i<stack2.length;i++)
  30.     {
  31.         stack3[i]=stack2[i];
  32.     }
  33.     return stack3;
  34. }
  35. public Object pop() throws Exception
  36.    {
  37.        Object popobj=null;
  38.        if(currentPos>-1)
  39.        {
  40.            popobj=stack[currentPos];
  41.            print("pop:"+popobj);
  42.            stack[currentPos--]=null;
  43.            
  44.         }
  45.        return popobj;
  46.    }
  47. public int getSize()
  48. {
  49.     return currentPos+1;
  50. }
  51.    
  52.    public static void main(String args[]) 
  53.    {
  54.        Stack stack = new Stack();
  55.        try
  56.        {
  57.            stack.push("1");
  58.            stack.push("2");
  59.            stack.push("3");
  60.            stack.push("4");
  61.            stack.pop();
  62.            stack.pop();
  63.            stack.push("5");
  64.            stack.push("6");
  65.            stack.push("1");
  66.            stack.push("2");
  67.            stack.pop();
  68.            stack.pop();
  69.            stack.pop();
  70.            stack.pop();
  71.            stack.pop();
  72.            stack.pop();
  73.            stack.pop();
  74.            stack.push("3");
  75.            stack.push("4");
  76.            stack.push("5");
  77.            stack.pop();
  78.            stack.push("6");
  79.            
  80.            for(int i=0,j=stack.getSize();i<j;i++)
  81.            {
  82.               
  83.                System.out.println("obj:"+(String)stack.pop());
  84.            }
  85.        }
  86.        catch(Exception e)
  87.        {
  88.            e.printStackTrace();
  89.        }
  90.        
  91.        
  92.    }
  93.    private void print(String msg)
  94.    {
  95.        System.out.println(msg);
  96.    }
  97. }
    1. package com.sms;
    2. /**
    3.  * 
    4.  * @author Administrator
    5.  *
    6.  */
    7. public class Queue {
    8. private static int MAX_SIZE=10;
    9. private Object[] queue = new Object[MAX_SIZE];
    10. int currentPos=-1;
    11. int head=-1;
    12. public void push(Object obj)
    13. {
    14.     try
    15.     {
    16.         if(currentPos<MAX_SIZE-1)
    17.         {
    18.             queue[++currentPos]=obj;
    19.             if(currentPos==0) head=0;
    20.         }
    21.         else
    22.         {
    23.             if(head==0)
    24.             {
    25.                 queue=appendQueue(queue,head,MAX_SIZE);
    26.                 MAX_SIZE=MAX_SIZE+MAX_SIZE;
    27.                 queue[++currentPos]=obj;
    28.             }
    29.             else
    30.             {
    31.                 queue=appendQueue(queue,head,0);
    32.                 
    33.                 currentPos=currentPos-head;
    34.                 queue[++currentPos]=obj;
    35.                 head=0;
    36.                 
    37.             }
    38.             
    39.             
    40.         }
    41.     }
    42.     catch(Exception e)
    43.     {
    44.         e.printStackTrace();
    45.     }
    46. }
    47. public Object pop()
    48. {
    49.     Object popObj=null;
    50.     try
    51.     {
    52.         if(currentPos==head && head!=-1)
    53.         {
    54.             popObj=queue[head];
    55.             queue[head]=null;
    56.             currentPos=-1;
    57.             head=-1;
    58.         }
    59.         else if(head==-1
    60.         {
    61.             
    62.         }
    63.         else
    64.         {
    65.             popObj=queue[head];
    66.             queue[head++]=null;
    67.         }
    68.         
    69.     }
    70.     catch(Exception e)
    71.     {
    72.         e.printStackTrace();
    73.     }
    74.     return popObj;
    75.     
    76. }
    77. public int getSize()
    78. {
    79.     if(currentPos==-1return 0;
    80.     else 
    81.         return currentPos-head+1;
    82. }
    83. private Object[] appendQueue(Object[] queue2, int pos, int appendSize) {
    84.     // TODO Auto-generated method stub
    85.     Object[] newQueue = new Object[MAX_SIZE+appendSize];
    86.     for(int j=0,i=pos;i<MAX_SIZE;i++)
    87.     {
    88.         newQueue[j++]=queue2[i];
    89.     }
    90.     return newQueue;
    91. }
    92. public static void main(String args[]) 
    93. {
    94.        Queue stack = new Queue();
    95.        try
    96.        {
    97.            stack.push("1");
    98.            stack.push("2");
    99.            stack.push("3");
    100.            stack.push("4");
    101.            stack.pop();
    102.            stack.pop();
    103.            stack.push("5");
    104.            stack.push("6");
    105.            stack.push("1");
    106.            stack.push("2");
    107.            stack.push("5");
    108.            stack.push("6");
    109.            stack.push("1");
    110.            stack.push("2");
    111.            stack.pop();
    112.            stack.pop();
    113.            stack.pop();
    114.            stack.pop();
    115.            stack.pop();
    116.            stack.pop();
    117.            stack.pop();
    118.            stack.push("3");
    119.            stack.push("4");
    120.            stack.push("5");
    121.            stack.pop();
    122.            stack.push("6");
    123.            
    124.            for(int i=0,j=stack.getSize();i<j;i++)
    125.            {
    126.               
    127.                System.out.println("obj:"+(String)stack.pop());
    128.            }
    129.        }
    130.        catch(Exception e)
    131.        {
    132.            e.printStackTrace();
    133.        }
    134.        
    135.        
    136. }
    137. private void print(String msg)
    138. {
    139.        System.out.println(msg);
    140. }
    141. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值