{"id":50742,"date":"2023-12-23T10:31:40","date_gmt":"2023-12-23T02:31:40","guid":{"rendered":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/"},"modified":"2023-12-23T15:24:34","modified_gmt":"2023-12-23T07:24:34","slug":"%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/","title":{"rendered":"\u5728C\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217"},"content":{"rendered":"<p>\u5728C\u4e2d\uff0c\u961f\u5217\u57fa\u672c\u4e0a\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u6570\u636e\u5143\u7d20\u3002\u5b83\u9075\u5faa\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u987a\u5e8f\u3002<\/p>\n<p>\u5728\u961f\u5217\u4e2d\uff0c\u8fdb\u5165\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u4ece\u6570\u7ec4\u4e2d\u79fb\u9664\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u516c\u4ea4\u8f66\u7968\u9884\u8ba2\u644a\u4f4d\u7684\u60c5\u666f\u3002\u5728\u8fd9\u91cc\uff0c\u9075\u5faaC\u7f16\u7a0b\u961f\u5217\u7684\u65b9\u5f0f\u3002\u8f66\u7968\u6309\u5148\u6765\u5148\u670d\u52a1\u7684\u65b9\u5f0f\u5206\u53d1\uff0c\u5373\u7b2c\u4e00\u4e2a\u8fdb\u5165\u7684\u4eba\u5c06\u7b2c\u4e00\u4e2a\u83b7\u5f97\u8f66\u7968\u3002<\/p>\n<p>\u961f\u5217\u7684\u4e24\u7aef\u90fd\u662f\u5f00\u653e\u7684\u3002\u4e00\u4e2a\u7aef\u53e3\u7528\u6765\u63d2\u5165\u6570\u636e\uff0c\u53e6\u4e00\u4e2a\u7aef\u53e3\u7528\u6765\u5220\u9664\u6570\u636e\u3002<\/p>\n<p>\u4e00\u4e2a\u961f\u5217\u53ef\u4ee5\u7528\u4efb\u4f55\u7f16\u7a0b\u8bed\u8a00\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982C\u3001Java\u3001Python\u7b49\u3002<\/p>\n<hr>\n<\/hr>\n<h2>\u5728C\u8bed\u8a00\u4e2d\u4e0e\u961f\u5217\u76f8\u5173\u7684\u64cd\u4f5c<\/h2>\n<p>\u961f\u5217\u4f5c\u4e3a\u4e00\u79cd\u62bd\u8c61\u6570\u636e\u7ed3\u6784\uff0c\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u5bf9\u6570\u636e\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul class=\"post-ul\">\n<li>isEmpty(): To check if the queue is empty<\/li>\n<li>isFull(): To check whether the queue is full or not<\/li>\n<li>dequeue(): Removes the element from the frontal side of the queue<\/li>\n<li>enqueue(): It inserts elements to the end of the queue<\/li>\n<li>Front: Pointer element responsible for fetching the first element from the queue<\/li>\n<li>Rear: Pointer element responsible for fetching the last element from the queue<\/li>\n<\/ul>\n<hr>\n<\/hr>\n<h2>\u961f\u5217\u6570\u636e\u7ed3\u6784\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>\u961f\u5217\u9075\u5faa\u5148\u8fdb\u5148\u51fa\u7684\u6a21\u5f0f\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u88ab\u9996\u5148\u4ece\u5143\u7d20\u5217\u8868\u4e2d\u53d6\u51fa\u3002<\/p>\n<ul class=\"post-ul\">\n<li>Front and Rear pointers keep the record of the first and last element in the queue.<\/li>\n<li>At first, we need to initialize the queue by setting Front = -1 and Rear = -1<\/li>\n<li>In order to insert the element (enqueue), we need to check whether the queue is already full i.e. check the condition for Overflow. If the queue is not full, we\u2019ll have to increment the value of the Rear index by 1 and place the element at the position of the Rear pointer variable. When we get to insert the first element in the queue, we need to set the value of Front to 0.<\/li>\n<li>In order to remove the element (dequeue) from the queue, we need to check whether the queue is already empty i.e. check the condition for Underflow. If the queue is not empty, we\u2019ll have to remove and return the element at the position of the Front pointer, and then increment the Front index value by 1. When we get to remove the last element from the queue, we will have to set the values of the Front and Rear index to -1.<\/li>\n<\/ul>\n<hr>\n<\/hr>\n<h2>\u4f7f\u7528C\u8bed\u8a00\u5b9e\u73b0\u961f\u5217\u3002<\/h2>\n<p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u961f\u5217\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u3001\u94fe\u8868\u3001\u7ed3\u6784\u4f53\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u6211\u4eec\u4f7f\u7528\u6570\u7ec4\u5728C\u8bed\u8a00\u4e2d\u5b9e\u73b0\u4e86\u961f\u5217\u3002<\/p>\n<p>\u8bf7\u7ed9\u6211\u4e00\u676f\u5496\u5561\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdio.h&gt;<\/span><\/span>\r\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span> <span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">SIZE<\/span> <span class=\"token expression\"><span class=\"token number\">100<\/span><\/span><\/span>\r\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">enqueue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">dequeue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">show<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">int<\/span> inp_arr<span class=\"token punctuation\">[<\/span>SIZE<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">int<\/span> Rear <span class=\"token operator\">=<\/span> <span class=\"token operator\">-<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">int<\/span> Front <span class=\"token operator\">=<\/span> <span class=\"token operator\">-<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token keyword\">int<\/span> ch<span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span>\r\n    <span class=\"token punctuation\">{<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"1.Enqueue Operation\\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"2.Dequeue Operation\\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"3.Display the Queue\\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"4.Exit\\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Enter your choice of operations : \"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token function\">scanf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"%d\"<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>ch<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">switch<\/span> <span class=\"token punctuation\">(<\/span>ch<span class=\"token punctuation\">)<\/span>\r\n        <span class=\"token punctuation\">{<\/span>\r\n            <span class=\"token keyword\">case<\/span> <span class=\"token number\">1<\/span><span class=\"token operator\">:<\/span>\r\n            <span class=\"token function\">enqueue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n            <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span>\r\n            <span class=\"token keyword\">case<\/span> <span class=\"token number\">2<\/span><span class=\"token operator\">:<\/span>\r\n            <span class=\"token function\">dequeue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n            <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span>\r\n            <span class=\"token keyword\">case<\/span> <span class=\"token number\">3<\/span><span class=\"token operator\">:<\/span>\r\n            <span class=\"token function\">show<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n            <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span>\r\n            <span class=\"token keyword\">case<\/span> <span class=\"token number\">4<\/span><span class=\"token operator\">:<\/span>\r\n            <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n            <span class=\"token keyword\">default<\/span><span class=\"token operator\">:<\/span>\r\n            <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Incorrect choice \\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token punctuation\">}<\/span> \r\n    <span class=\"token punctuation\">}<\/span> \r\n<span class=\"token punctuation\">}<\/span> \r\n \r\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">enqueue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token keyword\">int<\/span> insert_item<span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>Rear <span class=\"token operator\">==<\/span> SIZE <span class=\"token operator\">-<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span>\r\n       <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Overflow \\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">else<\/span>\r\n    <span class=\"token punctuation\">{<\/span>\r\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>Front <span class=\"token operator\">==<\/span> <span class=\"token operator\">-<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span>\r\n      \r\n        Front <span class=\"token operator\">=<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Element to be inserted in the Queue\\n : \"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token function\">scanf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"%d\"<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>insert_item<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        Rear <span class=\"token operator\">=<\/span> Rear <span class=\"token operator\">+<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>\r\n        inp_arr<span class=\"token punctuation\">[<\/span>Rear<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> insert_item<span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span> \r\n \r\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">dequeue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>Front <span class=\"token operator\">==<\/span> <span class=\"token operator\">-<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">||<\/span> Front <span class=\"token operator\">&gt;<\/span> Rear<span class=\"token punctuation\">)<\/span>\r\n    <span class=\"token punctuation\">{<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Underflow \\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token punctuation\">}<\/span>\r\n    <span class=\"token keyword\">else<\/span>\r\n    <span class=\"token punctuation\">{<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Element deleted from the Queue: %d\\n\"<\/span><span class=\"token punctuation\">,<\/span> inp_arr<span class=\"token punctuation\">[<\/span>Front<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        Front <span class=\"token operator\">=<\/span> Front <span class=\"token operator\">+<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span> \r\n \r\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">show<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\r\n<span class=\"token punctuation\">{<\/span>\r\n    \r\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>Front <span class=\"token operator\">==<\/span> <span class=\"token operator\">-<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Empty Queue \\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token keyword\">else<\/span>\r\n    <span class=\"token punctuation\">{<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"Queue: \\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">=<\/span> Front<span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;=<\/span> Rear<span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">++<\/span><span class=\"token punctuation\">)<\/span>\r\n            <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"%d \"<\/span><span class=\"token punctuation\">,<\/span> inp_arr<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">\"\\n\"<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span> \r\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token number\">1.<\/span>Enqueue Operation\r\n<span class=\"token number\">2.<\/span>Dequeue Operation\r\n<span class=\"token number\">3.<\/span>Display the Queue\r\n<span class=\"token number\">4.<\/span>Exit\r\nEnter your choice of operations <span class=\"token operator\">:<\/span> <span class=\"token number\">1<\/span>\r\nElement to be inserted in the Queue<span class=\"token operator\">:<\/span> <span class=\"token number\">10<\/span>\r\n\r\n<span class=\"token number\">1.<\/span>Enqueue Operation\r\n<span class=\"token number\">2.<\/span>Dequeue Operation\r\n<span class=\"token number\">3.<\/span>Display the Queue\r\n<span class=\"token number\">4.<\/span>Exit\r\nEnter your choice of operations <span class=\"token operator\">:<\/span> <span class=\"token number\">1<\/span>\r\nElement to be inserted in the Queue<span class=\"token operator\">:<\/span> <span class=\"token number\">20<\/span>\r\n\r\n<span class=\"token number\">1.<\/span>Enqueue Operation\r\n<span class=\"token number\">2.<\/span>Dequeue Operation\r\n<span class=\"token number\">3.<\/span>Display the Queue\r\n<span class=\"token number\">4.<\/span>Exit\r\nEnter your choice of operations <span class=\"token operator\">:<\/span> <span class=\"token number\">3<\/span>\r\nQueue<span class=\"token operator\">:<\/span> \r\n<span class=\"token number\">10<\/span> <span class=\"token number\">20<\/span> \r\n\r\n<span class=\"token number\">1.<\/span>Enqueue Operation\r\n<span class=\"token number\">2.<\/span>Dequeue Operation\r\n<span class=\"token number\">3.<\/span>Display the Queue\r\n<span class=\"token number\">4.<\/span>Exit\r\nEnter your choice of operations <span class=\"token operator\">:<\/span> <span class=\"token number\">2<\/span>\r\nElement deleted from the Queue<span class=\"token operator\">:<\/span> <span class=\"token number\">10<\/span>\r\n\r\n<span class=\"token number\">1.<\/span>Enqueue Operation\r\n<span class=\"token number\">2.<\/span>Dequeue Operation\r\n<span class=\"token number\">3.<\/span>Display the Queue\r\n<span class=\"token number\">4.<\/span>Exit\r\nEnter your choice of operations<span class=\"token operator\">:<\/span> <span class=\"token number\">3<\/span>\r\nQueue<span class=\"token operator\">:<\/span> \r\n<span class=\"token number\">20<\/span> \r\n<\/code><\/pre>\n<hr>\n<\/hr>\n<h2>\u4f7f\u7528\u5806\u6808\u5b9e\u73b0\u961f\u5217\u7684\u529f\u80fd<\/h2>\n<p>\u5806\u6808\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u961f\u5217\u7684\u64cd\u4f5c\u3002\u4f7f\u7528\u4e24\u4e2a\u5806\u6808\u6765\u5b9e\u73b0\u961f\u5217\u3002\u5728\u4f60\u7ec3\u4e60\u4e0b\u9762\u7684\u4f8b\u5b50\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u4f60\u975e\u5e38\u4e86\u89e3\u5806\u6808\u7684\u8fd0\u4f5c\u3002<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u6808\u5b9e\u73b0\u961f\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u4e4b\u4e00\u5b9e\u73b0\uff1a<\/p>\n<ul class=\"post-ul\">\n<li>Making the enqueue operation costly<\/li>\n<li>Making the dequeue operation costly<\/li>\n<\/ul>\n<h3>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u5165\u961f\u64cd\u4f5c\u53d8\u5f97\u6602\u8d35\u3002<\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u6808\uff1aS1 \u548c S2 \uff0c\u6211\u4eec\u4f7f\u7528\u5b83\u4eec\u6765\u5b9e\u73b0\u961f\u5217\u64cd\u4f5c\u3002<\/p>\n<ul class=\"post-ul\">\n<li>If S1 is not empty, push all elements to stack 2 (S2)<\/li>\n<li>In order to perform the enqueue operation, we will assume \u2018x\u2019 to be the element to be entered into the queue. We will push \u2018x\u2019 back to Stack S1 so it comes up on the top.<\/li>\n<li>Further, push all the elements of stack S2 back to Stack S1<\/li>\n<\/ul>\n<p>\u6ce8\u610f\uff1a\u5165\u961f\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c06\u4e3aO(n)\u3002<\/p>\n<ul class=\"post-ul\">\n<li>In order to perform dequeue operation, we\u2019ll need to pop an item from the Stack S1 since now the first inserted element is on the top in S1 instead of being at the bottom.<\/li>\n<\/ul>\n<p>\u6ce8\u610f\uff1a\u51fa\u961f\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c06\u662fO(1)\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5206\u6790\u4f7f\u7528\u6808\u8fdb\u884cEnqueue\u548cDequeue\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u4f60\u4f1a\u53d1\u73b0Enqueue\u64cd\u4f5c\u6bd4Dequeue\u64cd\u4f5c\u66f4\u52a0\u6602\u8d35\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5982\u4e0a\u6240\u8ff0\uff0c\u6211\u4eec\u5c06\u7b2c\u4e00\u4e2a\u8fdb\u5165\u6216\u6700\u65e9\u8fdb\u5165\u7684\u5143\u7d20\u4fdd\u7559\u5728\u5806\u6808S1\u7684\u9876\u90e8\uff0c\u4ee5\u4fbf\u5728\u8c03\u7528Dequeue\u64cd\u4f5c\u65f6\u88ab\u79fb\u9664\u3002<\/p>\n<hr>\n<\/hr>\n<h3>\u65b9\u6cd5\u4e8c\uff1a\u5c06\u51fa\u961f\u64cd\u4f5c\u53d8\u5f97\u66f4\u52a0\u6602\u8d35<\/h3>\n<p>\u8ba9\u6211\u4eec\u518d\u6b21\u5047\u8bbe\u6709\u4e24\u4e2a\u6808\uff1aS1\u548cS2\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u961f\u5217\u64cd\u4f5c\u3002<\/p>\n<ul class=\"post-ul\">\n<li>In order to implement enqueue operation, we insert the newly entered element at the top of Stack S1. Thus, the time complexity of the Enqueue operation using Stacks becomes O(1).<\/li>\n<li>For the implementation of dequeue operation, it checks for the Underflow condition of Stack S1 and S2. If both the Stacks stands out to be empty, an error would be raised.<\/li>\n<li>If the Stack S2 is empty and S1 is not empty, then all the elements from Stack S1 are moved to Stack S2 and the top element of Stack S2 is popped out and returned out of the Dequeue operation.<\/li>\n<li>Thus, the time complexity of the dequeue operation using Stacks becomes O(n).<\/li>\n<\/ul>\n<hr>\n<\/hr>\n<h2>\u961f\u5217\u6570\u636e\u7ed3\u6784\u7684\u5e94\u7528<\/h2>\n<ul class=\"post-ul\">\n<li>CPU Scheduling<\/li>\n<li>Disk Scheduling<\/li>\n<li>Asynchronous data transfer between processors such as File IO, etc.<\/li>\n<li>Breadth-First Search Algorithm (BFS)<\/li>\n<\/ul>\n<hr>\n<\/hr>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u961f\u5217\u6570\u636e\u7ed3\u6784\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5e76\u4e14\u4e5f\u7b80\u5355\u4ecb\u7ecd\u4e86\u4f7f\u7528\u6808\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u961f\u5217\u7684\u65b9\u6cd5\u3002<\/p>\n<hr>\n<\/hr>\n<h2>\u53c2\u8003\u6587\u732e<\/h2>\n<ul class=\"post-ul\">\n<li>Queue in C<\/li>\n<li>Queue using Stacks<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728C\u4e2d\uff0c\u961f\u5217\u57fa\u672c\u4e0a\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u6570\u636e\u5143\u7d20\u3002\u5b83\u9075\u5faa\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u987a\u5e8f\u3002 \u5728\u961f\u5217\u4e2d\uff0c\u8fdb [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-50742","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5728C\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217 - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/zh\/blog\/\u5728c\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5728C\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217\" \/>\n<meta property=\"og:description\" content=\"\u5728C\u4e2d\uff0c\u961f\u5217\u57fa\u672c\u4e0a\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u6570\u636e\u5143\u7d20\u3002\u5b83\u9075\u5faa\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u987a\u5e8f\u3002 \u5728\u961f\u5217\u4e2d\uff0c\u8fdb [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/zh\/blog\/\u5728c\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-23T02:31:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-23T07:24:34+00:00\" \/>\n<meta name=\"author\" content=\"\u6587, \u7fd4\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u6587, \u7fd4\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/\",\"name\":\"\u5728C\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217 - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\"},\"datePublished\":\"2023-12-23T02:31:40+00:00\",\"dateModified\":\"2023-12-23T07:24:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/64d5cc7727fffbff2f9a2a8da1de3e5c\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.silicloud.com\/zh\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5728C\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/\",\"name\":\"Blog - Silicon Cloud\",\"description\":\"\",\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/64d5cc7727fffbff2f9a2a8da1de3e5c\",\"name\":\"\u6587, \u7fd4\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/920c3d673e0bccacc98e5e6b7149bb3c22edd8d39cb753e5d7d7e471498118a1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/920c3d673e0bccacc98e5e6b7149bb3c22edd8d39cb753e5d7d7e471498118a1?s=96&d=mm&r=g\",\"caption\":\"\u6587, \u7fd4\"},\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/author\/wenxiang\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/#local-main-organization-logo\",\"url\":\"\",\"contentUrl\":\"\",\"caption\":\"Blog - Silicon Cloud\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"\u5728C\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217 - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/zh\/blog\/\u5728c\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217\/","og_locale":"zh_CN","og_type":"article","og_title":"\u5728C\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217","og_description":"\u5728C\u4e2d\uff0c\u961f\u5217\u57fa\u672c\u4e0a\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u6570\u636e\u5143\u7d20\u3002\u5b83\u9075\u5faa\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u987a\u5e8f\u3002 \u5728\u961f\u5217\u4e2d\uff0c\u8fdb [&hellip;]","og_url":"https:\/\/www.silicloud.com\/zh\/blog\/\u5728c\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217\/","og_site_name":"Blog - Silicon Cloud","article_published_time":"2023-12-23T02:31:40+00:00","article_modified_time":"2023-12-23T07:24:34+00:00","author":"\u6587, \u7fd4","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u6587, \u7fd4","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/","url":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/","name":"\u5728C\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217 - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website"},"datePublished":"2023-12-23T02:31:40+00:00","dateModified":"2023-12-23T07:24:34+00:00","author":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/64d5cc7727fffbff2f9a2a8da1de3e5c"},"breadcrumb":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.silicloud.com\/zh\/blog\/"},{"@type":"ListItem","position":2,"name":"\u5728C\u8bed\u8a00\u4e2d\u521b\u5efa\u4e00\u4e2a\u961f\u5217"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website","url":"https:\/\/www.silicloud.com\/zh\/blog\/","name":"Blog - Silicon Cloud","description":"","inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/64d5cc7727fffbff2f9a2a8da1de3e5c","name":"\u6587, \u7fd4","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/920c3d673e0bccacc98e5e6b7149bb3c22edd8d39cb753e5d7d7e471498118a1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/920c3d673e0bccacc98e5e6b7149bb3c22edd8d39cb753e5d7d7e471498118a1?s=96&d=mm&r=g","caption":"\u6587, \u7fd4"},"url":"https:\/\/www.silicloud.com\/zh\/blog\/author\/wenxiang\/"},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%9c%a8c%e8%af%ad%e8%a8%80%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e9%98%9f%e5%88%97\/#local-main-organization-logo","url":"","contentUrl":"","caption":"Blog - Silicon Cloud"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/50742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/comments?post=50742"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/50742\/revisions"}],"predecessor-version":[{"id":50787,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/50742\/revisions\/50787"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/media?parent=50742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/categories?post=50742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/tags?post=50742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}