1. 程式人生 > >嵌入式課程設計——學習日誌 (3)

嵌入式課程設計——學習日誌 (3)

姓名: 張春林
日期: 2018-9-12

一.今日任務
學習資料結構中的佇列,通過佇列進一步瞭解線性結構,上午學習線性結構的順序儲存(儲存空間連續)佇列,下午學習線性結構的鏈式儲存(儲存空間不連續)佇列。

二.今日任務完成情況
今日任務按計劃順利完成,上課程式碼除錯全部正常執行,今日程式碼量:500。

三.今日開發中出現的問題彙總
關於多級指標(**p)的使用還不熟練,對一些函式的編寫還不能及時理解。

四.今日未解決問題
今天老師講的非常好,解決了我心中的許多疑問,對什麼時候使用隊頭(front)或者隊尾(rear)什麼時候使用q->front(q->rear)還不夠清楚。

五.今日開發收穫
通過今天的學習,學會了佇列的相關知識與操作。
像棧一樣,佇列(queue)也是一種線性表,它的特性是先進先出,插入在一端,刪除在另一端。就像排隊一樣,先進來的資料入隊要排在隊尾(rear),每次出隊的都是隊首(front)的資料。

1.順序儲存的連續佇列,順序儲存佇列的編寫:
首先編寫標頭檔案 “queue.h”

#ifndef QUEUE_H
#define QUEUE_H

#define MAXSIZE 10      //佇列容量  

//標誌位(成功或者失敗)
#define SUCCESS 1000        
#define FAILURE 1001

struct
queue { int *data; //向佇列的儲存空間 int front; //隊頭指標 int rear; //隊尾指標 }; typedef struct queue Q; int InitQueue(Q *q); //初始化 int EnterQueue(Q *q, int e); //入隊 int DelQueue(Q *q); //出隊 int LengthQueue(Q q); //求佇列長度 int ClearQueue(Q *q); //清空佇列
int DestoryQueue(Q *q); //釋放佇列 #endif

編寫 “queue.c”

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

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

      //申請一塊記憶體空間,並且讓data指標指向這塊空間
      q->data = (int *)malloc(sizeof(int) * MAXSIZE);
      if (NULL == q->data)    //返回值判斷(如果申請失敗)
      {
          return FAILURE;
      }

      q->rear = q->front = 0//隊頭隊尾指標指向同一個 
      return SUCCESS;
  }

  int EnterQueue(Q *q, int e) //進隊
  {
      if (NULL == q)
      {
          return FAILURE;
      }
      if((q->rear + 1) % MAXSIZE == q->front) //隊滿
      {
          return FAILURE;
      }
      q->data[q->rear] = e;
      q->rear = (q->rear +1) % MAXSIZE;
      return SUCCESS;
  }

  int DelQueue(Q *q)  //出隊
  {
      if (NULL == q)
      {
          return FAILURE;
      }
      if (q->rear == q->front)    //空隊
      {
          return FAILURE;
      }

      int e = q->data[q->front];
      q->front = (q->front + 1) % MAXSIZE;
      return e;
  }

  int LengthQueue(Q q)    //求佇列長度
  {
      return (q.rear - q.front + MAXSIZE) % MAXSIZE;
  }

  int ClearQueue(Q *q)    //清理佇列
  {
      if (NULL == q)
      {
          return FAILURE;
      }
      q->rear = q->front;
      return SUCCESS;
  }

  int DestoryQueue(Q *q)
  {
      if(NULL == q)
      {
          return FAILURE;
      }
      free(q->data);  //釋放空間
      q->data = NULL;
      return SUCCESS;
 }

編寫 “main.c”

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

int main()
{   
    int ret, i;
    Q queue;            //定義佇列
    ret = InitQueue(&queue);    //初始化
    if (SUCCESS == ret)
    {
        printf("Init Success!\n");
    }   
    else
    {
        printf("Init Failure!\n");  
    }

    for(i = 0; i< 10; i++)            //進隊
    {
        ret = EnterQueue(&queue, i + 1);
        if(ret == FAILURE)
        {
            printf("Enter Failure!\n");     
        }
        else
        {   
            printf("Enter %d Success!\n",i + 1);        
        }   
    }

    for(i = 0; i < 5; i++)            //出隊
    {
        ret = DelQueue(&queue);
        if(ret == FAILURE)
        {
            printf("Delete Failure!\n");        
        }
        else
        {
            printf("Delete %d Success!\n",ret);         
        }

    }

    ret = LengthQueue(queue);   //求佇列長度
    printf("Length is %d\n",ret);

    ret = ClearQueue(&queue);   //清空佇列
    if (ret == SUCCESS)
    {
        printf("Clear Success!\n");
    }
    else
    {
        printf("Clear Failure!\n");
    }

    ret = DestoryQueue(&queue); //釋放佇列
    if(ret == SUCCESS)
    {
        printf("Destroy Success!\n");
    }
    else
    {
        printf("Destroy Failure!\n");
    }


    return 0;
}

執行結果輸出:
這裡寫圖片描述


2.鏈式儲存的不連續佇列,鏈式儲存佇列的編寫:

首先編寫標頭檔案 “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 EnterQueue(Q *q, int e);    //出隊
int DeleteQueue(Q *q);      //刪除一個結點
int LengthQueue(Q *q);      //求佇列長度
int ClearQueue(Q *q);       //清空佇列
int EmptyQueue(Q *q);       //判斷佇列是否為空
int DestroyQueue(Q **q);    //釋放佇列

#endif 

編寫 “queue.c”

#include<stdio.h>
#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 EnterQueue(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 DeleteQueue(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 LengthQueue(Q *q)           //求佇列長度
{
    if (NULL == q)
    {
        return FAILURE;
    }

    int length = 0;
    Node *p = q->front->next;
    while(p)                //while(P != NULL)
    {
        length++;
        p = p->next;
    }
    return length;
}

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 EmptyQueue(Q *q)                //判斷佇列是否為空
{
    if (NULL == q)
    {
        return FAILURE;
    }
    return (q->front == q->rear) ? SUCCESS : FAILURE;
}

int DestroyQueue(Q **q)         //釋放佇列
{
    if(NULL == q)
    {
        return FAILURE;
    }

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

    return SUCCESS;
}

編寫 “main.c”

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

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

    ret = InitQueue(&queue);        //初始化
    if (ret == SUCCESS)
    {
        printf("Init Success!\n");
    }
    else
    {
        printf("Init Failure!\n");
    }

    for(i = 0; i<10; i++)       //進隊,放入10個
    {
        ret = EnterQueue(queue, i + 1);
        if (ret == SUCCESS)
        {
            printf("Enter %d Success!\n", i + 1);
        }
        else
        {
            printf("Enter Failure!\n");
        }

    }

    for (i = 0; i < 6; i++)     //出隊,移出6個
    {
        ret = DeleteQueue(queue);
        if (ret == FAILURE)
        {
            printf("Delete Failure!\n");
        }
        else
        {
            printf("Delete %d Success!\n", ret);
        }

    }

    ret = LengthQueue(queue);       //求佇列長度
    printf("Length is %d\n",ret);

    ret = ClearQueue(queue);        //清空佇列
    if (ret == SUCCESS)
    {
        printf("Clear Success!\n");
    }
    else
    {
        printf("Clear Failure!\n");
    }

    ret = LengthQueue(queue);       //求佇列長度
    printf("Length is %d\n",ret);

    ret = EmptyQueue(queue);        //判斷是否為空佇列
    if (SUCCESS == ret)
    {
        printf("queue is empty!\n");
    }
    else
    {
        printf("queue is not empty!\n");
    }

    ret = DestroyQueue(&queue);     //釋放佇列
    if(ret == SUCCESS)
    {
        printf("Destroy Success!\n");
    }
    else
    {
        printf("Destroy Failure!\n");
    }

    return 0;
}

執行結果輸出:
這裡寫圖片描述

六.自我評價
今天學習了一些佇列結構方面的知識,課堂上寫的程式都能夠跟得上老師,但是還是會出現一些問題,通過自己的修改,程式都已經認真完成了,並且都可以正常執行,又是收穫滿滿的一天。