JAVA:用数组和链表实现队列

一,队列

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端进行删除操作,而在表的后端进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

队列的数据元素又称为队列元素。在队列中插入一个队列元素称为入队,从队列中删除一个队列元素称为出队。因为队列只允许在一端插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为先进先出线性表。

二,数组实现

1,定义属性和构造方法

1)需要定义长度为0的数组values,存储元素的个数size,数组当前长度length,默认最大容量DEFAULT_LENGTH。

2)传入参数为顺序表长度,如果传入长度小于0,报错,如果传入长度小于等于20,就用默认属性初始化,大于20就用传入长度初始化。


    private Object[] values = {};//长度为零的数组
    private int size;//当下坐标,当前存储的元素个数
    private int length;//表示数组当前的长度 最大容器
    private static final int DEFAULT_LENGTH = 20;//默认最大容量

    public MyLinkList (int initiallength){
        if (initiallength <= 0){
            throw new IllegalArgumentException("initiallength 不能小于0");
        }
        if (initiallength <= 20){
            length = DEFAULT_LENGTH;
            values = new Object[length];
            size=0;
        }
        else{
            length = initiallength;
            values = new Object[length];
            size=0;
        }
    }

2,插入操作

数组有空位直接在后面插入,队列满了则报错。


public void put(E e){
        if(size < length){
            values[size++]=e;
        }else{
            throw new IllegalArgumentException("队列满了");
        }

    }

3,删除操作

队列为空时报错,不为空时删除第一个,后面的元素前移。


public E take(){

        if(size==0){
            throw new NullPointerException("队列为空!");
        }
        E e = (E) values[0];
        for (int i = 0;i<size;i++){
            values[i]=values[i+1];
        }
        size--;
        return e;
    }

4,按元素查找

通过循环判断队列中是否有元素与查找元素相同。


public boolean Get(Object e) {
        for (int i = 0; i < length; i++) {
            if (values[i] == e || values[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

5,查看所有元素

通过循环输出查看队列中的所有元素。


public void Forall(){
        for(int i =0;i<length;i++){
            System.out.println(values[i]);
        }
    }

6,完整代码


public class MyLinkList <E>{

    private Object[] values = {};//长度为零的数组
    private int size;//当下坐标,当前存储的元素个数
    private int length;//表示数组当前的长度 最大容器
    private static final int DEFAULT_LENGTH = 20;//默认最大容量

    public MyLinkList (int initiallength){
        if (initiallength <= 0){
            throw new IllegalArgumentException("initiallength 不能小于0");
        }
        if (initiallength <= 20){
            length = DEFAULT_LENGTH;
            values = new Object[length];
            size=0;
        }
        else{
            length = initiallength;
            values = new Object[length];
            size=0;
        }
    }

    public void put(E e){
        if(size < length){
            values[size++]=e;
        }else{
            throw new IllegalArgumentException("队列满了");
        }

    }

    public E take(){

        if(size==0){
            throw new NullPointerException("队列为空!");
        }
        E e = (E) values[0];
        for (int i = 0;i<size;i++){
            values[i]=values[i+1];
        }
        size--;
        return e;
    }

    //查找
    public boolean Get(Object e) {
        for (int i = 0; i < length; i++) {
            if (values[i] == e || values[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

    public void Forall(){
        for(int i =0;i<length;i++){
            System.out.println(values[i]);
        }
    }

    public static void main(String[] args) {
        MyLinkList<Integer> myLinkList = new MyLinkList(20);

        for (int i =0;i<10;i++){
            myLinkList.put(i);
        }

        System.out.println("存入10个数");
        myLinkList.Forall();

        for(int i = 0;i<1;i++){
            myLinkList.take();
        }

        System.out.println("取出第一个");
        myLinkList.Forall();

        System.out.println(myLinkList.Get(8));

        myLinkList.Forall();

        /*for(int i=0;i<9;i++) {
            Integer take = myLinkList.take();
            System.out.println("take:" + take);
        }*/
    }
}

三,链表实现

1,创建节点类

1)属性

value:节点中存储的值

next:节点中存储的下一个节点的地址

2)构造方法

节点中存储值和下一个节点的地址


class Node<E>{
        Object value;
        Node<E> next;

        public Node(Object value){
            this.value = value;
            next = null;
        }
        public void setNext(Node<E> next){
            this.next=next;
        }
    }

2,定义属性和初始化

定义头尾指针,链表大小,对链表进行初始化。


    private Node<E> head;
    private Node<E> last;
    int size;
    int maxSize;

    public MyLinkedList(){
        head = last = null;
        size = 0;
    }

3,插入操作

链表为空则头尾指针指向插入节点,不为空则插在最后一个元素后


public void put(E e){
        if(size==0){
            head = last = new Node<> (e);
        }else{
            last.setNext(new Node<> (e));
            last = last.next;
        }
        size++;
    }

4,删除操作

链表为空则报错,不为空则删除头结点移动头指针到下一个节点,如果链表只有一个元素删除头结点后尾指针要指向空。


public E take(){
        if(size==0){
            throw new NullPointerException("队列为空!");
        }
        E e = (E) head.value;
        head = head.next;
        if(head == null){
            last = null;
        }
        size--;
        return e;
    }

5,按元素查找

通过循环判断队列中是否有元素与查找元素相同。


public boolean get(E e){
        Node<E> tempNode = head;
        while(tempNode != null){
            if(tempNode.value == e){
                return true;
            }tempNode = tempNode.next;
        }
        return false;
    }

6,查看所有元素

通过循环输出查看队列中的所有元素。


public void ForAll(Node head) {
        Node<E> tempNode = head;
        while(tempNode != null){
            System.out.println (tempNode.value);
            tempNode = tempNode.next;
        }
    }

7,完整代码


public class MyLinkedList<E> {
    class Node<E>{
        Object value;
        Node<E> next;

        public Node(Object value){
            this.value = value;
            next = null;
        }
        public void setNext(Node<E> next){
            this.next=next;
        }
    }

    private Node<E> head;
    private Node<E> last;
    int size;
    int maxSize;

    public MyLinkedList(){
        head = last = null;
        size = 0;
    }

    public void put(E e){
        if(size==0){
            head = last = new Node<> (e);
        }else{
            last.setNext(new Node<> (e));
            last = last.next;
        }
        size++;
    }

    public E take(){
        if(size==0){
            throw new NullPointerException("队列为空!");
        }
        E e = (E) head.value;
        head = head.next;
        if(head == null){
            last = null;
        }
        size--;
        return e;
    }

    //查找
    public boolean get(E e){
        Node<E> tempNode = head;
        while(tempNode != null){
            if(tempNode.value == e){
                return true;
            }tempNode = tempNode.next;
        }
        return false;
    }

    //遍历输出
    public void ForAll(Node head) {
        Node<E> tempNode = head;
        while(tempNode != null){
            System.out.println (tempNode.value);
            tempNode = tempNode.next;
        }
    }

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    public static void main(String[] args){
        MyLinkedList<Integer> tstlist = new MyLinkedList<>();
        for(int i = 0;i<10;i++){
            tstlist.put(i);
        }

        System.out.println("存入10个数");
        tstlist.ForAll(tstlist.head);

        for(int i = 0;i<1;i++){
            tstlist.take();
        }

        System.out.println("取出第一个数");
        tstlist.ForAll(tstlist.head);

        System.out.println(tstlist.get(8));

        /*for(int i=0;i<9;i++){
            String take = tstlist.take();
            System.out.println("take:"+take);
        }*/

    }


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值