java链队列图解_顺序栈,链栈,队列java实现

顺序栈

/**

* 顺序栈

* */

public class SqStack {

//栈的大小

private int maxSize;

//栈顶指针

private int top;

private char[] stack;

public SqStack(int size){

maxSize = size;

top = -1;

stack = new char[maxSize];

}

//压栈

public void push(char value){

stack[++top] = value;

}

//出栈

public char pop(){

return stack[top--];

}

//返回栈顶指针

public char peek(){

return stack[top];

}

//栈是否满

public boolean idFull(){

return maxSize-1==top;

}

//栈是否为空

public boolean isEmpty(){

return top==-1;

}

}

测试

SqStack sqStack = new SqStack(10);

sqStack.push('a');

sqStack.push('b');

System.out.println(sqStack.pop());

System.out.println(sqStack.pop());

输出

1555039e7908005e6818a5993ab1b6e7.png

链栈

import sun.awt.image.ImageWatched;

/**

* 链栈

* */

public class LinkStack {

/**

* @param size 栈的大小

* @param top 栈顶指针

* */

private int size;

private Node top =null;

class Node{

int data;

Node next = null;

public Node(int data){

this.data = data;

}

}

/**

* 压栈

* @param data 节点数据

* */

public void push(int data){

Node node = new Node(data);

node.next = top;

top = node;

size++;

}

/**

* 出栈

* */

public int pop()throws Exception{

if(top==null)

throw new Exception("空的");

else {

int data = top.data;

top = top.next;

size--;

return data;

}

}

public int getSize(){

return size;

}

public boolean isEmpty(){

return size==0;

}

public void showAllNode() throws Exception{

if(top==null)

throw new Exception("空栈");

else {

Node node = top;

while (node!=null){

System.out.println(node.data);

node = node.next;

}

}

}

}

测试

LinkStack stack = new LinkStack();

stack.push(2);

stack.push(3);

stack.showAllNode();

结果

33aa83ab82c1c2eeafc28f17d6692a05.png

队列

点类

public class Node {

//存储的数据

private T data;

//下一个节点

private Node next;

public Node(T data){

this.data = data;

}

public T getData() {

return data;

}

public void setData(T data) {

this.data = data;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

队列类

public class LinkQuene {

//头

private Node front;

//尾

private Node rear;

//大小

private int size;

/**

* 创建队列

* */

public LinkQuene(){

front = rear = null;

}

/**

* 入列

* @param data 节点数据

* */

public void enter(Integer data){

Node node = new Node<>(data);

if(isEmpty()){

front = rear = node;

}else{

rear.setNext(node);

rear = node;

}

size++;

}

/**

* 出列

* */

public Integer out(){

Node node = new Node(-1);

if(isEmpty()){

System.out.println("队列是空的");

return (Integer) node.getData();

}else {

node = front;

front = node.getNext();

node.setNext(null);

size--;

}

if(size==0){

front = null;

rear = null;

}

return (Integer) node.getData();

}

/**

* 判断是否为空

* */

public boolean isEmpty(){

return front==null&&rear==null?true:false;

}

/**

* 得到个数

* */

public int getSize(){

return this.size;

}

}

测试:

LinkQuene quene = new LinkQuene();

quene.enter(1);

quene.enter(2);

System.out.println(quene.getSize());

System.out.println(quene.out());

System.out.println(quene.getSize());

结果

c9718edc2e0d64e891aeee8d4de1a381.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值