栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

栈和队列的实现及简单应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

栈和队列的实现及简单应用

一.栈         1)栈的实现
//用链表实现栈(链式储存结构)
public class LLNode {
    public static void main(String[] args) {
        LLStack llStack = new LLStack();
        llStack.push(1);
        llStack.push(2);
        llStack.push(3);
        System.out.println("栈里面的个数为" + llStack.getLength());
        llStack.pop();
        System.out.println("出栈一个后,栈顶的值为" + llStack.top());
    }
    private Object data;
    private LLNode next;
    public LLNode(){

    }
    public LLNode(Object data){
        this.data = data;
    }

    public Object getData(){
        return data;
    }
    public void setData(Object data){
        this.data = data;
    }
    public LLNode getNext(){
        return next;
    }
    public void setNext(LLNode next){
        this.next = next;
    }
}

//实现栈的类
 class LLStack{
    LLNode headnode = null;
    public LLStack(){
        headnode = new LLNode(null);
    }
    public  boolean isEmpty(){
        return headnode==null;
    }
    //入栈
    public void push(Object data){
        if(headnode.getData() == null){//头结点为空时
            headnode.setData(data);
        }else if(headnode == null){
            headnode = new LLNode(data);
        }else{
            LLNode newnode = new LLNode(data);
            newnode.setNext(headnode);
            headnode = newnode;
        }
    }
    //出栈
    public Object pop(){
        Object data; //= null;
        if(isEmpty()){
            System.out.println("栈为空");
            return 0;
        }
        data = headnode.getData();
        headnode = headnode.getNext();
        return data;
    }
    //返回栈顶的值
    public Object top(){
        Object data = null;
        if(isEmpty()){
            System.out.println("栈为空");
            return 0;
        }
        data = headnode.getData();
        return data;
    }
   //找到栈里面的个数
   public int getLength(){
        int count = 0;
        LLNode tempnode=headnode;
        if(isEmpty()||tempnode.getData()==null){
            count = 0;
        }else{
            while(tempnode!=null){
                count++;
                System.out.printf("栈的第%d个值为%dn",count,tempnode.getData());
                tempnode=tempnode.getNext();
            }
        }
        return  count;
   }
}
//用数组实现栈(顺序储存结构)
public class ArrayStackDemo {
    public static void main(String[] args) {
         ArrayStack stack = new ArrayStack(4);
            String key ="";
            boolean loop = true;//控制是否退出菜单
            Scanner scanner = new Scanner(System.in);

            while(loop) {
                key = scanner.next();
                switch (key) {
                    case "show":
                        stack.list();
                        break;
                    case "push":

                        int value = scanner.nextInt();
                        stack.push(value);

                        break;
                    case "pop":
                        try {
                            int res = stack.pop();
                            System.out.printf("出栈的数据为%dn", res);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        break;
                    case "exit":
                        scanner.close();//scanner是一个流
                        loop = false;
                        break;
                    default:
                        break;
                }
            }
        System.out.println("程序退出");
    }
}
class ArrayStack{
    private int maxSize;//栈的大小
    private int[] stack;//用数组模拟栈
    private  int top = -1;//top表示栈顶,初始化为-1

    //构造器
    public ArrayStack(int maxSize){
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }
    //栈满
    public boolean isFull(){
        return top == maxSize-1;
    }
    //栈空
    public boolean isEmpty(){
        return top == -1;
    }
    //入栈
    public void push(int value){
        if(isFull()){
            return;
        }
        stack[++top] = value;
    }
    //出栈
    public int  pop(){
        if(isEmpty()){
            throw  new RuntimeException("栈空,没有数据");
        }
        int value = stack[top];
        top--;
        return value;
    }
    //遍历栈,ps:从栈顶开始显示数据
    public void list(){
        if(isEmpty()){
            System.out.println("栈空,无数据");
            return;
        }
        for(int i = top;i>=0;i--){
            System.out.printf("stack[%d]=%dn",i,stack[i]);
        }
    }
}
        2)栈的应用

逆波兰表达式

二.队列         1)队列的实现

                

1.链队列的结构  
2.typedef struct QNode  
3.{  
4.    int data;  
5.    struct QNode *next;  
6.}QNode,*QueuePtr;  
7.  
8.typedef struct  
9.{  
10.    QueuePtr front,rear;  
11.}LinkQueue;  
12.  
13.入队操作:  
14.int EnQueue (LinkQueue *Q,int e)//EnQueue:元素  
15.{  
16.    QueuePtr s=(QueuePtr)malloc(sizeof(QNode));  
17.    s->data=e;  
18.    s->next=NULL;  
19.    Q->rear->next=s;  
20.    Q->rear=s;  
21.    return;  
22.}  
23.  
24.出队操作:  
25. int EnQueue (LinkQueue *Q,int *e)  
26. {  
27.    QueuePtr p;  
28.     if(Q->front==Q->rear)  
29.     {  
30.        return ERROR;  
31.    }   
32.    p=Q->front->next;//将想删去的结点暂存给P  
33.    *e=p->data;  
34.    Q->front->next=p->next;  
35.    if(Q->rear==p)//队头是队尾,则删除后将rear指向头结点   
36.    {  
37.        Q->rear=Q->front;  
38.    }  
39.    free(p);   
40.    return;  
41. } 
2)应用 

思路:定义队列,存放请求的时刻

每次发起请求时,先将新的请求时刻t入队

再循环判断队头的数,是否小于t-3000,若小于,队头出队。这样就可以将3000 ms以外的时刻全部去除

循环完毕,返回队列长度

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891630.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号