学习日志 姓名:顾迎星 日期:2018.9.5
一.今日学习任务:队列(对头,队尾,先进先出,链式结构)
二.今日任务完成情况
1.main.c
#include <stdio.h>
#include "queue.h"
int main()
{
Q queue;
int ret,i;
ret = InitQueue(&queue);
if (ret == FAILURE)
{
printf("Init Failure!\n");
}
else if (ret == SUCCESS)
{
printf("Init Success!\n");
}
for (i = 0; i< 5;i++)
{
//jin dui
ret = EnterQueue(&queue,i+1);
if (ret == FAILURE)
{
printf("enter failure!\n");
}
else if (ret == SUCCESS)
{
printf("enter %d success\n",i+1);
}
}
//huo qu dui lie chang du
int length = LengthQueue(queue);
printf("length is %d\n ",length);
//huo qu dui tou yuan su
ret = GetFront(queue);
if (ret == FAILURE )
{
printf("get front failure!\n");
}
else
{
printf(" front is %d\n",ret);
}
for (i = 0;i<3;i++)
{
ret = DelQueue(&queue);
if (ret == FAILURE)
{
printf("Delete Failure!\n");
}
else
{
printf("Del %d Success!\n",ret);
}
}
//qing kong
ret = ClearQueue(&queue);
if (ret == FAILURE)
{
printf("Clear Failure!\n");
}
else
{
printf("Clear Success!\n");
}
//xiao hui
ret = DestroyQueue(&queue);
if (ret == SUCCESS)
{
printf(" Destroy Success!\n");
}
else if (ret == FAILURE)
{
printf(" Destroy Failure!\n");
}
for (i = 0; i< 5;i++)
{
//jin dui
ret = EnterQueue(&queue,i+1);
if (ret == FAILURE)
{
printf("enter failure!\n");
}
else if (ret == SUCCESS)
{
printf("enter %d success\n",i+1);
}
}
return 0;
}
2.queue.c
#include "queue.h"
#include <stdlib.h>
int InitQueue(Q *q)
{
Node *p = (Node *)malloc(sizeof(Node));
if (NULL == p)
{
return FAILURE;
}
p->next = NULL;
q->front = q->rear = p;
return SUCCESS;
}
int EnterQueue(Q *q,int e)
{
if (NULL == q)
{
return FAILURE;
}
if(q->rear == NULL)
{
return FAILURE;
}
Node *p = (Node *)malloc(sizeof(Node));
if (NULL == p)
{
return FAILURE;
}
p->data = e; //shu ju yu
p->next = NULL;
q->rear->next = p;
q->rear = p; //dui wei zhi zhen xiang hou yi dong
return SUCCESS;
}
int LengthQueue(Q q)
{
int len = 0;
Node *p = q.front->next;
while (p)
{
len++;
p = p->next;
}
return len;
}
int GetFront(Q q)
{
if (q.front == q.rear)
{
return FAILURE;
}
return q.front->next->data;
}
int DelQueue(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 ClearQueue(Q* q)
{
if (NULL == q)
{
return FAILURE;
}
if (q->rear == q->front)
{
return SUCCESS;
}
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 DestroyQueue(Q* q)
{
if (NULL == q)
{
return FAILURE;
}
free(q->front);
q->rear = q->front =NULL;
return SUCCESS;
}
3.queue.h
#ifndef QUEUE_H
#define QUEUE_H
#define SUCCESS 1000
#define FAILURE 1001
struct node //biaoshi dui lie zhong yi ge jie dian
{
int data; //shu ju yu
struct node *next; //zhi zhen yu
};
typedef struct node Node;
struct queue //biao shi dui lie xin xi
{
Node *front; //duitou zhi zhen
Node *rear; //dui wei zhi zhen
};
typedef struct queue Q;
int InitQueue(Q *q);
int EnterQueue(Q *q,int e);
int GetFront(Q q);
int DelQueue(Q *q);
int DestroyQueue(Q* q);
#endif
三.今日开发中的问题汇总
太粗心导致一些小问题,要不多一个括号,要不少一个分号,自己还死活找不出来,影响学习心情。
四.今日开发收货
1.对队列有了一些基础的概念和认识,能明白代码实现的功能和意义,
2.能跟上老师的节奏,完整完成今日的学习任务