1.用基础类型实现栈,代码如下
package com.guozz.testdatastructor.stack;
public class Node {
int data;
Node pre; //当前节点的前一个节点
public Node(int data) {
this.data = data;
}
}
// package com.guozz.testdatastructor.stack;
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;
}
}
package com.guozz.testdatastructor.stack;
public class TestClient {
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);
}
}
2.用java实现链表
package com.guozz.testdatastructor.queue;
public class Node {
int data;
Node next;
public Node(int data){
this.data=data;
}
}
package com.guozz.testdatastructor.queue;
public class Queue {
public Node head;
public Node current;
//向链表中添加节点
public void add(int data){
if(head ==null){
head=new Node(data);
current=head;
}else{
current.next=new Node(data);
current=current.next;
}
}
public int pop() throws Exception{
if(head ==null){
throw new Exception("队列为空");
}
Node node=head;
//出队之后,指针向下移
head=head.next;
return node.data;
}
}
package com.guozz.testdatastructor.queue;
public class QueueClient {
public static void main(String[] args) throws Exception {
Queue queue =new Queue();
//入队操作
for(int i=0;i<5;i++){
queue.add(i);
}
System.err.println(queue.pop());
System.err.println(queue.pop());
System.err.println(queue.pop());
}
}
3.用java.util下的两个链表实现一个栈
思路:
将1,2,3依次入队列一,然后将最上面的3留在队列一,将下面的1,2入队列二,
将3出队列一,此时队列一空了,然后把队列二中的所有数据入队列一;
将最上面的2留在队列一,将下面的1入队列二。。依次循环
package com.guozz.testdatastructor.stackcombine;
import java.util.ArrayDeque;
import java.util.Queue;
public class StackCombine {
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中的数据全部放到队列1中
queue1.add(queue2.poll());
}
return data;
}
queue2.add(queue1.poll());
}
throw new Exception("栈为空");
}
}
package com.guozz.testdatastructor.stackcombine;
public class StackCombineClient {
public static void main(String[] args) throws Exception {
StackCombine stackCombine = new StackCombine();
stackCombine.push(1);
stackCombine.push(2);
stackCombine.push(3);
System.out.println(stackCombine.pop());
System.out.println(stackCombine.pop());
System.out.println(stackCombine.pop());
stackCombine.push(4);
System.out.println(stackCombine.pop());
}
}
4.用java.util下的连个栈实现一个链表
思路:
栈1用于存储元素,栈2用于弹出元素,负负得正
说得通俗一点,现在把数据1,2,3分别放入栈1,然后从栈1中出来3,2,1
放到栈2中,那么栈2中出来的数据1,2,3就符合队列的规律了,即负负得正
package com.guozz.testdatastructor.queuecombine;
import java.util.Stack;
public class Queue {
private Stack<Integer> stack1 =new Stack<Integer>();//执行入栈操作的栈
private Stack<Integer> stack2 =new Stack<Integer>();//执行出栈操作的栈
//入栈操作
public void push(int data){
stack1.push(data);
}
//方法:给队列正价一个出队的操作
public int pop() throws Exception{
if(stack2.empty()){
//stack1中的数据放到stack2之前,要保证stack2里面是空的(要么一开始就是空,要么是stack2中的数据出完了)
//不然出队的顺序是乱的,这一点很重要
while (!stack1.empty()) {
//把stack1中的数据出栈,放到stack2中
stack2.push(stack1.pop());
}
}
if(stack2.empty()){
//stack2为空时,有两种情况
//1.一开始两个栈的数据都是空的,
//2.stack2中的数据出完了
throw new Exception("队列为空");
}
return stack2.pop();
}
}
package com.guozz.testdatastructor.queuecombine;
public class QueueCombineClient {
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());
}
}