- package com.sms;
- /**
- *
- * @author Administrator
- *
- */
- public class Stack {
- public static int MAX_SIZE=10;
- private Object[] stack = new Object[MAX_SIZE];
- int currentPos=-1;
- public void push(Object obj) throws Exception
- {
- print("push:"+obj);
- if(currentPos<MAX_SIZE-1)
- {
- stack[++currentPos]=obj;
- }
- else
- {
- stack= appendArray(stack);
- stack[++currentPos]=obj;
- MAX_SIZE=stack.length;
- }
- }
- private Object[] appendArray(Object[] stack2) {
- // TODO Auto-generated method stub
- Object[] stack3 = new Object[stack2.length+MAX_SIZE];
- for(int i=0;i<stack2.length;i++)
- {
- stack3[i]=stack2[i];
- }
- return stack3;
- }
- public Object pop() throws Exception
- {
- Object popobj=null;
- if(currentPos>-1)
- {
- popobj=stack[currentPos];
- print("pop:"+popobj);
- stack[currentPos--]=null;
- }
- return popobj;
- }
- public int getSize()
- {
- return currentPos+1;
- }
- public static void main(String args[])
- {
- Stack stack = new Stack();
- try
- {
- stack.push("1");
- stack.push("2");
- stack.push("3");
- stack.push("4");
- stack.pop();
- stack.pop();
- stack.push("5");
- stack.push("6");
- stack.push("1");
- stack.push("2");
- stack.pop();
- stack.pop();
- stack.pop();
- stack.pop();
- stack.pop();
- stack.pop();
- stack.pop();
- stack.push("3");
- stack.push("4");
- stack.push("5");
- stack.pop();
- stack.push("6");
- for(int i=0,j=stack.getSize();i<j;i++)
- {
- System.out.println("obj:"+(String)stack.pop());
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- private void print(String msg)
- {
- System.out.println(msg);
- }
- }
-
- package com.sms;
- /**
- *
- * @author Administrator
- *
- */
- public class Queue {
- private static int MAX_SIZE=10;
- private Object[] queue = new Object[MAX_SIZE];
- int currentPos=-1;
- int head=-1;
- public void push(Object obj)
- {
- try
- {
- if(currentPos<MAX_SIZE-1)
- {
- queue[++currentPos]=obj;
- if(currentPos==0) head=0;
- }
- else
- {
- if(head==0)
- {
- queue=appendQueue(queue,head,MAX_SIZE);
- MAX_SIZE=MAX_SIZE+MAX_SIZE;
- queue[++currentPos]=obj;
- }
- else
- {
- queue=appendQueue(queue,head,0);
- currentPos=currentPos-head;
- queue[++currentPos]=obj;
- head=0;
- }
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- public Object pop()
- {
- Object popObj=null;
- try
- {
- if(currentPos==head && head!=-1)
- {
- popObj=queue[head];
- queue[head]=null;
- currentPos=-1;
- head=-1;
- }
- else if(head==-1)
- {
- }
- else
- {
- popObj=queue[head];
- queue[head++]=null;
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return popObj;
- }
- public int getSize()
- {
- if(currentPos==-1) return 0;
- else
- return currentPos-head+1;
- }
- private Object[] appendQueue(Object[] queue2, int pos, int appendSize) {
- // TODO Auto-generated method stub
- Object[] newQueue = new Object[MAX_SIZE+appendSize];
- for(int j=0,i=pos;i<MAX_SIZE;i++)
- {
- newQueue[j++]=queue2[i];
- }
- return newQueue;
- }
- public static void main(String args[])
- {
- Queue stack = new Queue();
- try
- {
- stack.push("1");
- stack.push("2");
- stack.push("3");
- stack.push("4");
- stack.pop();
- stack.pop();
- stack.push("5");
- stack.push("6");
- stack.push("1");
- stack.push("2");
- stack.push("5");
- stack.push("6");
- stack.push("1");
- stack.push("2");
- stack.pop();
- stack.pop();
- stack.pop();
- stack.pop();
- stack.pop();
- stack.pop();
- stack.pop();
- stack.push("3");
- stack.push("4");
- stack.push("5");
- stack.pop();
- stack.push("6");
- for(int i=0,j=stack.getSize();i<j;i++)
- {
- System.out.println("obj:"+(String)stack.pop());
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- private void print(String msg)
- {
- System.out.println(msg);
- }
- }
用数组实现堆栈和队列
最新推荐文章于 2024-07-15 11:03:37 发布