java实现栈与队列

https://blog.csdn.net/weixin_42490488/article/details/90221357

Queue<Integer> queue = new LinkedList<Integer>();

//java队列应该是这样!
Stack<Character> stack = new Stack<Character>();

//java栈应该是这样!

实例化(栈)

Stack<Integer> stack= new Stack<Integer>();   //Character

判断是否为空

stack.empty()

取栈顶值(不出栈)

stack.peek()

进栈

stack.push(Object);

出栈

stack.pop();

实例化(队列)

Queue<Integer> queue = new LinkedList<Integer>();  //Character

判断是否为空

queue.empty()

取栈顶值(不出栈)

queue.peek()   //queue.element()

进栈

queue.offer(Object);

出栈

queue.poll();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java实现的代码: ```java public class Stack { private int maxSize; private int[] stackArray; private int top; public Stack(int size) { maxSize = size; stackArray = new int[maxSize]; top = -1; } public void push(int value) { if (isFull()) { System.out.println("Stack is full!"); return; } stackArray[++top] = value; } public int pop() { if (isEmpty()) { System.out.println("Stack is empty!"); return -1; } return stackArray[top--]; } public int peek() { if (isEmpty()) { System.out.println("Stack is empty!"); return -1; } return stackArray[top]; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == maxSize - 1); } } ``` Java实现队列的代码: ```java public class Queue { private int maxSize; private int[] queueArray; private int front; private int rear; private int nItems; public Queue(int size) { maxSize = size; queueArray = new int[maxSize]; front = 0; rear = -1; nItems = 0; } public void insert(int value) { if (isFull()) { System.out.println("Queue is full!"); return; } if (rear == maxSize - 1) { rear = -1; } queueArray[++rear] = value; nItems++; } public int remove() { if (isEmpty()) { System.out.println("Queue is empty!"); return -1; } int temp = queueArray[front++]; if (front == maxSize) { front = 0; } nItems--; return temp; } public int peekFront() { if (isEmpty()) { System.out.println("Queue is empty!"); return -1; } return queueArray[front]; } public boolean isEmpty() { return (nItems == 0); } public boolean isFull() { return (nItems == maxSize); } } ``` Java实现数组的代码: ```java public class MyArray { private int[] array; private int length; public MyArray(int[] arr) { array = arr; length = arr.length; } public int get(int index) { if (index < 0 || index >= length) { System.out.println("Invalid index!"); return -1; } return array[index]; } public void set(int index, int value) { if (index < 0 || index >= length) { System.out.println("Invalid index!"); return; } array[index] = value; } public int length() { return length; } public void display() { for (int i = 0; i < length; i++) { System.out.print(array[i] + " "); } System.out.println(); } } ``` Java实现链表的代码: ```java public class Node { public int value; public Node next; public Node(int val) { value = val; next = null; } } public class LinkedList { private Node head; public LinkedList() { head = null; } public void insertFirst(int value) { Node newNode = new Node(value); newNode.next = head; head = newNode; } public Node deleteFirst() { if (isEmpty()) { System.out.println("List is empty!"); return null; } Node temp = head; head = head.next; return temp; } public boolean isEmpty() { return (head == null); } public void display() { Node current = head; while (current != null) { System.out.print(current.value + " "); current = current.next; } System.out.println(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值