{"id":203563,"date":"2025-05-22T13:43:47","date_gmt":"2025-05-22T05:43:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203563\/"},"modified":"2025-05-22T13:43:47","modified_gmt":"2025-05-22T05:43:47","slug":"%e5%b0%9d%e8%af%95%e5%90%91-queue-%e7%b1%bb%e6%b7%bb%e5%8a%a0%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203563\/","title":{"rendered":"\u5c1d\u8bd5\u5411 Queue \u7c7b\u6dfb\u52a0\u5f02\u5e38"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5c1d\u8bd5\u5411 Queue \u7c7b\u6dfb\u52a0\u5f02\u5e38<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5c1d\u8bd5\u5411 Queue \u7c7b\u6dfb\u52a0\u5f02\u5e38\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/173137072967329ee91f037.jpg\" class=\"aligncenter\"><\/p>\n<p>\u7ec3\u4e60\u6587\u4ef6\uff1a<br \/> queuefullexception.java<br \/> queueemptyexception.java<br \/> \u56fa\u5b9a\u961f\u5217.java<br \/> qexcdemo.java<\/p>\n<p>\u5728\u8fd9\u4e2a\u9879\u76ee\u4e2d\uff0c\u4e3a\u961f\u5217\u7c7b\uff08queue\uff09\u521b\u5efa\u4e86\u4e24\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u5206\u522b\u6307\u793a\u6ee1\u961f\u5217\u548c\u7a7a\u961f\u5217\u7684\u9519\u8bef\u60c5\u51b5\u3002\u8fd9\u4e9b\u5f02\u5e38\u7531 put() \u548c get() \u65b9\u6cd5\u4f7f\u7528\u3002<\/p>\n<p><strong>\u961f\u5217\u5f02\u5e38\uff1a<\/strong><\/p>\n<ul>\n<li>queuefullexception\uff1a\u5c1d\u8bd5\u5c06\u5143\u7d20\u63d2\u5165\u5b8c\u6574\u961f\u5217\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li>\u8be5\u7c7b\u5305\u542b\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u6700\u5927\u961f\u5217\u5927\u5c0f\u7684\u5b57\u6bb5\uff0c\u5e76\u91cd\u5199 tostring() \u65b9\u6cd5\u4ee5\u663e\u793a\u81ea\u5b9a\u4e49\u6d88\u606f\u3002<\/li>\n<li>queueemptyexception\uff1a\u5c1d\u8bd5\u4ece\u7a7a\u961f\u5217\u4e2d\u5220\u9664\u5143\u7d20\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li>\u8be5\u7c7b\u8fd8\u91cd\u5199 tostring() \u4ee5\u5728\u961f\u5217\u4e3a\u7a7a\u65f6\u663e\u793a\u6d88\u606f\u3002<\/li>\n<\/ul>\n<p><strong>fixedqueue \u7c7b\u5b9e\u73b0\uff1a<\/strong><\/p>\n<ul>\n<li>fixedqueue \u7c7b\u5df2\u4fee\u6539\u4e3a\u5728\u53d1\u751f\u9519\u8bef\u60c5\u51b5\u65f6\u629b\u51fa queuefullexception \u548c queueemptyexception\u3002<\/li>\n<li>\u4e3a\u6b64\uff0cput() \u548c get() \u5728\u5176\u7b7e\u540d\u4e2d\u5305\u542b\u4e00\u4e2a throws \u5b50\u53e5\u3002<\/li>\n<li>\u901a\u8fc7\u629b\u51fa\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u8ba9\u8c03\u7528\u4ee3\u7801\u66f4\u6709\u6548\u5730\u5904\u7406\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><strong>\u5f02\u5e38\u548cfixedqueue\u7c7b\u4ee3\u7801\uff1a<\/strong><br \/> queuefullexception.java<\/p>\n<pre>public class queuefullexception extends exception {\n  int size;\n  queuefullexception(int s) { size = s; }\n  public string tostring() {\n    return \"\\nqueue is full. maximum size is \" + size;\n  }\n}\n\n<\/pre>\n<p>queueemptyexception.java:<\/p>\n<pre>public class queueemptyexception extends exception {\n  public string tostring() {\n    return \"\\nqueue is empty.\";\n  }\n}\n\n<\/pre>\n<p>fixedqueue.java\uff1a<\/p>\n<pre>class fixedqueue implements icharq {\n  private char q[];\n  private int putloc, getloc;\n\n  public fixedqueue(int size) {\n    q = new char[size];\n    putloc = getloc = 0;\n  }\n\n  public void put(char ch) throws queuefullexception {\n    if (putloc == q.length)\n      throw new queuefullexception(q.length);\n    q[putloc++] = ch;\n  }\n\n  public char get() throws queueemptyexception {\n    if (getloc == putloc)\n      throw new queueemptyexception();\n    return q[getloc++];\n  }\n}\n\n<\/pre>\n<p><strong>\u4f7f\u7528 qexcdemo \u8fdb\u884c\u6d4b\u8bd5\uff1a<\/strong><br \/> qexcdemo\u7c7b\u6a21\u62df\u961f\u5217\u7684\u4f7f\u7528\uff1a<br \/> \u63d2\u5165\u5143\u7d20\u76f4\u5230\u8d85\u8fc7\u9650\u5236\uff0c\u629b\u51fa queuefullexception\u3002<br \/> \u5b83\u5c1d\u8bd5\u901a\u8fc7\u629b\u51fa queueemptyexception \u4ece\u7a7a\u961f\u5217\u4e2d\u5220\u9664\u5143\u7d20\u3002<\/p>\n<pre>class qexcdemo {\n  public static void main(string args[]) {\n    fixedqueue q = new fixedqueue(10);\n    char ch;\n    int i;\n    try {\n      for(i=0; i &lt; 11; i++) {\n        system.out.print(\"attempting to store : \" + (char) ('a' + i));\n        q.put((char) ('a' + i));\n        system.out.println(\" - ok\");\n      }\n    } catch (queuefullexception exc) {\n      system.out.println(exc);\n    }\n\n    try {\n      for(i=0; i &lt; 11; i++) {\n        system.out.print(\"getting next char: \");\n        ch = q.get();\n        system.out.println(ch);\n      }\n    } catch (queueemptyexception exc) {\n      system.out.println(exc);\n    }\n  }\n}\n\n<\/pre>\n<p><strong>\u66f4\u65b0\u4e86 icharq \u754c\u9762\uff1a<\/strong><br \/> icharq \u73b0\u5728\u5728 put() \u548c get() \u65b9\u6cd5\u4e2d\u5305\u542b\u629b\u51fa\u5f02\u5e38\uff0c\u53cd\u6620\u4e86\u56fa\u5b9a\u961f\u5217\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/p>\n<pre>public interface ICharQ {\n  void put(char ch) throws QueueFullException;\n  char get() throws QueueEmptyException;\n}\n\n<\/pre>\n<p><strong>\u9884\u671f\u8f93\u51fa\uff1a<\/strong><br \/> \u7a0b\u5e8f\u4f1a\u663e\u793a\u6307\u793a\u5143\u7d20\u63d2\u5165\u548c\u5220\u9664\u6210\u529f\u7684\u6d88\u606f\uff0c\u4ee5\u53ca\u9519\u8bef\u6d88\u606f\uff1a<br \/> \u961f\u5217\u5df2\u6ee1\u3002\u5f53\u961f\u5217\u5df2\u6ee1\u65f6\uff0c\u6700\u5927\u5927\u5c0f\u4e3a 10\u3002<br \/> \u961f\u5217\u4e3a\u7a7a\u3002\u5f53\u5c1d\u8bd5\u4ece\u7a7a\u961f\u5217\u4e2d\u5220\u9664\u5143\u7d20\u65f6\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5c1d\u8bd5\u5411 Queue \u7c7b\u6dfb\u52a0\u5f02\u5e38\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c1d\u8bd5\u5411 Queue \u7c7b\u6dfb\u52a0\u5f02\u5e38 &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203563","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203563"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203563\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}