苏嵌 项目实战
姓名:卢雯 日期:2018-09-05
今日学习任务
循环队列,程序编写,列队的清空,读取,队头,队尾的取出,链式结构。
今日任务完成情况
main.c
#include <stdio.h>
#include "queue.h"
#include <stdlib.h>
int main()
{
Q queue;
int ret,i;
ret = InitQueue(&queue);
if (ret == FAILURE)
{
printf("Init Failure!\n");
exit(1);
}
else if (ret == SUCCESS)
{
printf("Init Success!\n");
}
ret = EmptyQueue(queue);
if (ret == SUCCESS)
{
printf("queue is empty\n");
}
else if (ret == FAILURE)
{
printf("queue is not empty\n");
}
for (i=0;i<10;i++)
{
ret = EnterQueue(&queue, i+1);
if (ret == FAILURE)
{
printf("enter failure!\n");
}
else if (ret == SUCCESS)
{
printf("enter %d success!\n",i+1);
}
}
int length = LengthQueue(queue);
printf("length is %d\n", length);
ret = GetFront(queue);
if (ret == FAILURE)
{
printf("get front failure!\n");
}
else
{
printf("front is %d\n",ret);
}
for (i=0;i<5;i++)
{
ret = DelQueue(&queue);
if (ret == FAILURE)
{
printf("Del Failure!\n");
}
else
{
printf("Del %d Success!\n",ret);
}
}
length = LengthQueue(queue);
printf("length is %d\n", length);
ret = GetFront(queue);
if (ret == FAILURE)
{
printf("get front failure!\n");
}
else
{
printf("front is %d\n",ret);
}
ret = ClearQueue(&queue);
if (ret == SUCCESS)
{
printf("clear Success!\n");
}
else
{
printf("clear Failure!\n");
}
ret = GetFront(queue);
if (ret == FAILURE)
{
printf("get front failure!\n");
}
else
{
printf("front is %d\n",ret);
}
ret = DestroyQueue(&queue);
if (ret == FAILURE)
{
printf("Destroy Failure!\n");
}
else
{
printf("Destroy Success!\n");
}
return 0;
}
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 EmptyQueue(Q q);
int EnterQueue(Q *q,int e);
int LengthQueue(Q q);
int GetFront(Q q);
int DelQueue(Q *q);
int ClearQueue(Q *q);
int DestroyQueue(Q *q);
#endif
queue.c
#include "queue.h"
#include <stdlib.h>
int InitQueue(Q *q)
{
if (NULL == q)
{
return FAILURE;
}
q->data = malloc(sizeof(int) * MAXSIZE);
if (NULL == q->data)
{
return FAILURE;
}
q->front = q->rear = 0;
return SUCCESS;
}
int EmptyQueue(Q q)
{
return ((q.front == q.rear) ? SUCCESS : FAILURE);
}
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 LengthQueue(Q q)
{
int len;
len = (q.rear - q.front + MAXSIZE)% MAXSIZE;
return len;
}
int GetFront(Q q)
{
if (q.front == q.rear)
{
return FAILURE;
}
return q.data[q.front];
}
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 ClearQueue(Q *q)
{
if (NULL == q)
{
return FAILURE;
}
q->rear = q->front;
return SUCCESS;
}
今日开发中出现的问题汇总
速度太慢,程序编写错误多。
今日未解决问题
程序编写还未完成,对程序没有完全理解
今日开发收获
熟悉队列的顺序链式存储
自我评价
只是勉强跟上老师的步骤,对程序未能理解