学习日志3
姓名:龙村村 什么时候:2018.9.5
今天学习任务 学习队列 链式 学习相关函数的编写
今日任务完成情况:代码写完了,
今日开发中出现的问题汇总:都是前两天的小问题,漏写什么的就编译不成功
今日未解决问题:不是特别熟练
今日开发收获:能用链式 队列写一些小程序了
自我评价:代码写完,上课认真,安排的妥妥的
程序代码:
队列的顺序存储
源文件main.c
#include <stdio.h>
#include "queue.h"
#include <stdlib.h>
int main(void)
{
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("Delete Failure!\n");
}
else
{
printf("Delete %d Success!\n",ret);
}
}
//***********************************************
// printf("length is %d\n",LengthQueue(queue));
// printf("front is %d \n",GetFront(queue));
//***********************************************
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 queue success.\n");
}
else
{
printf("Clear queue failure! \n");
}
//***********************************************
ret = GetFront(queue);//获取队头元素
if (ret == FAILURE)
{
printf("Get front failure!\n");
}
else
{
printf("Front is %d \n",ret);
}
// printf("length is %d\n",LengthQueue(queue));
//***********************************************
ret = DestroyQueue(&queue);//销毁队列
if (ret == FAILURE)
{
printf("Destroy Failure!\n");
}
else
{
printf("Destroy Success.\n");
}
//***********************************************
return 0;
}
源文件queue.c
#include "queue.h"
#include <stdlib.h>
#include <stdio.h>
int InitQueue(Q *q)
{
if (NULL == q) //入参判断
{
return FAILURE;
}
q->data = (int *)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->front = q->rear;
return SUCCESS;
}
//***********************************************
int DestroyQueue(Q *q)
{
if (NULL == q) //入参判断
{
return FAILURE;
}
free(q->data); //释放空间
return SUCCESS;
}
头文件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
队列的链式存储
源文件main.c
#include <stdio.h>
#include "queue.h"
#include <stdlib.h>
int main(void)
{
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++)//进队
{
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 < 3;i++)//出队
{
ret = DelQueue(&queue);
if (ret == FAILURE)
{
printf("Delete failure!\n");
}
else
{
printf("Delete %d success!\n",ret);
}
}
//************************************************
// printf("Length is %d\n",LengthQueue(queue));
//************************************************
ret = ClearQueue(&queue);
if (ret == FAILURE)
{
printf("Clear failure!\n");
}
else
{
printf("Clear success!\n");
}
//************************************************
// printf("Length is %d\n",LengthQueue(queue));
//************************************************
ret = DestroyQueue(&queue);
if (ret == SUCCESS)
{
printf("Destroy success!\n");
}
else if (ret == FAILURE)
{
printf("Destroy failure!\n");
}
//************************************************
for (i = 0;i < 5;i++)//进队
{
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;
}
源文件queue.c
#include "queue.h"
#include <stdlib.h>
#include <stdio.h>
int InitQueue(Q *q)
{
Node *p = (Node *)malloc(sizeof(Node));
//申请头节点
if (NULL == q) //入参判断
{
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; //数据域
p->next = NULL;
q->rear->next = p;
q->rear = p; //队尾指针向后移动
return SUCCESS;
}
//************************************************
int LengthQueue(Q q)
{
int len = 0;
Node *p = q.front->next;
while (p) //while (p!=NULL)
{
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->front == q->rear)
{
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);//free(q->rear);
q->front = q->rear = NULL;
return SUCCESS;
}
//************************************************
头文件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 LengthQueue(Q q);
int GetFront(Q q);
int DelQueue(Q *q);
int ClearQueue(Q *q);
int DestroyQueue(Q *q);
#endif