嵌入式课程设计——学习日志 (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;
}

运行结果输出:
这里写图片描述

六.自我评价
今天学习了一些队列结构方面的知识,课堂上写的程序都能够跟得上老师,但是还是会出现一些问题,通过自己的修改,程序都已经认真完成了,并且都可以正常运行,又是收获满满的一天。

猜你喜欢

转载自blog.csdn.net/qq_41009234/article/details/82666826