嵌入式學習第3天
阿新 • • 發佈:2018-12-09
學習日誌3 姓名:何堯華 日期:2018.9.12 今日學習任務: 1、什麼是佇列? 線性結構儲存方式: 順序儲存(連續) 鏈式儲存(不連續) 3、佇列有哪些資訊? 4、如何理解佇列的用法 5、如何編寫進對、出對、讀取佇列中元素以及判斷佇列是否為空? 佇列的進出方式:先進先出 今日任務完成情況: 基本上能跟上老師的步伐完成任務,有一些生疏。 今日開發程式碼量:約500行 學到了新的編寫命令,如gcc *.c -Wall 執行指令 ./a.out 學到了如何把一個程式拆分為3個小程式的方法 今日開發中出現的問題彙總: 1、對佇列的編寫運用需要更加了解,有一些生疏。 2、在程式設計時有一些基本語法錯誤導致程式不能編譯成功。 3、順序儲存佇列功能函式中進隊、出隊的程式碼實現功能的流程還存在一些不太清楚; 4、鏈式儲存佇列功能函式,何時需要釋放結點,還不太明確。 今日未解決問題: 無 編寫程式時小問題多總是要回頭查詢問題 ,對結構體還需要學習。 今日開發收穫:對頭用來取出資料,對尾用來儲存資料,進隊是操作對尾的, 出隊是操作隊頭的,佇列的順序儲存是用迴圈佇列來完成的,空對的意思是對頭對尾重合,對尾指標是隻向最後一個元素的後一個 迴圈佇列長度:(rear-front+MAXSIZE)%MAXSIZE 判斷佇列是否滿:(rear+1)%MAXSIZE=front 今日編寫的6個程式: queue 1、main.c
#include <stdio.h>
#include "queue.h"
int main()
{
int i,ret;
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("Destory Success!\n");
}
else
{
printf("Destory Failure!\n");
}
return 0;
}
2、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;
}
3、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 ClearQeue(Q *q);
int DestoryQueue(Q *q);
#endif
linkqueue 1、main.c
#include "queue.h"
#include<stdio.h>
int main()
{
int i,ret;
Q *queue;
ret = InitQueue(&queue);
if(ret == SUCCESS)
{
printf("Init Success!\n");
}
else
{
printf("Init Failure!\n");
}
for (i = 0; i < 10; i++)
{
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++)
{
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(ret == SUCCESS)
{
printf("queue is empty!\n");
}
else
{
printf("queue is not empty!\n");
}
ret = DestoryQueue(&queue);
if (ret == SUCCESS)
{
printf("Destory Success!\n");
}
else
{
printf("Destory Failure!\n");
}
return 0;
}
2、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 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 DestoryQueue(Q **q)
{
if (NULL == q)
{
return FAILURE;
}
free((*q)->front); //釋放頭結點
free(*q); //釋放佇列資訊
*q = NULL;
return SUCCESS;
}
3、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 DestoryQueue(Q **q);
#endif