线性表——栈,队列

1.3 栈
栈是一种先进后出的数据结构。只能在一端进行插入和删除操作的特殊线性表。
将数据进入栈称为压栈,数据出去称为弹栈。在这里插入图片描述在这里我们用链表的形式表示栈,感兴趣的同学也可以用顺序表的形式来表示。
压栈

public void push(T t){
        //找到首结点指向的第一个结点
        Node oldfirst = head.next;
        //创建新结点
        Node newNode = new Node(t,null);
        //让首结点指向新结点
        head.next=newNode;
        //让新结点指向原来的第一个结点
        newNode.next=oldfirst;
        //元素个数+1
        N++;
    }

弹栈

 public T pop(){
        //找到首结点指向的第一个结点
        Node oldfirst = head.next;
        //测试是否出现空指针
        if (oldfirst==null){
            return null;
        }
        //让首结点指向第一个结点的下一个结点
        head.next=oldfirst.next;
        //元素个数-1
        N--;
        return oldfirst.item;
    }

遍历数据

    public Iterator<T> iterator() {
        return new siterator();
    }
    public class siterator implements Iterator{
        private Node n;

        public siterator() {
            this.n=head;
        }
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }
        @Override
        public Object next() {
            n=n.next;
            return n.item;
        }
    }

完整代码

public class Stack<T> implements Iterable<T>{
    private Node head;
    private int N;
    public Stack() {
        this.head = new Node(null,null);
        this.N=0;
    }
    //创建内部类
    private class Node {
        T item;
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
    public boolean isEmpty(){
        return N==0;
    }
    public int size(){
        return N;
    }
    //把t元素压入栈
    public void push(T t){
        //找到首结点指向的第一个结点
        Node oldfirst = head.next;
        //创建新结点
        Node newNode = new Node(t,null);
        //让首结点指向新结点
        head.next=newNode;
        //让新结点指向原来的第一个结点
        newNode.next=oldfirst;
        //元素个数+1
        N++;
    }
    //把元素t弹出栈
    public T pop(){
        //找到首结点指向的第一个结点
        Node oldfirst = head.next;
        //测试是否出现空指针
        if (oldfirst==null){
            return null;
        }
        //让首结点指向第一个结点的下一个结点
        head.next=oldfirst.next;
        //元素个数-1
        N--;
        return oldfirst.item;
    }
    //遍历数据
    @Override
    public Iterator<T> iterator() {
        return new siterator();
    }
    public class siterator implements Iterator{
        private Node n;

        public siterator() {
            this.n=head;
        }
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

        @Override
        public Object next() {
            n=n.next;
            return n.item;
        }
    }
}

1.4 队列
队列与栈不同,它是一种先进先出的数据结构,只能在一端进行插入,在另一端进行删除的线性表。
在这里插入图片描述插入元素

public void enqueue (T t){
        //当前尾结点为null
        if (last==null){
            last=new Node(t,null);
            head.next=last;
        }
        //当前尾结点不为null
        else{
            Node oldlast=last;
            last = new Node(t,null);
            oldlast.next=last;
        }
        //元素个数+1
        N++;
    }

从队列取出元素

 public T dequeue(){
        if (isEmpty())
            return null ;
        Node oldfirst=head.next;
        head.next=oldfirst.next;
        N--;
        //当队列中的元素删除完,需要重置last=null
        if (isEmpty()){
            last=null;
        }
        return oldfirst.item;
    }

完整代码

public class Queue<T> implements Iterable<T>{
    private Node head;
    private int N;
    private Node last;
    public Queue() {
        this.head = new Node(null,null);
        N = 0;
        this.last = null;
    }

    //创建内部类
    private class Node {
        T item;
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
    public boolean isEmpty(){
        return N==0;
    }
    public int size(){
        return N;
    }
    //向队列中插入元素
    public void enqueue (T t){
        //当前尾结点为null
        if (last==null){
            last=new Node(t,null);
            head.next=last;
        }
        //当前尾结点不为null
        else{
            Node oldlast=last;
            last = new Node(t,null);
            oldlast.next=last;
        }
        //元素个数+1
        N++;
    }
    //从队列中取出元素
    public T dequeue(){
        if (isEmpty())
            return null ;
        Node oldfirst=head.next;
        head.next=oldfirst.next;
        N--;
        //当队列中的元素删除完,需要重置last=null
        if (isEmpty()){
            last=null;
        }
        return oldfirst.item;
    }
    @Override
    public Iterator<T> iterator() {
        return new siterator();
    }
    public class siterator implements Iterator{
        private Node n;
        public siterator(){
            this.n=head;
        }
        @Override
        public boolean hasNext() {
            return  n.next!=null;
        }
        @Override
        public Object next() {
            n=n.next;
            return n.item;
        }
    }
}

b站详细讲解网址:http://yun.itheima.com/course/639.html

猜你喜欢

转载自blog.csdn.net/love521314123/article/details/107358752