栈
后进先出原则;
使用顺序表尾插的方式,实现栈,
代码如下:
public class MyStack {
private int[] elem;
private int top;
public MyStack(){
this.elem = new int[10];//定义顺序表
}
private boolean isFull(){//是否满了
if(this.top == this.elem.length ){//top值等于数组长度
return true;
}else{
return false;
}
}
public void push(int data){//入栈
if(isFull()){
throw new UnsupportedOperationException("数组为空");
}
this.elem[this.top++] = data;
}
private boolean isEmpty(){
if(this.top == 0){
return true;
}
return false;
}
public int pop(){//出栈
if(isEmpty()){
throw new UnsupportedOperationException("数组为空");
}
return this.elem[--this.top];
}
public int peek(){//获取头元素
if(isEmpty()){
throw new UnsupportedOperationException("数组为空");
}
return this.elem[this.top-1];
}
public int size(){//长度
return this.top;
}
}
队列
先进先出原则
使用链表的方式实现,
代码如下:
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
}
}
public class MyQueue {
public Node head;//头节点
public Node tail;//尾节点
public int size;
//入队
public void offer(int data) {
//第一次入队
Node node = new Node(data);
if(this.head == null) {
if(tail == null){
this.head = node;//直接放
}
}else {
tail.next = node;//尾插
}
tail = node;
size++;
}
//出队
public int poll() {
if (size == 0) {
throw new RuntimeException("队列为空");
}
Node oldHead = head;
head = head.next;//从头出
if (head == null) {
tail = null;
}
size--;
return oldHead.data;
}
//得到队列的队头元素 并且不删除
public int peek() {
if (size == 0) {
throw new RuntimeException("队列为空");
}
return head.data;//头节点的值
}
//长度
public int size() {
return size;
}
}
循环队列
环形结构
代码实现如下:
public class myQueueCircle {
public int[] elem;//定义数组
public int front;//定义头尾指针
public int rear;
public myQueueCircle(int k){
this.elem = new int[k];//如果尾部不空余,则是k+1
this.front = 0;
this.rear = 0;
}
public boolean isFull(){
if((this.rear+1)%this.elem.length==this.front){
return true;
}
return false;
}
//入队列
public boolean enQueue(int value){
if(isFull()){
return false;
}
this.elem[this.rear] = value;
this.rear = (this.rear+1)%this.elem.length;
return true;
}
public boolean isEmpty(){
if(this.front == this.rear){
return true;
}
return false;
}
//出队列
public boolean deQoueue(){
if(isEmpty()){
return false;
}
this.front = (this.front + 1)%this.elem.length;
return true;
}
//得到队头元素
public int Front(){
if(isEmpty()){
return -1;
}
return this.elem[front];
}
//得到队尾元素
public int Rear(){
if(isEmpty()){
return -1;
}
int index = 0;
if(this.rear == 0){
index = this.elem.length-1;
}else{
index = this.rear-1;
}
return this.elem[index];
}
}
队列实现栈
代码如下:
class MyStack {
public Queue<Integer> queue1;//定义两个队列
public Queue<Integer> queue2;
/** 初始化. */
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {//入栈
//第一次入栈,就选择queue1,如果不是第一次,则选择不为空的队列入栈
if(! queue1.isEmpty()) {
queue1.offer(x);
}else if(!queue2.isEmpty()){
queue2.offer(x);
}else{
queue1.offer(x);//都为空
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {//出栈
if(empty()){
return -1;//为空返回-1
}
int oldDate = 0;
if(!queue1.isEmpty()){//队列1不为空
int size = queue1.size();
for (int i = 0; i < size-1; i++) {
queue2.offer(queue1.poll());队列1出到队列2中
}
oldDate = queue1.poll();//最后一个值就是栈第一个要出栈的值
}else if(!queue2.isEmpty()){//同理,队列2不为空也是如此
int size = queue2.size();
for (int i = 0; i < size - 1; i++) {
queue1.offer(queue2.poll());
}
oldDate = queue2.poll();
}
return oldDate;
}
/** Get the top element. */
public int top() {
if(empty()){
return -1;
}
int oldDate = 0;
if(!queue1.isEmpty()){
int size = queue1.size();
for (int i = 0; i < size; i++) {
oldDate = queue1.poll();
queue2.offer(oldDate);//队列1最后出来的元素就是栈顶元素
}
}else if(!queue2.isEmpty()){
int size = queue2.size();
for (int i = 0; i < size; i++) {
oldDate = queue2.poll();
queue1.offer(oldDate);
}
}
return oldDate;
}
/** Returns whether the stack is empty. */
public boolean empty() {
if(queue1.isEmpty() && queue2.isEmpty()){
return true;
}
return false;
}
}
栈实现队列
代码如下:
class MyQueue {
public Stack<Integer> stack;//定义两个栈
public Stack<Integer> stackTmp;
/** Initialize your data structure here. */
public MyQueue() {
stack = new Stack<>();
stackTmp = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
//入队列就把值入栈
stack.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {//出栈
if(stackTmp.empty()) {
//将stack 栈当中的所有元素全部倒入 stackTmp;
while(!stack.empty()){
stackTmp.push(stack.pop());
}
}
if(!stackTmp.empty()) {
//将stackTmp 的栈顶元素弹出-》出栈stackTmp.pop
return stackTmp.pop();//满足先进先出的原则,stack中先进入的元素就是队列先出的元素
}
return -1;
}
/** Get the front element. */
public int peek() {
if ((stackTmp.empty())) {
//将stack中的所有元素全部倒入stackTmp
while (!stack.empty()) {
stackTmp.push(stack.pop());
}
}
if(!stackTmp.empty()){
//将stackTmp的栈顶元素弹出,,,出栈
return stackTmp.peek();//第一个进入stack的元素,就是stackTmp的栈顶元素,也是队列的顶元素
}
return -1;
}
/** Returns whether the queue is empty. */
public boolean empty() {
if(stack.empty()&&stackTmp.empty()){
return true;
}
return false;
}
}
最小栈
代码如下:
class MinStack {
public Stack<Integer> stack;
public Stack<Integer> minStack;
/**
* initialize your data structure here.
*/
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
//1、stack栈内一定会入栈
//2、第一次minStack一定会放元素,其余的每次
//往stack栈-》和minStack栈顶比较
// x <= minStack.peek()--->minStack.push(x);
stack.push(x);
if (minStack.empty()) {
minStack.push(x);
} else {
if (x <= minStack.peek()) {
minStack.push(x);
}
}
}
public void pop() {
if (stack.empty()) {
return;
}
int num = stack.pop();
if (num == minStack.peek()) {
minStack.pop();
}
}
public int top() {
if (stack.empty()) {
return -1;
}
return stack.peek();
}
public int getMin() {
if (minStack.empty()) {
return -1;
}
return minStack.peek();
}
}