队列的顺序结构和链式结构定义
来源:互联网 发布:淘宝网二手手机回收 编辑:程序博客网 时间:2024/05/17 08:17
队列的顺序结构:
SeQueue.h
#ifndef _SEQUEUE_H_
#define _SEQUEUE_H_
#define MAXSIZE 1000
#define FAILURE 100001
#define SUCCESS 100002
typedef int ElemType;
struct queue
{
ElemType data[MAXSIZE];
int front;
int rear;
};
typedef struct queue SeQueue;
int QueueInit(SeQueue *Q);
int EnQueue(SeQueue *Q,ElemType e);
int QueueLength(SeQueue Q);
ElemType GetFront(SeQueue Q);
ElemType DelQueue(SeQueue *Q);
#endif
SeQueue.c
#include <stdio.h>
#include "SeQueue.h"
int QueueInit(SeQueue *Q)
{
Q->rear = Q->front = 0;
return SUCCESS;
}
int EnQueue(SeQueue *Q,ElemType e)
{
if((Q->rear +1) % MAXSIZE == Q->front)
{
return FAILURE;
}
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1)% MAXSIZE;
return SUCCESS;
}
int QueueLength(SeQueue Q)
{
return ((Q.rear - Q.front + MAXSIZE) % MAXSIZE);
}
ElemType GetFront(SeQueue Q)
{
return Q.data[Q.front];
}
ElemType DelQueue(SeQueue *Q)
{
ElemType e;
if(Q->rear == Q->front)
{
return FAILURE;
}
e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return e;
}
TestSeQueue.c
#include <stdio.h>
#include "SeQueue.h"
int main()
{
int ret,i;
SeQueue sq;
ret = QueueInit(&sq);
if(FAILURE == ret)
{
printf("Init Failure!\n");
}
else
{
printf("Init Success!!\n");
}
for(i = 0;i < 10; i++)
{
ret = EnQueue(&sq,i+1);
if(FAILURE == ret)
{
printf("Enter Failure!!\n");
}
else
{
printf("Enter %d Success!!\n",i+1);
}
}
printf("Length is %d!\n",QueueLength(sq));
printf("Front Element is %d!\n",GetFront(sq));
for(i = 0; i < 5; i++)
{
printf("Delete %d Success!!\n",DelQueue(&sq));
}
printf("Length is %d!\n",QueueLength(sq));
return 0;
}
队列的链式机构:
LinkQueue.h
#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_
#define SUCCESS 1000001
#define FAILURE 1000002
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node *next;
}Node;
typedef struct queue
{
Node * front;
Node * rear;
}Queue;
int InitQueue(Queue *L);
int DeQueue(Queue *L,ElemType e);
int QueueLength(Queue L);
int GetTop(Queue L);
int DeleteQueue(Queue *L);
int ClearQueue(Queue *L);
int DestryQueue(Queue *L);
#endif
LinkQueue.c
#include <stdio.h>
#include"LinkQueue.h"
#include<stdlib.h>
int InitQueue(Queue *L)
{
L->front = (Node *)malloc(sizeof(Node));
if(L == NULL)
{
return FAILURE;
}
L->front->next = NULL;
L->rear = L->front;
return SUCCESS;
}
int DeQueue(Queue *L,ElemType e)
{
Node *n = (Node *)malloc(sizeof(Node));
if(n == NULL)
{
return FAILURE;
}
n->data = e;
n->next = NULL;
L->rear->next = n;
L->rear = n;
return SUCCESS;
}
int QueueLength(Queue L)
{
Node *n = L.front;
int j = 0;
while(n->next != NULL)
{
j++;
n = n->next;
}
return j;
}
int GetTop(Queue L)
{
if(L.front->next == NULL)
{
return FAILURE;
}
else
return L.front->next->data;
}
int DeleteQueue(Queue *L)
{
ElemType e;
Node *p = L->front->next;
if(NULL == p)
{
return FAILURE;
}
L->front->next = p->next;
e = p->data;
free(p);
if(NULL == L->front->next)
{
L->rear = L->front;
}
return e;
}
int ClearQueue(Queue *L)
{
ElemType e;
Node *p = L->front->next;
if(NULL == p)
{
return FAILURE;
}
while(p != NULL)
{
L->front->next = p->next;
free(p);
p=L->front->next;
if(NULL == L->front->next)
{
L->rear = L->front;
}
}
return SUCCESS;
}
int DestroyQueue(Queue *L)
{
free(L->front);
L->rear = NULL;
L->front = NULL;
return SUCCESS;
}
TestLinkQueue.c
#include <stdio.h>
#include "LinkQueue.h"
int main()
{
int ret,i;
Queue sq;
ElemType e;
ret = InitQueue(&sq);
if(ret == FAILURE)
{
printf("Init Faliure!\n");
}
else
{
printf("Init Success!\n");
}
for(i = 0; i < 5; i++)
{
e = i+1;
ret = DeQueue(&sq,e);
if(ret == FAILURE)
{
printf("Enter FAILURE!\n");
}
else
{
printf("Enter %d Success!\n",e);
}
}
printf("Length is %d \n",QueueLength(sq));
printf("Get Top is %d \n",GetTop(sq));
for(i = 0; i < 6; i++)
{
ret = DeleteQueue(&sq);
if(ret == FAILURE)
{
printf("Delete Failure!!\n");
}
else
{
printf("Delete %d Success!!\n",ret);
}
}
ret = ClearQueue(&sq);
if(ret == FAILURE)
{
printf("Clear Failure!!\n");
}
else
{
printf("Clear Success!!\n");
}
printf("Length is %d \n",QueueLength(sq));
ret = DestroyQueue(&sq);
if(ret == FAILURE)
{
printf("Destory FAILURE!!\n");
}
else
{
printf("Destory Success!!\n");
}
return 0;
}
- 队列的顺序结构和链式结构定义
- 队列的顺序存储结构和链式存储结构
- 数据结构:顺序结构和链式结构的数据类型定义
- 队列的链式结构
- Python 数据结构 之 队列 的顺序、链式存储结构
- 栈的顺序存储结构和链式存储结构
- 比较顺序存储结构和链式存储结构的优缺点
- 顺序存储结构和链式存储结构的选择
- 顺序存储结构和链式存储结构的优缺点
- 栈的顺序存储结构和链式存储结构
- 顺序存储结构和链式存储结构的优缺点
- 顺序存储结构和链式存储结构的优缺点比较
- Day17、链式存储结构动态分配内存、栈的顺序存储结构和链式存储结构
- 表之顺序结构和链式结构
- 栈之顺序结构和链式结构
- 队列的链式存储结构
- 队列的链式存储结构
- 队列的链式存储结构
- Mac升级到10.13.1 之后cocoapods出现问题的解决办法
- PSNR峰值信噪比和 DMOS算法介绍
- 深度机器学习56G视频资源分享
- myeclipse项目导入到eclipse
- 我的项目经验总结——CDN镜像:1(初探)
- 队列的顺序结构和链式结构定义
- Vmware安装CentOS7后访问不了外网
- 利用公网ip盒子搭建自己的kod云资源管理器
- springmvc中前台页面form表单提交方法为post,但是报HTTP Status 405
- bond
- Codeforces Round #449 (Div. 2) C. Nephren gives a riddle (DFS)
- 基于分布式环境下限流系统的设计
- 清除浮动clear样式
- JPush(极光)推送相关整理