api design and code implementation of stack and queue

Stack and queue are the most common data structures. In this article, we use java code to implement stack and queue.

1, Stack

1. Stack features

The characteristic of the stack is first in and then out, because there is only one data access (implemented with a head pointer) for the stack. Therefore, the elements in the stack are placed at the bottom of the stack, and the elements in the stack are placed at the top of the stack. The operation to store an element in the stack is called push, and the operation to remove an element from the top of the stack is called pop.

2. api design of stack

Class name: stack < T > (based on linked list physically)
Construction method: Stack()
Member method:
Public void push (t t t) puts an element into the stack
public T pop() takes an element from the stack
public int size() get length
public boolean isEmpty() to determine whether it is empty

3. java code implementation of stack

package com.tingcream.alg.linear;
 
import java.util.Iterator;
 
/**
 * The api design of stack (physically using linked list to realize stack)
 */
public class Stack<T> implements  Iterable<T>{
    private Node head;//Head node
    private int N;//Chain length
 
    //Construction method
    public Stack(){
        this.head=new Node(null,null);
        this.N=0;
    }
 
    //Stack pressing
    public void push(T t){
        //First node found
        Node<T> oldFirst=head.next;
        Node<T>  newNode=new Node(t,null);
        newNode.next=oldFirst;
        head.next=newNode;
 
        N++;
    }
 
    //Bomb stack
    public T pop(){
        Node<T> oldFirst=  head.next;
        if(oldFirst==null){
            return null ;
        }
        head.next=oldFirst.next;
        N--;
        return oldFirst.item;
    }
 
    //Get length
    public int size(){
        return this.N;
    }
    //Judge whether it is empty
    public boolean isEmpty(){
        return this.N==0;
    }
    @Override
    public Iterator<T> iterator() {
        return new Itr();
    }
 
    private class Itr implements Iterator{
 
        private Node n;
 
        public Itr(){
            this.n=head;
        }
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }
 
        @Override
        public Object next() {
            n=n.next; //n pointer moves backward one step
            return n.item;
        }
    }
 
    private class Node<T> {
        private  T  item;
        private  Node next;
        public  Node(T item,Node next){
            this.item=item;
            this.next=next;
        }
    }
}

4. Test code

public class StackTest {
    public static void main(String[] args) {
        Stack<String> stack=new Stack<>();
        stack.push("a");
        stack.push("b");
        stack.push("c");
        stack.push("d");
        for(String s: stack){
            System.out.println(s);
        }
        System.out.println("============");
        String s1= stack.pop();
        System.out.println("eject:"+s1);//d
        System.out.println("Remaining length:"+stack.size());//3
    }
}

2, Queues

1. Characteristics of queues

The characteristic of queue is first in first out (FIFO). This is because there are two operation entrances (implemented by the head pointer and tail pointer). The operation of the head pointer to insert new elements (queue) in the queue is called queue, and the operation of removing one element in the queue is called queue

2. api design of queue

Class name: queue < T >
Construction method: Queue()
Member variable:
private Node head
private Node last tail node
Number of private int N linked lists
Membership method:
Public void queue (t t t)
public T dequeue() out
public int size() get length
public boolean isEmpty() to determine whether it is empty

3. java code implementation of queue

package com.tingcream.alg.linear;
 
import java.util.Iterator;
 
/**
 * Queues (physically implemented using linked lists)
 */
public class Queue<T> implements Iterable<T>{
    private Node head;//Head node
    private Node last;//Last node
    private int N;//Number of linked lists
 
 
    public Queue(){
        this.head=new Node(null,null);
        this.last=null;
        this.N=0;
    }
 
    //Whether the queue is empty
    public boolean isEmpty(){
        return this.N==0;
    }
    //Number of elements in the queue
    public int size(){
        return this.N;
    }
 
    //Join an element
    public void enqueue(T t){
        //If the current tail node is null,
        if(last==null){
            last=new Node(t,null);
            head.next=last;
        }else{
          // The current tail node is not null
            Node oldLast=  last;
            last= new Node(t,null);
            oldLast.next=last;
        }
        N++;
 
    }
 
    //Take an element out of the team
    public T dequeue(){
       if(isEmpty()){
           return null;
       }
       Node<T> oldFirst=head.next;
       head.next=oldFirst.next;
       N--;
 
       //If all data nodes in the queue are deleted, you need to reset the last node to null
       if(isEmpty()){
           last=null;
       }
        return oldFirst.item ;
    }
 
 
 
    @Override
    public Iterator<T> iterator() {
        return new Itr();
    }
 
    //Inner class iterator
    private  class Itr implements Iterator<T>{
        private Node<T> n;
 
        public Itr(){
            this.n=head;
        }
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }
 
        @Override
        public T next() {
            n=n.next;//n pointer moves backward one step
            return n.item;
        }
    }
 
 
 
    private class Node<T> {
        private  T  item;
        private  Node next;
        public  Node(T item,Node next){
            this.item=item;
            this.next=next;
        }
    }
 
}

4. Test code

import com.tingcream.alg.linear.Queue;


public class QueueTest {
    public static void main(String[] args) {
        Queue<String> queue=new Queue<String>();
        queue.enqueue("a");
        queue.enqueue("b");
        queue.enqueue("c");
        queue.enqueue("d");

        //Because the iterator interface is implemented in the Queue class, you can use for traversal
        for(String s:queue){
            System.out.println(s);
        }
        System.out.println("===========");

        String s1=queue.dequeue();
        String s2=queue.dequeue();

        System.out.println("s1:"+s1);//a
        System.out.println("s2:"+s2);//b
        System.out.println("Number of remaining elements:"+queue.size());//2
    }
}

 

 

Tags: Java

Posted on Sat, 27 Jun 2020 01:46:56 -0400 by tsg