1. 程式人生 > >嵌入式基礎應用開發之佇列的開發與使用(蘇嵌日誌之Wednesday)

嵌入式基礎應用開發之佇列的開發與使用(蘇嵌日誌之Wednesday)

學習日誌 姓名:沈壯壯 1.今日學習任務 佇列的順序儲存及鏈式儲存 2.今日任務完成情況 回顧了昨天的棧式結構:先進後出,由此引出佇列:先進先出。 隊頭(front):取出資料 隊尾(rear):存放資料(順序儲存,鏈式儲存) 順序儲存:迴圈佇列 (空隊:對頭隊尾重合。 隊尾指標:指向最後一個元素的後一個。 ) (迴圈佇列長度:(rear-front+MAXSIZE)% MAXSIZE) (判斷佇列是否滿:(rear+1)%MAXSIZE==front) 突破了350+的程式碼量,實現了所要的功能。 3.今日開發中出現的問題彙總 對於細節方面把握的不是很好,老是犯一些低階的錯誤。;未加呀等等,為此耽誤了不少時間。 4.今日收穫 通過程式的編寫,基本瞭解C語言開發的流程,雖然是最基礎的東西,但對我的未來的程式設計師之路又邁出一小步。 4.自我評價 打字的速度依舊過慢,改變了一指禪的打法,還要多加練習。 成功完成任務,並且改正了好多的錯誤,今天完美收官。好多知識點不是應用的太熟練,多加使用,應該才會牢牢掌握。 5.附上今日的成果: main.c的主函式檔案

#include"queue.h"
#include<stdio.h>

int main()
{
   int ret,i;
   Q *queue;

   //初始化鏈隊
   ret=InitQueue(&queue);
   if(ret==SUCCESS)
   {
       printf("初始化成功!\n");
   }
   else
   {
       printf("初始化失敗!\n");
   }

     //
   for(i=0;i<6;i++)
   {
       ret=EntQueue(queue,i+1);
       if
(ret==SUCCESS) { printf("資料 %d 進隊(寫入)成功!\n",i+1); } else { printf("資料進隊(寫入)失敗!\n"); } } // for(i=0;i<2;i++) { ret=DelQueue(queue); if(ret==FAILURE) { printf("資料出隊(刪除)失敗!\n"); } else
{ printf("資料 %d 出隊(刪除)成功!\n",ret); } } // ret=LenQueue(queue); printf("資料長度為 %d\n",ret); // ret=ClearQueue(queue); if(ret==SUCCESS) { printf("資料清空成功!\n"); } else { printf("資料清空失敗!\n"); } // ret=LenQueue(queue); printf("資料長度為 %d\n",ret); // ret=EmQueue(queue); if(ret==SUCCESS) {printf("資料為空\n");} else {printf("資料非空\n");} // ret=DesQueue(&queue); if(ret==SUCCESS) { printf("銷燬成功\n"); } else { printf("銷燬失敗\n"); } return 0; }

queue.h的標頭檔案

#ifndef QUEUE_H
#define QUEUE_H

#define SUCCESS 1000
#define FAILURE 1001


struct node     //節點資訊
{
    int data;     //資料域
    struct node *next;    //指標域
};
typedef struct node Node;

struct queue    //佇列資訊
{
     Node *front;     //隊頭指標
     Node *rear;     //隊尾指標
};
typedef struct queue Q;

int InitQueue(Q **q);
int EntQueue(Q *q,int e);
int DelQueue(Q *q);
int LenQueue(Q *q);
int ClearQueue(Q *q);
int EmQueue(Q *q);
int DesQueue(Q **q);

#endif

queue.c的子函式檔案

#include"queue.h"
#include<stdlib.h>

//
int InitQueue(Q **q)
{
    if(NULL==q)     //入參判斷
    {
        return FAILURE;
    }

    (*q)=(Q *)malloc(sizeof(Q));   //給佇列資訊申請空間

    if(NULL==(*q))
    {
        return FAILURE;
    }

    Node *p=(Node *)malloc(sizeof(Node));   //頭節點申請空間
    if(NULL==p)
    {
        return FAILURE;
    }

    (*q)->front=(*q)->rear=p;     //隊頭指標 隊尾指標都指向頭節點

    return SUCCESS;
}

//
int EntQueue(Q *q,int e)
{
    if(NULL==q)
    {
        return FAILURE;
    }

    Node *p=(Node *)malloc(sizeof(Node));

    if(NULL==p)
    {
        return FAILURE;
    }
    p->next=NULL;      //指標域
    p->data=e;         //資料域

    q->rear->next=p;
    q->rear=p;

    return SUCCESS;
}

//
int DelQueue(Q *q)
{
    if(NULL==q)
    {
         return FAILURE;
    }

    if(q->rear==q->front)   //空隊
    {
      return FAILURE;
    }

    Node *p=q->front->next;
    int e=p->data;
    q->front->next=p->next;
    free(p);

    if(q->rear==p)     //只剩一個節點的情況
    {
         q->rear=q->front;
    }

    return e;
}

//
int LenQueue(Q *q)
{
    if(NULL==q)     //入參判斷
    {
        return FAILURE;
    }

    int len=0;

    Node *p=q->front->next;
    while(p)  //while(p!=NULL)
    {
         len++;
         p=p->next;
    }


    return len;
}

//
int ClearQueue(Q *q)
{
    if(NULL==q)     //入參判斷
    {
        return FAILURE;
    }
    Node *p=q->front->next;
    while(p)
    {
         q->front->next=p->next;
         free(p);   //釋放結點
         p=q->front->next;     //指向新的結點
    }

    q->rear =q->front;         //刪完所有結點,隊尾指向開頭

    return SUCCESS;
}

int EmQueue(Q *q)
{
    if(NULL==q)
    {
          return FAILURE;
    }
    return (q->front==q->rear)?SUCCESS:FAILURE;
}

//
int DesQueue(Q **q)
{
    if(NULL==q)
    {
          return FAILURE;
    }

    free((*q)->front);   //釋放頭結點
    free(*q);         //釋放佇列資訊
    *q=NULL;

    return SUCCESS;
}