Java实现栈和队列,java排序算法面试

top = i;

return result;

}

}

栈的链式存储结构实现:

public class LinkStack {

//链栈的节点

private class Node{

E e;

Node next;

public Node(){}

public Node(E e, Node next){

this.e = e;

this.next = next;

}

}

private Node top; //栈顶元素

private int size; //当前栈大小

public LinkStack(){

top = null;

}

//当前栈大小

public int length(){

return size;

}

//判空

public boolean empty(){

return size==0;

}

//入栈:让top指向新创建的元素,新元素的next引用指向原来的栈顶元素

public boolean push(E e){

top = new Node(e,top);

size ++;

return true;

}

//查看栈顶元素但不删除

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

public Node peek(){

if(empty()){

throw new RuntimeException(“空栈异常!”);

}else{

return top;

}

}

//出栈

public Node pop(){

if(empty()){

throw new RuntimeException(“空栈异常!”);

}else{

Node value = top; //得到栈顶元素

top = top.next; //让top引用指向原栈顶元素的下一个元素

value.next = null; //释放原栈顶元素的next引用

size --;

return value;

}

}

}

基于LinkedList实现的栈结构:

import java.util.LinkedList;

/**

  • 基于LinkedList实现栈

  • 在LinkedList实力中只选择部分基于栈实现的接口

*/

public class StackList {

private LinkedList ll = new LinkedList();

//入栈

public void push(E e){

ll.addFirst(e);

}

//查看栈顶元素但不移除

public E peek(){

return ll.getFirst();

}

//出栈

public E pop(){

return ll.removeFirst();

}

//判空

public boolean empty(){

return ll.isEmpty();

}

//打印栈元素

public String toString(){

return ll.toString();

}

}

队列的顺序存储结构实现

public class Queue {

private Object[] data=null;

private int maxSize; //队列容量

private int front; //队列头,允许删除

private int rear; //队列尾,允许插入

//构造函数

public Queue(){

this(10);

}

public Queue(int initialSize){

if(initialSize >=0){

this.maxSize = initialSize;

data = new Object[initialSize];

front = rear =0;

}else{

throw new RuntimeException(“初始化大小不能小于0:” + initialSize);

}

}

//判空

public boolean empty(){

return rear==front?true:false;

}

//插入

public boolean add(E e){

if(rear== maxSize){

throw new RuntimeException(“队列已满,无法插入新的元素!”);

}else{

data[rear++]=e;

return true;

}

}

//返回队首元素,但不删除

public E peek(){

if(empty()){

throw new RuntimeException(“空队列异常!”);

}else{

return (E) data[front];

}

}

//出队

public E poll(){

if(empty()){

throw new RuntimeException(“空队列异常!”);

}else{

E value = (E) data[front]; //保留队列的front端的元素的值

data[front++] = null; //释放队列的front端的元素

return value;

}

}

//队列长度

public int length(){

return rear-front;

}

}

循环队列的顺序存储结构实现

import java.util.Arrays;

public class LoopQueue {

public Object[] data = null;

private int maxSize; // 队列容量

private int rear;// 队列尾,允许插入

private int front;// 队列头,允许删除

private int size=0; //队列当前长度

public LoopQueue() {

this(10);

}

public LoopQueue(int initialSize) {

if (initialSize >= 0) {

this.maxSize = initialSize;

data = new Object[initialSize];

front = rear = 0;

} else {

throw new RuntimeException(“初始化大小不能小于0:” + initialSize);

}

}

// 判空

public boolean empty() {

return size == 0;

}

// 插入

public boolean add(E e) {

if (size == maxSize) {

throw new RuntimeException(“队列已满,无法插入新的元素!”);

} else {

data[rear] = e;

rear = (rear + 1)%maxSize;

size ++;

return true;

}

}

// 返回队首元素,但不删除

public E peek() {

if (empty()) {

throw new RuntimeException(“空队列异常!”);

} else {

return (E) data[front];

}

}

// 出队

public E poll() {

if (empty()) {

throw new RuntimeException(“空队列异常!”);

} else {

E value = (E) data[front]; // 保留队列的front端的元素的值

data[front] = null; // 释放队列的front端的元素

front = (front+1)%maxSize; //队首指针加1

size–;

return value;

}

}

// 队列长度

public int length() {

return size;

}

//清空循环队列

public void clear(){

Arrays.fill(data, null);

size = 0;

front = 0;

rear = 0;

}

}

队列的链式存储结构实现

public class LinkQueue {

// 链栈的节点

private class Node {

E e;

Node next;

public Node() {

}

public Node(E e, Node next) {

this.e = e;

this.next = next;

}

}

private Node front;// 队列头,允许删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值