public class Stack {
public Node head;
public Node current;
//方法:入栈操作
public void push(int data) {
if (head == null) {
head = new Node(data);
current = head;
} else {
Node node = new Node(data);
node.pre = current;//current结点将作为当前结点的前驱结点
current = node; //让current结点永远指向新添加的那个结点
}
}
public Node pop() {
if (current == null) {
return null;
}
Node node = current; // current结点是我们要出栈的结点
current = current.pre; //每出栈一个结点后,current后退一位
return node;
}
class Node {
int data;
Node pre; //我们需要知道当前结点的前一个结点
public Node(int data) {
this.data = data;
}
}
public static void main(String[] args) {
Stack stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.pop().data);
System.out.println(stack.pop().data);
System.out.println(stack.pop().data);
}
}
public class Queue {
public Node head;
public Node curent;
//方法:链表中添加结点
public void add(int data) {
if (head == null) {
head = new Node(data);
curent = head;
} else {
curent.next = new Node(data);
curent = curent.next;
}
}
//方法:出队操作
public int pop() throws Exception {
if (head == null) {
throw new Exception("队列为空");
}
Node node = head; //node结点就是我们要出队的结点
head = head.next; //出队之后,head指针向下移
return node.data;
}
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
}
public static void main(String[] args) throws Exception {
Queue queue = new Queue();
//入队操作
for (int i = 0; i < 5; i++) {
queue.add(i);
}
//出队操作
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
}
}
import java.util.Stack;
/**
* Created by smyhvae on 2015/9/9.
*/
public class Queue {
private Stack<Integer> stack1 = new Stack<>();//执行入队操作的栈
private Stack<Integer> stack2 = new Stack<>();//执行出队操作的栈
//方法:给队列增加一个入队的操作
public void push(int data) {
stack1.push(data);
}
//方法:给队列正价一个出队的操作
public int pop() throws Exception {
if (stack2.empty()) {//stack1中的数据放到stack2之前,先要保证stack2里面是空的(要么一开始就是空的,要么是stack2中的数据出完了),不然出队的顺序会乱的,这一点很容易忘
while (!stack1.empty()) {
stack2.push(stack1.pop());//把stack1中的数据出栈,放到stack2中【核心代码】
}
}
if (stack2.empty()) { //stack2为空时,有两种可能:1、一开始,两个栈的数据都是空的;2、stack2中的数据出完了
throw new Exception("队列为空");
}
return stack2.pop();
}
public static void main(String[] args) throws Exception {
Queue queue = new Queue();
queue.push(1);
queue.push(2);
queue.push(3);
System.out.println(queue.pop());
queue.push(4);
System.out.println(queue.pop());
System.out.println(queue.pop());
System.out.println(queue.pop());
}
}
import java.util.ArrayDeque;
import java.util.Queue;
/**
* Created by smyhvae on 2015/9/9.
*/
public class Stack {
Queue<Integer> queue1 = new ArrayDeque<Integer>();
Queue<Integer> queue2 = new ArrayDeque<Integer>();
//方法:入栈操作
public void push(int data) {
queue1.add(data);
}
//方法:出栈操作
public int pop() throws Exception {
int data;
if (queue1.size() == 0) {
throw new Exception("栈为空");
}
while (queue1.size() != 0) {
if (queue1.size() == 1) {
data = queue1.poll();
while (queue2.size() != 0) { //把queue2中的全部数据放到队列一中
queue1.add(queue2.poll());
return data;
}
}
queue2.add(queue1.poll());
}
throw new Exception("栈为空");//不知道这一行的代码是什么意思
}
public static void main(String[] args) throws Exception {
Stack stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.pop());
System.out.println(stack.pop());
stack.push(4);
}
}
import java.util.Stack;
/**
* Created by smyhvae on 2015/9/9.
*/
public class MinStack {
private Stack<Integer> stack = new Stack<Integer>();
private Stack<Integer> minStack = new Stack<Integer>(); //辅助栈:栈顶永远保存stack中当前的最小的元素
public void push(int data) {
stack.push(data); //直接往栈中添加数据
//在辅助栈中需要做判断
if (minStack.size() == 0 || data < minStack.peek()) {
minStack.push(data);
} else {
minStack.add(minStack.peek()); //【核心代码】peek方法返回的是栈顶的元素
}
}
public int pop() throws Exception {
if (stack.size() == 0) {
throw new Exception("栈中为空");
}
int data = stack.pop();
minStack.pop(); //核心代码
return data;
}
public int min() throws Exception {
if (minStack.size() == 0) {
throw new Exception("栈中空了");
}
return minStack.peek();
}
public static void main(String[] args) throws Exception {
MinStack stack = new MinStack();
stack.push(4);
stack.push(3);
stack.push(5);
System.out.println(stack.min());
}
}
import java.util.Stack;
/**
* Created by smyhvae on 2015/9/9.
*/
public class StackTest {
//方法:data1数组的顺序表示入栈的顺序。现在判断data2的这种出栈顺序是否正确
public static boolean sequenseIsPop(int[] data1, int[] data2) {
Stack<Integer> stack = new Stack<Integer>(); //这里需要用到辅助栈
for (int i = 0, j = 0; i < data1.length; i++) {
stack.push(data1[i]);
while (stack.size() > 0 && stack.peek() == data2[j]) {
stack.pop();
j++;
}
}
return stack.size() == 0;
}
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
int[] data1 = {1, 2, 3, 4, 5};
int[] data2 = {4, 5, 3, 2, 1};
int[] data3 = {4, 5, 2, 3, 1};
System.out.println(sequenseIsPop(data1, data2));
System.out.println(sequenseIsPop(data1, data3));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有