数据结构基础——队列

在这里插入图片描述
在这里插入图片描述

import java.util.Scanner;

public class Main {

  static String Vqueue[] = new String[1000]; // V队列
  static int Vhead = 0; // 首指针
  static int Vtail = 0; // 尾指针

  static String Nqueue[] = new String[1000]; // N队列
  static int Nhead = 0; // 首指针
  static int Ntail = 0; // 尾指针

  static void in(String name, String type) {
      if (type.contains( "V")) {
          Vqueue[Vtail] = name;
          Vtail++;
      } else {
          Nqueue[Ntail] = name;
          Ntail++;
      }
  }

  static boolean out(String type) {

      if (type.contains( "V")) {
          if (Vhead == Vtail) {
              // 队伍没有人不能在出队了。
              return false;
          } else {
              Vhead++;// head前的数据都是无效数据,无需删除,逻辑明确即可。
              return true;
          }

      } else {
          if (Nhead == Ntail) {
              // 队伍没有人不能在出队了。
              return false;
          } else {
              Nhead++;// head前的数据都是无效数据,无需删除,逻辑明确即可。
              return true;
          }
      }
  }

  static String getHead(String type) {

      if (type.contains( "V")) {
          return Vqueue[Vhead];
      } else {
          return Nqueue[Nhead];
      }
  }

  public static void main(String[] args) {
      int M;
      Scanner in=new Scanner(System.in);
      M=in.nextInt();
      while(M>0) //
      {
          M--;
          String op,name,type;
          op=in.next();
          // System.out.println("op"+op);
          if(op.contains("IN"))
          {
              name=in.next();
              type=in.next();
              in(name,type);
              // System.out.println("name:"+name+"type:"+type);

              // System.out.println(Vqueue);
          }
          else
          {
              type=in.next();
              out(type);
              // System.out.println("type"+type);
          }
      }
      // System.out.println(Nhead);
      String s=getHead("V");
      while(out("V"))
      {
          System.out.println(s);
          s=getHead("V");
      }
      s=getHead("N");
      while(out("N"))
      {
          System.out.println(s);
          s=getHead("N");
      }
  }

}

在这里插入图片描述

import java.util.Scanner;

public class Main {


   static int QueueSize=10005;
   static String Vqueue[] = new String[QueueSize]; // V队列
   static int Vhead = 0; // 首指针
   static int Vtail = 0; // 尾指针

   static String Nqueue[] = new String[QueueSize]; // N队列
   static int Nhead = 0; // 首指针
   static int Ntail = 0; // 尾指针

   static boolean in(String name, String type) {

       if(type.contains("V")){
           if ((Vtail+1) % QueueSize ==Vhead) return false;
               //队列以达到容量上限满了,所以不能再插入了返回错误;
           else{
               Vtail=(Vtail+1) % QueueSize;
               Vqueue[Vtail]=name;
               return true;
           }
       }
       else
       {
           if ((Ntail+1) % QueueSize ==Nhead) return false;
               //队列以达到容量上限满了,所以不能再插入了返回错误;
           else{
               Ntail=(Ntail+1) % QueueSize;
               Nqueue[Ntail]=name;
               return true;
           }
       }
   }

   static boolean out(String type) {

       if(type.contains("V")){
           if (Vtail==Vhead) return false;
               //空队列不能出队列了

           else {
               Vhead=(Vhead+1) % QueueSize;
               //不是空队列,但是因为是循环的,如果到了数组末尾也要调整到前面去。
               return true;
           }

       }
       else
       {
           if (Ntail==Nhead) return false;
               //空队列不能出队列了

           else {
               Nhead=(Nhead+1) % QueueSize;
               //不是空队列,但是因为是循环的,如果到了数组末尾也要调整到前面去。
               return true;
           }

       }
   }

   static String getHead(String type) {

       if(type.contains("V")){
           if (Vtail==Vhead) return "";//空队列返回空
           else {
               return Vqueue[Vhead+1];
           }
       }
       else
       {
           if (Ntail==Nhead) return "";//空队列返回空
           else {
               return Nqueue[Nhead+1];
           }
       }
   }

   public static void main(String[] args) {
       int M;
       Scanner in=new Scanner(System.in);
       M=in.nextInt();
       while(M>0) //
       {
           M--;
           String op,name,type;
           op=in.next();
           // System.out.println("op"+op);
           if(op.contains("IN"))
           {
               name=in.next();
               type=in.next();
               in(name,type);
               // System.out.println("name:"+name+"type:"+type);

               // System.out.println(Vqueue);
           }
           else
           {
               type=in.next();
               out(type);
               // System.out.println("type"+type);
           }
       }
       // System.out.println(Nhead);
       String s=getHead("V");
       while(out("V"))
       {
           System.out.println(s);
           s=getHead("V");
       }
       s=getHead("N");

       while(out("N"))
       {
           System.out.println(s);
           s=getHead("N");
       }
   }

}

本次实验,我们学习了普通队列和循环队列两种队列的实现方式,了解了队列的原理与基本的实现方式。随着我们课程的深入,我们只需要掌握原理即可,这些工具和数据结构,我们都不会再自己定义使用,而是直接使用各类编程语言已经写好的库模板。

另外,大家从我们上面写过的代码可以看出,其中设置了很多输出,用于调试代码,满是代码调试的痕迹。其实每个人写代码都不是一蹴而就的,在后续的学习中,希望大家在觉得复杂困难的部分,要想办法解决,不要因为困难就放弃。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值