栈与队列的实现方法

来源:互联网 发布:万博宣伟 知乎 编辑:程序博客网 时间:2024/06/08 09:34

如何实现栈?
后进先出 ~ 有两种方式:
1、数组

import java.util.Arrays;//数组实现栈public class MyStack<E>{    private Object[] stack;    private int size;//数组中存储元素的个数    public MyStack()    {        stack = new Object[10];    }    public boolean isEmpty()    {        return size==0;    }    public E peek()    {        if(isEmpty())            return null;        return (E) stack[size-1];    }    public E pop()    {         E e=peek();         stack[size-1]=null;        size--;        return e;    }    public E push(E item)    {//检查容量        ensurecapacity(size+1);        stack[size++]=item;        return item;        }    public void ensurecapacity(int size)    {        int newlen=0;        int len=stack.length;        if(len>size)             newlen = 10; //每次数组扩充的容量            stack =Arrays.copyOf(stack, newlen);    }        public static void main(String[] args) {        MyStack<Integer> s = new MyStack<Integer>();        s.push(1);        s.push(4);        System.out.println(s.size);        System.out.println("栈顶元素"+s.peek());    }}

2、链表

//不是线程安全的class Node<E>{    Node<E>next =null;    E data;    public Node(E data)    {        this.data=data;    }}public class MyStack<E>{    Node<E> top=null;    public boolean isEmpty()    {        return top==null;    }    public void push(E data)    {        Node <E> newnode= new Node<E>(data);//新建立        newnode.next=top;        top=newnode;    }    public E pop()    {        if(isEmpty())            return null;        E data =top.data; //top 有值,就是newnode        top=top.next;        return data;    }    public E peek()    {        if(isEmpty())            return null;        return top.data;    }    public static void main(String[] args)    {        MyStack<Integer> s =new MyStack<Integer>();        s.push(2);        s.push(6);        System.out.println(s.peek());        System.out.println(s.pop());        System.out.println(s.pop());    }}

如何实现队列?
先进先出~front 和back
1、链表

package stack;class Node<E>    {        Node <E>next =null;        E data;        public Node(E data)        {            this.data=data;        }    }public class MyQueue<E> {    private Node<E> head=null;    private Node<E> tail=null;    public boolean isEmpty()    {        return head==tail;    }    public void put(E data)    {        Node<E> newnode= new Node<E>(data);        if(head==null&&tail==null)//队列为空            head =tail=newnode; //        else        {            tail.next=newnode;            tail=newnode; //??        }    }    public E pop()    {        if(this.isEmpty()) //            return null;        E data =head.data;        head=head.next;        return data;    }    public int size()    {        Node<E> temp=head;        int num=0;        while(temp!=null)        {   num++;            temp=temp.next;        }        return num;    }    public static void main(String[] args) {        MyQueue <Integer> q= new MyQueue<Integer>();        q.put(3);        q.put(6);        q.put(9);        System.out.println("长度:"+q.size());        System.out.println(q.pop());    }}

2、数组
ArrayList其实是包装了一个数组Object[],当实例化一个arraylist时,一个数组也被数组化,象棋中添加对象时,数组大小也相应改变。特点:快速随即访问,通过get可以快速访问。但是向其中添加对象速度慢,数组需要移动后面所有的对象。

LinkedList是通过结点直接彼此连接实现的。每一个节点都包含前一个节点的引用,后一个节点的引用和结点存储的值。当一个结点插入时,只需修改其中保持先后关系的节点的引用即可,删除也一样。新节点可以存在内存中任何地方。 但是不能随即访问,虽然存在get方法,但是是通过遍历结点来定位的所以速度慢。
总之,就是数据结构中的顺序存储和链式存储。

//数组实现 为了多线程安全,增加了对队列操作的同步。public class MyQueue<E>{    private LinkedList<E> list =new LinkedList<E>(); //??    private int size=0;    public synchronized void put( E e)    {        list.addLast(e);        size++;    }    public synchronized E pop()    {        size--;        return list.removeFirst();    }    public synchronized boolean isEmpty()    {        return size==0;    }    public synchronized int size()    {        return size;    }    public static void main(String[] args)    {        MyQueue<Integer> q= new MyQueue<Integer>();        q.put(2);        q.put(4);        q.put(6);        System.out.println("长度"+q.size);        System.out.println(q.pop());    }}

**如何使用两个栈来模拟队列?

//两个栈模拟队列 一个入栈A,一个出栈B//入栈进入A即可,但是出栈分两种情况:1 若B不为空,则直接弹出B中数据;//2 若B为空,则依次弹出栈A的数据,放入B中,再弹出B中的数据public class MyQueue<E>{    private Stack<E> s1 =new Stack<E>();    private Stack<E> s2= new Stack<E>();    public synchronized void put( E e)    {        s1.push(e);    }    public synchronized E pop()    {        if(s2.isEmpty())        {            while(!s1.isEmpty())                s2.push(s1.pop()); //!!        }        return s2.pop();    }    public  synchronized boolean isEmpty()    {        return s1.isEmpty()&&s2.isEmpty();    }    public static void main(String[] args)    {        MyQueue<Integer> q= new MyQueue<Integer>();        q.put(2);        q.put(3);        q.put(6);        System.out.println(q.pop());//2        System.out.println(q.pop());//3    }}
0 0
原创粉丝点击