1. Chain stack
The stack has been implemented with arrays before, and the chain stack can also follow the characteristics of first-in, last-out.
To implement push and pop with linked list, you can consider head insertion method and tail insertion method. But the tail insertion method is not used, because each time the stack is pushed or popped, the stack must be traversed once, which increases the complexity of the program. Therefore, using the head insertion method, the bottom element of the obtained chain stack is the end element of the linked list, and the top element of the stack is the element after the head node.
class LinkStack{ Entry head;//head node 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 LinkStack(){//The constructor of the class this.head = new Entry(); } }
1. Push the stack
public void push(int val){ Entry entry = new Entry(val); entry.next = this.head.next; this.head.next = entry; }
2. Pop the stack
public boolean pop(){ if(head.next != null){//Determine whether the stack is empty this.head.next = this.head.next.next; return true; } return false; }
3. Get the top element of the stack
public int getTop(){ if(this.head.next != null){ return head.next.data; } return -1; }
4. Print the elements in the stack
public void show(){ Entry cur = head.next; while(cur != null){//traverse the linked list to print System.out.print(cur.data+" "); cur = cur.next; } System.out.println(); }
have a test
2. Circular queue
A queue is a first-in, first-out linear data structure that deletes at the head of the queue and inserts at the tail. The head of the queue is represented by front, and the tail of the queue is represented by rear.
Arrays are considered to implement sequential teams, but due to the limited length of the arrays, false overflows may occur as shown in the figure
circular queue
Therefore, the circular queue we constructed should be as follows
Next, let's take a look at the basic operations on queues
class QueueLink{ int[] elem;//Array to store queue elements int front; // team head int rear;//The end of the queue int usedSize = 0;//The number of valid data in the circular queue, that is, the length of the queue int allSize = 10;//Array length public QueueLink(){ this(10); } public QueueLink(int size){ this.elem = new int[size]; this.front = 0; this.rear = 0; } }
1. Judgment of team empty
front = rear, the queue is empty
public boolean isEmpty(){ return front == rear;//Equal to true and not equal to false }
2. Judging that the team is full
front = (rear + 1)%allsize
public boolean isFull(){ if(this.front == (this.rear + 1) % allSize){//There is an idle unit return true; } return false; }
3. Join the team
public void push(int val){ if(isFull()){//The team cannot enter the team when the team is full return; } this.elem[this.rear] = val; this.rear = (this.rear + 1) % allSize;//Can't rear++, rear is always in the range of 0~allsize-1 this.usedSize++;//Queue length plus one }
4. Departure
public void pop(){ if(isEmpty()){//The team is empty and not on the team return; } this.elem[front] = -1;//Give the team's position flag -1, indicating that this element has been dequeued this.front = (this.front + 1)%allSize;//front move one bit back this.usedSize--;//Queue length minus one }
5. Get the head of the team
public int getTop(){ if(isEmpty()){//Judging team empty return -1; } return this.elem[front];//Return the element at the front position }
3. Implement a queue with two stacks
public class TestDemo8 { Stack s1 = new Stack(); Stack s2 = new Stack(); public void pushQue(int val){//Enqueue if(s1.isFull()){ return; } s1.push(val); } public void popQue(){ if(s1.isEmpty()){ return; } while(s1.top > 1){//Put the elements of s2 other than the top of the stack into s2 s2.push(s1.getpop()); s1.top--; } s1.top = 0; while(s2.top>0){//Put the elements of s2 back to s1 s1.push(s2.getpop()); s2.top--; } } public int getFront(){//Get the head of the team if(s1.isEmpty()){ return -1; } return s1.elem[0]; } public void show(){ for(int i = 0;i < s1.top;i++){ System.out.print(s1.elem[i]+" "); } System.out.println(); } public static void main(String[] args) { TestDemo8 t= new TestDemo8(); t.pushQue (5); t.pushQue (6); t.pushQue (7); t.show(); t.popQue(); t.show(); } }