第 17 章 高級數據表示(測試隊列)

来源:https://www.cnblogs.com/web1013/archive/2018/07/09/9286563.html
-Advertisement-
Play Games

1 /* 2 queue.h -- Queue介面 3 */ 4 5 #ifndef QUEUE_H 6 #define QUEUE_H 7 8 #define MAXQUEUE 10 9 10 typedef int Item; 11 12 typedef struct node 13 { 14 ...


 1 /*------------------------
 2     queue.h -- Queue介面
 3 ------------------------*/
 4 
 5 #ifndef QUEUE_H
 6 #define QUEUE_H
 7 
 8 #define MAXQUEUE 10
 9 
10 typedef int Item;
11 
12 typedef struct node
13 {
14     Item item;
15     struct node *next;
16 } Node;
17 
18 typedef struct queue
19 {
20     Node *front;    //指向隊列首項的指針
21     Node *rear;        //指向隊列尾項的指針
22     int items;        //隊列中的項數
23 } Queue;
24 
25 //初始化隊列為空
26 void InitializeQueue(Queue *pq);
27 
28 //檢查隊列是否已滿,已滿返回true,否則返回false
29 bool QueueIsFull(const Queue *pq);
30 
31 //檢查隊列是否為空,為空返回true,否則返回false
32 bool QueueIsEmpty(const Queue *pq);
33 
34 //確定隊列的項數
35 int QueueItemCount(const Queue *pq);
36 
37 //在隊列末尾添加項:item是被添加在隊列末尾的項
38 bool EnQueue(Item item, Queue *pq);
39 
40 //在隊列開頭刪除項:隊列不為空,隊列首位item將被拷貝到*pitem中
41 bool DeQueue(Item *pitem, Queue *pq);
42 
43 //清空隊列
44 void EmptyTheQueue(Queue *pq);
45 
46 #endif
queue.h
 1 /*----------------------------
 2     queue.c -- Queue 實現
 3 ----------------------------*/
 4 
 5 #include <stdio.h>
 6 #include <stdlib.h>
 7 #include "queue.h"
 8 
 9 static void CopyToNode(const Item *pi, Node *pn)
10 {
11     pn->item = *pi;
12 }
13 
14 static void CopyToItem(const Node *pn, Item *pi)
15 {
16     *pi = pn->item;
17 }
18 
19 void InitializeQueue(Queue *pq)
20 {
21     pq->front = pq->rear = NULL;
22     pq->items = 0;
23 }
24 
25 bool QueueIsFull(const Queue *pq)
26 {
27     return pq->items == MAXQUEUE;
28 }
29 
30 bool QueueIsEmpty(const Queue *pq)
31 {
32     return pq->items == 0;
33 }
34 
35 int QueueItemCount(const Queue *pq)
36 {
37     return pq->items;
38 }
39 
40 bool EnQueue(Item item, Queue *pq)
41 {
42     if (QueueIsFull(pq)) return false;
43 
44     Node *pnew = (Node*)malloc(sizeof(Node));
45     if (pnew == NULL)
46     {
47         fprintf(stderr, "Unable to allocate memory!\n");
48         exit(EXIT_FAILURE);
49     }
50 
51     CopyToNode(&item, pnew);
52     pnew->next = NULL;
53 
54     if (QueueIsEmpty(pq))
55         pq->front = pnew;                //新添項位於隊列頭端
56     else
57         pq->rear->next = pnew;          //新添項鏈接到隊列尾端
58 
59     pq->rear = pnew;                    //記錄隊列尾端的位置
60     ++(pq->items);                      //隊列項數加1
61 
62     return true;
63 }
64 
65 bool DeQueue(Item *pitem, Queue *pq)
66 {
67     if (QueueIsEmpty(pq)) return false;
68 
69     CopyToItem(pq->front, pitem);
70 
71     Node *pt = pq->front;
72     pq->front = pt->next;
73     free(pt);
74     --(pq->items);
75 
76     if (QueueIsEmpty(pq)) pq->rear = NULL;
77 
78     return true;
79 }
80 
81 void EmptyTheQueue(Queue *pq)
82 {
83     Item dummy;
84     while (!QueueIsEmpty(pq)) DeQueue(&dummy, pq);
85 }
queue.c
 1 /*---------------------------------------
 2     use_q.c -- 驅動程式測試 Queue 介面
 3 ---------------------------------------*/
 4 
 5 #include <stdio.h>
 6 #include "queue.h"
 7 
 8 int main()
 9 {
10     Queue line;
11     Item temp;
12     char ch;
13 
14     InitializeQueue(&line);
15 
16     puts("Testing the Queue interface. Type \"a\" to add a value, ");
17     puts("type \"d\" to delete a value, and type \"q\" to quit.");
18 
19     while ((ch = getchar()) != 'q')
20     {
21         if (ch != 'a' && ch != 'd') continue;    //忽略其他輸出
22 
23         if (ch == 'a')        //add
24         {
25             printf("Integer to add: ");
26             scanf("%d", &temp);
27             
28             if (!QueueIsFull(&line))
29             {
30                 printf("Putting %d into queue\n", temp);
31                 EnQueue(temp, &line);
32             }
33             else
34                 puts("Queue is full!");
35         }
36         else                //delete
37         {
38             if (QueueIsEmpty(&line))
39                 puts("Nothing to delete!");
40             else
41             {
42                 DeQueue(&temp, &line);
43                 printf("Removing %d from queue\n", temp);
44             }
45         }
46 
47         printf("%d items in queue\n", QueueItemCount(&line));
48         puts("Type \"a\" to add, \"d\" to delete, \"q\" to quit;");
49     }
50 
51     EmptyTheQueue(&line);
52     puts("Bye!");
53 
54     return 0;
55 }
use_q.c


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • __author__ = "yang xin"array=[]def quickSort(left,right): if left > right: return temp = array[left] i = left j = right while i < j: if array[j] >= te ...
  • 首先感謝老觀眾水中盜影和其它幾位的回覆,我媽還沒見到我娶媳婦就走了,也是我的不孝啊! 這一個月前半都在跑我媽的後事,後半都是強制996的加班中 天天坐火車回去當天再回來,雖然忙但還挺充實,也沒想什麼,公證處要什麼材料我就去各單位去開各種證明... 但是公證跑完了之後,後半個月的兩周我幾乎天天失眠,甚 ...
  • Angular中的裝飾器是一個函數,它將元數據添加到類、類成員(屬性、方法)和函數參數。 用法:要想應用裝飾器,把它放在被裝飾對象的上面或左邊。 Angular使用自己的一套裝飾器來實現應用程式各部件之間的相互操作。 這個地方是前面幾個模塊(Modules), 指令(Diretives)、組件(Co ...
  • 一、面向對象簡介 Python設計之初,就是一門面向對象的語言,在Python中一切皆對象,而且在Python中創建一個對象也很簡單,今天我們就來學習一下Python的面向對象的知識。 二、兩種編程方式 在C#、Java中,只能使用面向對象編程,在Ruby、Python中可以使用函數編程以及面向對象 ...
  • 原創 題目為:()()()+()()()=()()() 將1~9這9個數字填入括弧,每個數字只能用一次。 枚舉: 1 public class Test { 2 public static void main(String[] args){ 3 int a[]=new int[9]; 4 int f ...
  • 創建一個socketserver 至少分以下幾步 First, you must create a request handler class by subclassing the BaseRequestHandlerclass and overriding its handle() method; ...
  • 1、引言 點陣圖是使用位(bit)數組來對數據進行統計,排序和去重,其結構圖如下: 其中點陣圖的索引映射需要存儲的值,點陣圖索引所在位置的值表示索引對應的值是否已經存儲。 2、介面 3、實現 定義靜態byte數組常量,用於快速檢驗點陣圖上索引對應的值: 聲明欄位: 其中size為點陣圖的大小;當點陣圖的size ...
  • O’Reilly的電子書《Reactive Microservices Architecture》講述了微服務/分散式系統的一些設計原則,本文是筆者閱讀完此書後的理解。 微服務相比傳統的單體應用能夠帶來快速的響應,以小的系統產生大的影響。而隨著網路加速、磁碟成本降低、RAM成本降低、多核技術的發展、 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...