姓名:刘姻
日期:9.5
今日学习任务
有关队列的程序编写;
循环队列,队列的清空;
今日任务完成情况
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<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 (FAILURE==ret)
{
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);
}
}
ret = ClearQueue(&queue);
if (ret == FAILURE)
{
printf("Clear Failure!\n");
}
else
{
printf("Clear Success!\n");
}
ret = DestroyQueue(&queue);
if (ret == SUCCESS)
{
printf("Destroy Success!\n");
}
else if(ret == FAILURE)
{
printf("Destroy Failure!\n");
}
return 0;
}
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 ClearQueue(Q *q);
#endif
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;
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)
{
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);
q->front = q->rear = NULL;
return SUCCESS;
}
今日遇到的问题
对队列的链式结构没有熟练掌握;
编写程序出现很多错误;
今日未解决的问题
对队列相关程序的编写没有掌握
今日开发所获
编写了很多关于队列的程序,对队列有了初步理解
自我评价
程序可以跟着老师打,但不能独立编写