堆栈和队列的实现
1.使用数组实现,代码和结果如下所示:
要求:
写Stack.java,实现堆栈功能,使用int数组保存数据特点:先进后处 后进先出
写Queue.java,实现队列功能,使用int数组保存数据特点:先进先出 后进后出 使用Test.java对堆栈和队列进行测试
如下所示,分别为实现方法:
首先实现stack.java类,其代码如下所示:
- package ex.md05;
- public class Stack
- {
- private int[] a=new int[1000];
- private int i=0;
- public void push(int m)
- {
- a[++i]=m;
- }
- public int pop()
- {
- if(i>0)
- {
- return a[i--];
- }
- else return -1;
- }
- }
- package ex.md05;
- public class Queue
- {
- int[] a=new int[1000];
- private int i=1;
- public void in(int m)
- {
- a[i++]=m;
- }
- public int out()
- {
- int k=1;
- int index=0;
- int temp=a[k];
- for(int j=k;j<i;j++)
- {
- a[j-1]=a[j];
- index++;
- }
- i=index;
- return temp;
- }
- }
- package ex.md05;
- public class Test
- {
- public static void main(String[] args)
- {
- Stack stack = new Stack();
- //stack.pop();
- System.out.println("Stack push()---1-200--------");
- for(int i=1; i<=200; i++){
- stack.push(i);
- }
- System.out.println("Stack pop()---1-100--------");
- for(int i=1; i<=100; i++){
- System.out.println("pop:" + stack.pop());
- }
- System.out.println("Stack push()---201-300--------");
- for(int i=201; i<=300; i++){
- stack.push(i);
- }
- System.out.println("Stack pop()---1-200--------");
- for(int i=1; i<=200; i++){
- System.out.println("pop:" + stack.pop());
- }
- Queue queue = new Queue();
- //queue.out();
- System.out.println("Queue in()---1-200--------");
- for(int i=1; i<=200; i++){
- queue.in(i);
- }
- System.out.println("Queue out()---1-100--------");
- for(int i=1; i<=100; i++){
- System.out.println("out:" + queue.out());
- }
- System.out.println("Queue in()---201-300--------");
- for(int i=201; i<=300; i++){
- queue.in(i);
- }
- System.out.println("Queue out()---1-200--------");
- for(int i=1; i<=200; i++){
- System.out.println("out:" + queue.out());
- }
- }
- }
编译运行之后,我们能够看到运行结果如下所示:
根据分析,我们的运行结果是正确的(以上只是部分结果,下面是运行后的所有结果,请下载查看http://download.****.net/my)
2.使用list来实现功能,代码如下所示:
写MyStack类,实现堆栈功能。在类中使用ArrayList保存数据。
写MyQueue类,实现队列功能。在类中使用ArrayList保存数据。
使用Test.java测试堆栈和队列
首先实现MyStack.java代码:
- package sample;
- import java.util.*;
- public class MyStack
- {
- List<Integer> list = new ArrayList<Integer>();
- Iterator it=list.iterator();
- int index=0;
- public MyStack(){}
- public void push(Integer i)
- {
- list.add(i);
- index++;
- }
- public Integer pop()
- {
- if(!(list.isEmpty()))
- {
- index--;
- return (Integer)list.remove(index);
- }
- return null;
- }
- }
- package sample;
- import java.util.*;
- public class MyQueue
- {
- List<Integer> list = new ArrayList<Integer>();
- Iterator it=list.iterator();
- int index=0;
- public MyQueue(){}
- public void in(Integer i)
- {
- list.add(i);
- index++;
- }
- public Integer out()
- {
- if(!(list.isEmpty()))
- {
- Integer temp=0;
- temp=(Integer)list.get(0);
- list.remove(0);
- index--;
- return temp;
- }
- return null;
- }
- }
- package sample;
- public class Test
- {
- public static void main(String[] args)
- {
- MyStack stack = new MyStack();
- stack.push(new Integer(1));
- stack.push(new Integer(2));
- stack.push(new Integer(3));
- System.out.println(stack.pop());
- stack.push(new Integer(4));
- System.out.println(stack.pop());
- System.out.println(stack.pop());
- System.out.println(stack.pop());
- System.out.println(stack.pop());
- stack.push(new Integer(5));
- System.out.println(stack.pop());
- System.out.println(stack.pop());
- System.out.println("------------------------");
- MyQueue queue = new MyQueue();
- queue.in(new Integer(1));
- queue.in(new Integer(2));
- queue.in(new Integer(3));
- System.out.println(queue.out());
- queue.in(new Integer(4));
- System.out.println(queue.out());
- System.out.println(queue.out());
- System.out.println(queue.out());
- System.out.println(queue.out());
- queue.in(new Integer(5));
- System.out.println(queue.out());
- System.out.println(queue.out());
- }
- }
以上就是堆栈的两种实现方法