Stack (last out)
Stack method:
- E push (E item) push
- E pop () pop
- E peek () View the top element
- Whether boolean empty () determines stack is empty
Order to achieve stack table
public class MyStack {
private int[] array = new int[100];
array[size++] = v;
public void push(int v) {
array[size++] = v;
}
public int pop() {
return array[--size];
}
public int peek() {
return array[size - 1];
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
}
Queue (last out)
Queue method
Error Handling | Throw an exception | Returns the special value |
---|---|---|
Into the queue | add(e) | offer(e) |
The queue | remove() | poll() |
Team first element | element() | peek() |
Circular queue
Achieve circular queue
class MyCircularQueue {
int[] a;
int front;
int rear;
int size;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
a=new int[k+1];
front=rear=0;
size=k+1;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if(isFull()){
return false;
}
a[rear]=value;
rear=(rear+1)%size;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if(isEmpty()){
return false;
}
front=(front+1)%size;
return true;
}
/** Get the front item from the queue. */
public int Front() {
if(isEmpty()){
return -1;
}
return a[front];
}
/** Get the last item from the queue. */
public int Rear() {
if(isEmpty()){
return -1;
}
return a[(rear-1+size)%size];
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
if(front==rear){
return true;
}
return false;
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
if((rear+1)%size==front){
return true;
}
return false;
}
}