On stacks and queues

### Stack

Stack Model

Stack (Stack) limiting the insertion of the element (push) and delete (pop) table in only one position, the position of the end of the table is called the top of the stack the stack (top).

There are only two basic operation of the stack, pushed onto the stack (push) and pop stack (pop), and can only act on the top of the stack. (Only the top element is accessible

You can stack structure understood as a closed bottom, open at the top of the barrel. The first element must be the last to go out to, the latest element must be the first to go out.
Thus stack also known LIFO (last in, first out, Last In First Out) list.

Stack advantage

Stack operations are constant time, but in a very fast time constant. In some machines, Push and pop instructions can be written in a machine, the stack of modern computer operating as part of its instructions. So the stack is the most basic data structure after the relay array in computer science.

Realization stack

Achieve stack into an array of implementation and realization of the list.

1` list implementation
here we use single chain to achieve, first define a pointer to the top of the stack, the stack is a linked list implementation actually simplifies single linked list implementation, see the following code embodied.

public class StackImplementByLinklist<AnyType> {
    public Node<AnyType> first;
    int size;
    //内部类定义node
    public class Node<AnyType>{
        AnyType data;
        Node<AnyType> next;
    }
     //初始化
    public void stack(){
        first=null;
        size=0;
    }

    public void push(AnyType a){
        Node oldNode=first;
        first=new Node();
        first.data=a;
        first.next=oldNode;
        size++;
    }

    public AnyType pop(){
        AnyType a=first.data;
        first=first.next;
        size--;
        return a;
    }

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

    public int size(){
        return size;
    }
}

2` array to achieve
compared to the linked list implementation, the array realization stack more common. Because the array operation time constant is very short, and is simpler to implement.

public class StackImplementByArray<AnyType> {
    AnyType[] arr;
    int size;
    public void stack(int capacity){
        arr=(AnyType[])new Object[capacity];
        size=0;

    }
    public void push(AnyType a){
        if(size==arr.length){
            changeArray(2*size+1);
        }
        arr[size]=a;
        size++;
    }
    public AnyType pop(){
        if(size==0){
            System.out.println("栈顶为空");
            System.exit(0);
        }
        AnyType a=arr[size-1];
        arr[size-1]=null;
        size--;
        return a;
    }
    public boolean isEmpty(){
        return size==0;
    }
    public int size(){
        return size;
    }

    //由于数组大小是要先确定的,因此当数组满了后要扩大数组容量
    public void changeArray(int newCapacity){
        AnyType[] newArr=(AnyType[])new Object[newCapacity];
        for(int i=0;i<arr.length;i++){
            newArr[i]=arr[i];
        }
        arr=newArr;
    }

}

Stack of applications

  • Detected symbol balance

    Syntax error checker symbol compiler, is often achieved through the stack.

In programming, we often use "(), [], {}," "" These symbols, when these symbols are not occurring in pairs, the compiler will report an error, the compiler will not pass.

So, how is the compiler know there is no sign of it occurring in pairs? It is usually such a deal.

When faced with the left symbol, such as "([{" "These, put it onto a prepared stack; otherwise pop stack, testing whether the current symbol is paired with the top element can not be paired once, exit error.


### queue

Queuing model

wiki: queue, also called queue (Queue), a first in first out (FIFO, First-In-First-Out) linear form. Typically using an array or a linked list in specific applications. Only the rear end of the queue (referred REAR) insert operation, a delete operation in the front end (referred to as front). Operation queue stack and the like, the only difference is that only the new data queue is added at the rear end.

Queue model is equivalent to our daily lives line up behind the team into the team, the team in the front ranks.

Multiple queue

Queue array is generally divided into ordinary queue, the queue list, and circular queue.

The queue list: general length is infinite, there is generally no possibility of overflow, run out of destruction, without wasting memory space.

Common array queue: generally limited length, i.e. the length of the array. Because of its location in the memory space and it will not be released from the team after the element, and therefore will waste a lot of memory space.

Circular queue: special array queue, and the queue will waste a lot of memory space ordinary array, resulting in a circular queue. When the tail pointer reaches the end of the circular queue array, the array will return to the starting position, to achieve the reuse of memory.

The queue

1` queue list

public class QueueImplementByLinkList<AnyType> {
    Node first;//队首
    Node last;//队尾
    int size;
    public class Node{
        AnyType data;
        Node next;
        public Node(AnyType data,Node next){
            this.data=data;
            this.next=next;
        }
    }
    
    //初始化队列
    public void initqueue(){
        first=new Node(null,null);
        last=first;
        size=0;
    }

    //入队
    public void enqueue(AnyType a){
        if(size==0){
            last.data=a;
            size++;
            return;
        }
        Node oldlast=last;
        last=new Node(a,null);
        oldlast.next=last;
        size++;
    }

    //出队
    public AnyType dequeue(){
        if(size==0){
            System.out.print("队列为空");
            System.exit(0);
        }
        AnyType a=first.data;
        first=first.next;
        size--;
        return a;
    }
    public boolean isEmpty(){
        return size==0;
    }
    public int size(){
        return size;
    }
}

2` array queue

public class QueueImplementByArray<AnyType> {
    AnyType[] arr;
    int first;
    int last;
    int size;
    //初始化
    public void ininqueue(int capacity){
        arr=(AnyType[])new Object[capacity];
        first=0;
        last=0;
        size=0;
    }
    public void enqueue(AnyType a){
        if(size==arr.length){
            changeArray(2*size+1);
        }
        arr[last++]=a;
        size++;
    }
    public AnyType dequeue(){
        if(size==0){
            System.out.println("队列为空");
            System.exit(0);
        }
        AnyType a=arr[first++];
        arr[first-1]=null;
        size--;
        return a;
    }
    public void changeArray(int newCapacity){
        AnyType[] newArr=(AnyType[])new Object[newCapacity];
        for(int i=0;i<arr.length;i++){
            newArr[i]=arr[i];
        }
        arr=newArr;
    }
    public boolean isEmpty(){
        return size==0;
    }
    public int size(){
        return size;
    }

}

3` circular queue

public class CycleQueue {
    int[] arr;
    int start;//队首
    int end;//队尾
    int size=0;
    //初始化
    public void initqueue(int size){
        arr=new int[size];
        size=0;
        start=0;
        end=0;
    }

    //入队
    public void enqueue(int num){
        if(size>arr.length){
            System.out.println("队列已满");
            return;
        }
        if(end==arr.length){
            end=0;
        }
        arr[end++]=num;
        size++;
    }

    //出队
    public int dequeue(){
        if(size==0){
            System.out.println("队列为空");
            System.exit(0);
        }
        if(start==arr.length){
            start=0;
        }
        size--;
        return arr[start++];
    }

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

A little summary

Stacks and queues is the basic data structure is a linked list arrays and repackaging and extension. Due to their characteristics and speed of execution stacks and queues are widely used.

Finally, do not in order to use data structures that use a data structure, to distinguish between the use of a variety of scene data structures, flexible use of data structures, you can do more with less.

Guess you like

Origin www.cnblogs.com/sang-bit/p/11757553.html