栈,队列的实现(Java版)

一、栈(先进后出)

public class Stack{
    public int maxsize;
    public int top=-1;
    public String []arr;
    public Stack(int maxsize){
        this.maxsize=maxsize;
        arr=new String[maxsize];
        top=-1;
    }
    public void push(String str){
        arr[++top]=str;
    }
    public String pop(){
        if(!isEmpty()){
         return arr[top--];
        }
        return null;
    }
    public boolean isEmpty(){
      return (top==-1);
    }
    public boolean isFull(){
    return (top==maxsize-1);
    }
    public void clear(){
     top=-1;
    }
    public String peek(){
        return (arr[top]);
    }

}

二、队列(先进先出)

public class Queue {
    private int front;
    private int rear;
    private int maxSize;
    private String arr[];
    private int curNumber;
    public Queue(int maxSize){
        front=0;
        rear=-1;
        curNumber=0;
        this.maxSize=maxSize;
        arr=new String[maxSize];
    }
    public void insert(String str){
        if(rear==maxSize-1)rear=-1;
        arr[++rear]=str;
        curNumber++;
    }
    public String remove(){
        if(front==maxSize-1)
            front=0;
        curNumber--;
        return arr[front++];
    }
    public boolean isEmpty(){
       return (curNumber==0);
    }
    public boolean isFull(){
        return (curNumber==maxSize);
    }
    public int size(){
     return curNumber;
    }
    public String peekFirst(){
        return arr[front];
    }
}

 

### 队列的基本概念 是一种后进先出(LIFO)的数据结构,而队列是一种先进先出(FIFO)的数据结构。在Java中,可以通过链表来实现队列的功能[^4]。 ### 使用链表实现 可以通过链表来实现,其中链表的头部作为顶,这样可以方便地进行插入和删除操作。以下是一个简单的实现示例: ```java public static class Node<V> { public V value; public Node<V> next; public Node(V val) { this.value = val; } } public static class MyStack<V> { private Node<V> head; private int size; public MyStack() { head = null; size = 0; } public boolean isEmpty() { return size == 0; } public int size() { return size; } public void push(V value) { Node<V> cur = new Node<>(value); cur.next = head; head = cur; size++; } public V pop() { V res = null; if (head != null) { res = head.value; head = head.next; size--; } return res; } public V peek() { V res = null; if (head != null) { res = head.value; } return res; } } ``` ### 使用链表实现队列 队列可以通过链表来实现,其中链表的头部作为队列的前端,链表的尾部作为队列的后端。以下是一个简单的实现示例: ```java static class ListNode { public int val; public ListNode prv; public ListNode next; public ListNode(int val) { this.val = val; } } public ListNode head; public ListNode last; public void offer(int val) { ListNode node = new ListNode(val); if (head == null) { head = last = node; } else { last.next = node; node.prv = last; last = node; } } public int poll() { if (head == null) { return -1; } int val = head.val; head = head.next; if (head != null) { head.prv = null; } else { last = null; } return val; } ``` ### 使用实现队列 可以通过两个实现一个队列。一个用于入队操作,另一个用于出队操作。以下是一个简单的实现示例: ```java class MyStack { Queue<Integer> in; Queue<Integer> out; public MyStack() { in = new LinkedList<>(); out = new LinkedList<>(); } public void push(int x) { in.offer(x); while (!out.isEmpty()) { in.offer(out.poll()); } Queue<Integer> temp = in; in = out; out = temp; } public int pop() { return out.poll(); } public int top() { return out.peek(); } public boolean empty() { return in.isEmpty() && out.isEmpty(); } } ``` ### 相关问题 1. 如何使用实现队列的入队和出队操作? 2. 如何使用链表实现的push和pop操作? 3. 如何使用两个实现一个队列? 4. 链表实现队列如何处理空队列的情况? 5. 如何确保使用实现队列在多线程环境下的线程安全?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值