Chain stack---circular queue---implement queue with two stacks

1. Chain stack (implement stack with linked list)

>1, initialize the stack

class Entry{
		int data;
		Entry next;
		public Entry(){
			this.data = -1;
			this.next = null;
		}
		public Entry(int val){
			this.data = val;
			this.next = null;
		}
		
	}
	public Entry head = null;
	public LinkStack(){
		this.head = new Entry();
	}

>2, push the stack

public void push(int val){
		Entry entry = new Entry(val);
		entry.next = head.next;
		head.next = entry;
	}

>3, judge the stack empty

public boolean isEmpty(){
		if(head.next == null)
			return true;
		return false;
	}

>4, pop the stack

public boolean pop(){
		if(isEmpty()){
			return false;
		}
		head.next = head.next.next;
		return true;
	}

>5, get the top element of the stack

public int gettop(){
		if(isEmpty()){
			return -1;
		}
		return head.next.data;
	}

2. Queue

Queue: A linear table that can only perform insertion operations at one end of the table and delete operations at the other end of the table (head-delete-tail insertion)

(Two stacks implement queues)

1, initialize two stacks

Stack s1 = new Stack();
Stack s2 = new Stack();

2. Join the team

Put data into the stack of s1


// enqueue
		public void pushQ(int val){
			if(s1.isFull()){
				return;
			}
			s1.push(val);
		}

3. Departure

Pop the data in the S1 stack to S2, (pay attention to keep the low number of the s1 stack) and then pour s2 into s1, overwriting the element at the low level of the s1 stack. Departure

        ------》

//dequeue
		public void popQ(){
		    if(s1.isEmpty()){  
		        return;  
		    }  
		    while(s1.top > 1){  
		        s2.push(s1.gettop());  
		        s1.top--;  
		    }  
		    s1.top = 0;  
		    while(!s2.isEmpty()){  		  
		        s1.push(s2.gettop());     
		        s2.top--;  
		    }  
		}
3. Get the top element of the team
// get the top element
		public int gettop(){
			return s1.elem[0];
		}


3, circular queue

         Sequential team "deletion and insertion" is prone to " false overflow "

        When the tail pointer has reached the upper bound of the array, there can be no more enqueue operations, but there are still empty positions in the array, which is called "false overflow".

        So we will use --- circular queue --- to solve the false overflow


In the circular queue, the empty queue is characterized by front=rear; when the queue is full, there will also be front=rear;
the solution:

                One unit is wasted artificially, and the full feature of the team is front=(rear+1)%N;

Empty condition: front = rear     
Full condition: front = (rear+1) % N (N=allsize)
Queue length: L=(N+rear-front) % N 


     Circular queue operations -------

1. Initialize

int [] element;
	int front; // team head
	int rear;//The end of the queue
	
	int usedSize = 0;//Number of valid data in the current circular queue
	int allSize = 10;
	
	public QueueLink(){
		this(10);
	}
	public QueueLink(int size){
		this.elem = new int[size];
		this.front = 0;
		this.rear = 0;
	}
2, the team is full
// method to determine whether it is full
	public boolean isFull(){
		if((this.rear+1) % this.allSize == this.front)
			return true;
		return false;
	}

3. Join the team

public void push(int val){
		if(isFull()){
			return;
		}
		this.elem[this.rear] = val;
		this.rear = (this.rear+1) % this.allSize;
		this.usedSize++;
	}

4, the team is empty

public boolean isEmpty(){
		return this.front == this.rear;
	}

5. Departure

//dequeue
	public void pop(){
		if(isEmpty()){
			return;
		}
		this.elem[this.front] = -1;
		this.front = (this.front+1) % this.allSize;
		this.usedSize--;
	}

6. Get the head element

// get the head element
	public int gettop(){
		if(isEmpty()){
			return -1;
		}
		return this.elem[this.front];
	}

7, print queue elements

public void show(){
		for(int i = this.front;i != this.rear;i = (i+1) % this.allSize){
			System.out.print(this.elem[i]+" ");
		}
		System.err.println();
	}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326398587&siteId=291194637