Stack and queue of data structure in java

Stack

A stack is a linear table that only inserts or deletes at the end of the table, also known as a LIFO linear table

  • Stack interface design
    interface Stack <T>{
    
        void destroyStack();
        boolean isEmpty();
        int stackLength();
        T getTop(); //Top of stack element
        void Push(T element); //Stack in operation
        T Pop(); //Out of stack operation
        void stackTraverse();
    }
    

Sequential stack

Stack implementation based on sequence table

  • Implementation code
    public class SqStack<T> implements Stack<T> {
    
        private final int MAX_SIZE = 10; //Maximum stack capacity
        private Object[] array;  //Array based sequential stack
        private int length; //Number of elements in the stack
        
        private int top; //Stack top identification
        private int base;//Stack bottom identification
        
        public SqStack() {  //Initialization stack
            
            array = new Object[MAX_SIZE]; //Maximum capacity of initialization stack
            length = 0;
            top = 0;
            base = 0;
        }
        
        @Override
        public void destroyStack() {
            // TODO Auto-generated method stub
            top = 0;
            length = 0;
        }
    
        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            if(this.length == 0)
                return true;
            else
                return false;
        }
    
        @Override
        public int stackLength() {
            // TODO Auto-generated method stub
            return length;
        }
    
        @Override
        public T getTop() { //De stack elements
            // TODO Auto-generated method stub
            return (T)array[top - 1];  //Returns the top element of the stack
        }
    
        @Override
        public void Push(T element) { //Stack operation
            // TODO Auto-generated method stub
            array[top++] = element;
            length++;
        }
    
        @Override
        public T Pop() {//Out of stack operation
            // TODO Auto-generated method stub
            length--;
            return (T) array[--top];  //Returns the top element of the stack, top minus one
            
        }
    
        @Override
        public void stackTraverse() { //Traverse from the bottom of the stack to the top of the stack
            // TODO Auto-generated method stub
            for(int i = base; i< top; i++) {
                System.out.print(array[i] + " ");
            }
            System.out.println();
        }
    }
    
  • Code analysis
    • The length of the sequential stack is the index value of the top ID, and the length attribute is optional.
    • return (T) array[--top] in the code after returning the top element of the stack to top - because the elements in the array are array[0]~array[top-1]

Chain stack

Stack implementation based on linked list

  • code implementation
    class LinkStack<T> implements Stack<T> {
    
        private class Node{  //Node class
            
            private Node next; //Subsequent citation
            private T dataElement;
            
            public Node() {   //Construct head node
                // TODO Auto-generated constructor stub
                this.next = null;   //Next in node (connect next node)
                this.dataElement = null;
            }
            public Node(T dataElement) {  //Construct tail node
                // TODO Auto-generated constructor stub
                this.next = null;
                this.dataElement = dataElement;
            }
        }
        
        private Node top;//Top node of stack
        private Node base;//Stack bottom node (here the stack bottom node is the head node)
        private int length;
        
        public LinkStack() {
            // TODO Auto-generated constructor stub
            base = new Node(); //Initializing the bottom node of the stack
            top = base;
            length = 0;
        }
        
        @Override
        public void destroyStack() { //Reset stack
            // TODO Auto-generated method stub
            top = base;
            length = 0;
        }
    
        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            if(this.length == 0)
                return true;
            else
                return false;
        }
    
        @Override
        public int stackLength() {
            // TODO Auto-generated method stub
            return length;
        }
    
        @Override
        public T getTop() {
            // TODO Auto-generated method stub
            return top.dataElement;
        }
    
        @Override
        public void Push(T element) { //Stack operation
            // TODO Auto-generated method stub
            //The list constructed by the chain stack is a conventional one-way list
            Node newNode = new Node(element);  
            newNode.next = top;
            top = newNode;
            length++;
        }
    
        @Override
        public T Pop() { //Out of stack operation
            // TODO Auto-generated method stub
            T data = top.dataElement;
            top = top.next;
            length--;
            return data;
        }
    
        @Override
        public void stackTraverse() { //ergodic
            // TODO Auto-generated method stub
            Node currentNode = top;
            while(currentNode.next != null) {
                System.out.print( currentNode.dataElement + " ");
                currentNode = currentNode.next;
            }
            System.out.println();
        }
    }
    
  • Code analysis
    • push operation of stack

      The stack pushing operation points the subsequent reference of the new node and new node to the top node, while the base node is the lowest head node. Therefore, the chain stack is actually an inverted conventional one-way linked list, and the top layer is the new entry node.

    • Stack out operation

      Return the top node and reset top to its successor

queue

A queue is a first in, first out linear table

  • Queue interface design
    interface Queue<T> {
    
        void destroy();//Reset queue
        boolean isEmpty();
        int queueLength();
        T getHead();//To the opposite element
        void enQueue(T element); //Queue up
        T deQueue();//Out of line
        void queueTraverse(); //Traversal of queue
    }
    

Sequential queue

Queue based on sequence table

  • code implementation
    class SqQueue<T> implements Queue<T> { 
    
        private final int MAX_SIZE = 10; //Maximum capacity of the queue
        private Object[] array; //Array container
        private int front;//Team leader
        private int rear;//Contratail
        
        public SqQueue() { //Initialize queue
            // TODO Auto-generated constructor stub
            array = new Object[MAX_SIZE];
            front = rear = 0;
        }
        @Override
        public void destroy() { //Reset queue
            // TODO Auto-generated method stub
            rear = front; //When the end of the queue is equal to the opposite end, the queue is empty
        }
    
        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            if(front == rear)
                return true;
            else 
                return false;
        }
    
        @Override
        public int queueLength() {//Take queue length
            // TODO Auto-generated method stub
            return (rear - front + MAX_SIZE) % MAX_SIZE;//Queue length algorithm
        }
    
        @Override
        public T getHead() {
            // TODO Auto-generated method stub
            return (T) array[front];
        }
    
        @Override
        public void enQueue(T element) { //Add data
            // TODO Auto-generated method stub
            if((rear + 1) % MAX_SIZE == front) //Determine whether the queue is full
                System.out.println("The queue is full and cannot be added");
            else {
                array[rear] = element;//Data entry
                rear = (rear + 1) % MAX_SIZE; //The end of the team moved back one place
            }
        }
    
        @Override
        public T deQueue() {  //Return the opposite element and delete
            // TODO Auto-generated method stub
            if( isEmpty() )
                System.out.println("Queue is empty and cannot be deleted");
            else {
                
                T temp = (T) array[front];//Data out of the team
                front = (front + 1) % MAX_SIZE; //The head of the team moved back one place
                return temp;
            }
            return null;
        }
    
        @Override
        public void queueTraverse() { //ergodic
            // TODO Auto-generated method stub
            int temp = front;  //Cannot directly operate on front (affect result)
            while(temp != rear) {
                System.out.print(array[temp] + " ");
                temp = (temp + 1) % MAX_SIZE;
            }
            System.out.println();
        }
    }
    
  • Code analysis
    • The front and rear movement of queues

      Compared with adding and deleting elements on the stack, only the top of the stack moves differently. When the queue enters, the rear moves backward, and when the queue exits, the front moves backward. Therefore, when the front moves backward, the memory before will be wasted. Therefore, in order to make the rear reach the maximum value of the array, it can move back from 0 until the rear is equal to the front. When the queue enters, it should be the rear= (rear + 1) % MAX_ Size; when leaving the team, it should be front = (front + 1)% max_ SIZE;

    • Judgment of queue empty and full

      To empty: front == rear
      Team full: (rear + 1)% max_ SIZE == front

Chain queue

Queue based on linked list

  • code implementation
    class LinkQueue<T> implements Queue<T> {
    
        private class Node{  //Node class
            
            private Node next;
            private T dataElement;
            
            public Node() {   //Construct head node
                // TODO Auto-generated constructor stub
                this.next = null;   //Next in node (connect next node)
                this.dataElement = null;
            }
            public Node(T dataElement) {  //Construct tail node
                // TODO Auto-generated constructor stub
                this.next = null;
                this.dataElement = dataElement;
            }
        }
        
        private Node front;//Team leader node
        private Node rear; //Tail node
        private int length;
        
        public LinkQueue() { //Initialize queue
            // TODO Auto-generated constructor stub
            front = new Node();
            rear = front;
            length = 0;
        }
        
        @Override
        public void destroy() {//Reset node
            // TODO Auto-generated method stub
            rear = front;
            length = 0;
        }
    
        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            if(rear == front)
                return true;
            else
                return false;
        }
    
        @Override
        public int queueLength() {
            // TODO Auto-generated method stub
            return length;
        }
    
        @Override
        public T getHead() {
            // TODO Auto-generated method stub
            return front.next.dataElement;
        }
    
        @Override
        public void enQueue(T element) {//Enter the team
            // TODO Auto-generated method stub
            Node newNode = new Node(element);
            rear.next = newNode;
            rear = newNode;
        }
    
        @Override
        public T deQueue() {//Out of the team
            // TODO Auto-generated method stub
            //The dequeue operation is equivalent to deleting the post drive node of the head node
            T temp = front.next.dataElement;
            front.next = front.next.next;
            return temp;
        }
    
        @Override
        public void queueTraverse() { //ergodic
            // TODO Auto-generated method stub
            Node curentNode = front;
            while(curentNode != rear) {
                System.out.print( curentNode.next.dataElement + " ");
                curentNode = curentNode.next;
            }
            System.out.println();
        }
    }
    
  • Code analysis
    • Queue entry and exit

      The entry of the chain team is the same as the addition of the regular post insertion chain list. Each time the team exits, the subsequent nodes of the head node are deleted (first in, first out)

summary

  • Stack LIFO, queue FIFO
  • For the sequential stack, base corresponds to the first element, and top-1 corresponds to the last element, and for the chain stack base.next Corresponding to the first element, top corresponds to the last element, and the queue is the same

Tags: Attribute

Posted on Mon, 29 Jun 2020 05:34:26 -0400 by markjoe