嵌入式学习第3天

学习日志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

猜你喜欢

转载自blog.csdn.net/weixin_43175816/article/details/82666608