队列的链式表示和实现
2016-08-24 15:29
246 查看
队列的链式表示和实现
16年3月2日19:56:24
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Qnode {
int data;
struct Qnode *pNext;
}QNODE, *PQNODE;
typedef struct {
PQNODE front;
PQNODE rear;
}LinkQueue;
/**
* Initialise the Queue. Attention : return the address of the Queue. Don't use the void .
* @param Q [the LinkQueue]
* @return [if success, return the address of the queue]
*/
LinkQueue * InitQueue (LinkQueue *Q)
{
Q->front = Q->rear = (PQNODE)malloc(sizeof(QNODE));
if (!Q->front || !Q->rear)
{
printf("Cannot malloc memory for Q->fornt or Q->rear.\n");
exit(-1);
}
Q->front->pNext = NULL;
return Q;
}
/**
* Insert the entry to the front of the queue.
* @param Q [the LinkQueue]
* @param val [the value of the entry]
* @return [if success, return 1]
*/
int EnQueue(LinkQueue *Q, int val)
{
PQNODE pTmp;
pTmp = (PQNODE)malloc(sizeof(QNODE));
if (!pTmp)
{
printf("Can not malloc memory for pTmp.\n");
exit(-1);
}
pTmp->data = val;
pTmp->pNext = NULL;
Q->rear->pNext = pTmp;
Q->rear = pTmp;
return 1;
}
/**
* Judge the LinkQueue is empty or not.
* @param Q [the LinkQueue]
* @return [if the linkqueue is empty, return 1, else return 0]
*/
int IsQueueEmpty(const LinkQueue *Q)
{
return Q->front == Q->rear;
}
/**
* Remove the entry from the rear of the linkqueue.
* @param Q [the linkqueue]
* @param val [the val of the removed entry]
* @return [if success, return 1]
*/
int DeQueue(LinkQueue *Q, int *val)
{
PQNODE pTmp;
if (IsQueueEmpty(Q))
{
printf("The Queue is empty.\n");
exit(-1);
}
pTmp = Q->front->pNext;
*val = pTmp->data;
Q->front->pNext = pTmp->pNext;
free(pTmp);
return 1;
}
/**
* Ruturn the length of the linkqueue.
* @param Q [the linkqueue]
* @return [the length]
*/
int TheLengthOfQueue(LinkQueue Q)
{
int i = 0;
PQNODE pTmp;
pTmp = Q.front;
while (pTmp != Q.rear)
{
i++;
pTmp = pTmp->pNext;
}
return i;
}
/**
* Traverse the linkqueue.
* @param Q [the linkqueue]
*/
void TraverseQueue(LinkQueue Q)
{
PQNODE pTmp = Q.front->pNext;
while (pTmp)
{
printf("%d ", pTmp->data);
pTmp = pTmp->pNext;
}
printf("\n");
}
int main(int argc, char const *argv[])
{
LinkQueue Q;
int val;
InitQueue(&Q);
EnQueue(&Q, 1);
EnQueue(&Q, 2);
EnQueue(&Q, 3);
EnQueue(&Q, 4);
TraverseQueue(Q);
printf("TheLengthOfQueue is %d. \n", TheLengthOfQueue(Q));
if (DeQueue(&Q, &val))
{
printf("DeQueue success, the val is %d.\n", val);
}
if (DeQueue(&Q, &val))
{
printf("DeQueue success, the val is %d.\n", val);
}
printf("TheLengthOfQueue is %d. \n", TheLengthOfQueue(Q));
return 0;
}
P { margin-bottom: 0.21cm; }
程序运行结果是:
1
2 3 4
TheLengthOfQueue
is 4.
DeQueue
success, the val is 1.
DeQueue
success, the val is 2.
TheLengthOfQueue
is 2.
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(117) | 评论(0) | 转发(0) |
0
上一篇:栈的应用之行编辑程序
下一篇:sublime text 3103从新安装中ctags配置
相关热门文章
SHTML是什么_SSI有什么用...
卡尔曼滤波的原理说明...
shell中字符串操作
关于java中的“错误:找不到或...
linux设备驱动归纳总结...
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
16年3月2日19:56:24
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Qnode {
int data;
struct Qnode *pNext;
}QNODE, *PQNODE;
typedef struct {
PQNODE front;
PQNODE rear;
}LinkQueue;
/**
* Initialise the Queue. Attention : return the address of the Queue. Don't use the void .
* @param Q [the LinkQueue]
* @return [if success, return the address of the queue]
*/
LinkQueue * InitQueue (LinkQueue *Q)
{
Q->front = Q->rear = (PQNODE)malloc(sizeof(QNODE));
if (!Q->front || !Q->rear)
{
printf("Cannot malloc memory for Q->fornt or Q->rear.\n");
exit(-1);
}
Q->front->pNext = NULL;
return Q;
}
/**
* Insert the entry to the front of the queue.
* @param Q [the LinkQueue]
* @param val [the value of the entry]
* @return [if success, return 1]
*/
int EnQueue(LinkQueue *Q, int val)
{
PQNODE pTmp;
pTmp = (PQNODE)malloc(sizeof(QNODE));
if (!pTmp)
{
printf("Can not malloc memory for pTmp.\n");
exit(-1);
}
pTmp->data = val;
pTmp->pNext = NULL;
Q->rear->pNext = pTmp;
Q->rear = pTmp;
return 1;
}
/**
* Judge the LinkQueue is empty or not.
* @param Q [the LinkQueue]
* @return [if the linkqueue is empty, return 1, else return 0]
*/
int IsQueueEmpty(const LinkQueue *Q)
{
return Q->front == Q->rear;
}
/**
* Remove the entry from the rear of the linkqueue.
* @param Q [the linkqueue]
* @param val [the val of the removed entry]
* @return [if success, return 1]
*/
int DeQueue(LinkQueue *Q, int *val)
{
PQNODE pTmp;
if (IsQueueEmpty(Q))
{
printf("The Queue is empty.\n");
exit(-1);
}
pTmp = Q->front->pNext;
*val = pTmp->data;
Q->front->pNext = pTmp->pNext;
free(pTmp);
return 1;
}
/**
* Ruturn the length of the linkqueue.
* @param Q [the linkqueue]
* @return [the length]
*/
int TheLengthOfQueue(LinkQueue Q)
{
int i = 0;
PQNODE pTmp;
pTmp = Q.front;
while (pTmp != Q.rear)
{
i++;
pTmp = pTmp->pNext;
}
return i;
}
/**
* Traverse the linkqueue.
* @param Q [the linkqueue]
*/
void TraverseQueue(LinkQueue Q)
{
PQNODE pTmp = Q.front->pNext;
while (pTmp)
{
printf("%d ", pTmp->data);
pTmp = pTmp->pNext;
}
printf("\n");
}
int main(int argc, char const *argv[])
{
LinkQueue Q;
int val;
InitQueue(&Q);
EnQueue(&Q, 1);
EnQueue(&Q, 2);
EnQueue(&Q, 3);
EnQueue(&Q, 4);
TraverseQueue(Q);
printf("TheLengthOfQueue is %d. \n", TheLengthOfQueue(Q));
if (DeQueue(&Q, &val))
{
printf("DeQueue success, the val is %d.\n", val);
}
if (DeQueue(&Q, &val))
{
printf("DeQueue success, the val is %d.\n", val);
}
printf("TheLengthOfQueue is %d. \n", TheLengthOfQueue(Q));
return 0;
}
P { margin-bottom: 0.21cm; }
程序运行结果是:
1
2 3 4
TheLengthOfQueue
is 4.
DeQueue
success, the val is 1.
DeQueue
success, the val is 2.
TheLengthOfQueue
is 2.
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(117) | 评论(0) | 转发(0) |
0
上一篇:栈的应用之行编辑程序
下一篇:sublime text 3103从新安装中ctags配置
相关热门文章
SHTML是什么_SSI有什么用...
卡尔曼滤波的原理说明...
shell中字符串操作
关于java中的“错误:找不到或...
linux设备驱动归纳总结...
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
相关文章推荐
- 链队列——队列的链式表示与实现
- C语言 队列的链式结构的实现与表示 数据结构 队列的实现与表示
- 数据结构之队列的链式表示及其实现
- 数据结构---C语言之队列的链式表示和实现(链队列)
- 队列的链式表示与实现
- 数据结构(6): 链队——队列的链式表示和实现
- 数据结构之队列的链式表示和实现
- 队列的链式表示和实现----循环队列
- 链队列-队列的链式表示和实现
- 队列的链式表示和实现
- 链式队列的表示和实现
- 链队列---队列的链式表示和实现
- 数据结构(C语言版)---第三章栈和队列 3.4.2 队列的链式表示和实现(单链表)
- 链式队列的表示和实现
- 《链队列---队列的链式表示和实现》
- 队列的链式表示和实现(数据结构)
- 链队列——队列的链式表示和实现
- C_栈和队列(ADT)-队列的链式表示和实现
- 队列的链式表示和实现
- 队列的链式表示和实现----单链队列