手机版
你好,游客 登录 注册
背景:
阅读新闻

嵌入式C语言学习笔记

栈与队列的实现

[日期:2012-06-11] 来源:Linux社区  作者:PirateLeo [字体:大 中 小]

栈与队列的区别:

栈——先入后出,后入先出;

队列——先入先出,后入后出;

应用举例:

栈:函数调用时会将临时数据压栈;函数返回时再弹出来。

队列:一般,系统中的任务和消息经常使用队列。可以按任务或消息到来的先后顺序执行。

代码实现:

背景:使用C语言,在VS2008环境下,按栈与队列的原理,采用最简单易懂的方式,针对正整型(int)元素实现栈与队列。

原理:栈的存储空间使用数组构造,队列的空间又使用栈来构造,即,使用两个栈实现队列的功能。

文件:libStack.h/c;libQueue.h/c;

一、栈的源代码

libStack.h

  1. #ifndef _LIBSTACK_H   
  2. #define _LIBSTACK_H   
  3.   
  4. #ifdef __cplusplus   
  5. extern "C" {  
  6. #endif   
  7.   
  8. #define STACK_MAX_LEN   2048   
  9.   
  10. typedef struct tagstStack   
  11. {  
  12.     int udwPc;              /* 栈顶所在位置,指向空闲位置 */  
  13.     int a[STACK_MAX_LEN];  
  14. }stStack;  
  15.   
  16. /* 创建新的栈 */  
  17. extern stStack *stack_new();  
  18.   
  19. /* 栈复位 */  
  20. extern void stack_reset(stStack *pstStack);  
  21.   
  22. /* 入栈,栈满时忽略此次操作 */  
  23. extern void stack_push(stStack *pstStack,int udwTmp);  
  24.   
  25. /* 出栈,栈空时返回-1 */  
  26. extern int stack_pop(stStack *pstStack);  
  27.   
  28. /* 栈判空 1-空 0-非空 */  
  29. extern int stack_is_empty(stStack *pstStack);  
  30.   
  31. /* 删除栈 */  
  32. extern void stack_delete(stStack *pstStack);  
  33.   
  34. #ifdef __cplusplus   
  35. }  
  36. #endif   
  37.   
  38. #endif  

由于是C语言实现,所以必须添加extern "C"{};

libStack.c

  1. #ifdef __cplusplus   
  2. extern "C" {  
  3. #endif   
  4.   
  5. #include <malloc.h>   
  6. #include <stddef.h>   
  7. #include "libStack.h"   
  8.   
  9. /* 创建新的栈 */  
  10. stStack *stack_new()  
  11. {  
  12.     stStack *p;  
  13.     p = (stStack *)malloc(sizeof(stStack));  
  14.     p->udwPc = 0;  
  15.     return p;  
  16. }  
  17.   
  18. /* 栈复位 */  
  19. void stack_reset(stStack *pstStack)  
  20. {  
  21.     if (NULL == pstStack)  
  22.     {  
  23.         return;  
  24.     }  
  25.     pstStack->udwPc = 0;  
  26. }  
  27.   
  28. /* 入栈,栈满时忽略此次操作 */  
  29. void stack_push(stStack *pstStack, int udwTmp)  
  30. {  
  31.     if (NULL == pstStack)  
  32.     {  
  33.         return;  
  34.     }  
  35.   
  36.     if (STACK_MAX_LEN > pstStack->udwPc)  
  37.     {  
  38.         pstStack->a[pstStack->udwPc] = udwTmp;  
  39.         pstStack->udwPc++;  
  40.     }  
  41. }  
  42.   
  43. /* 出栈,栈空时返回-1 */  
  44. int stack_pop(stStack *pstStack)  
  45. {  
  46.     if (NULL == pstStack)  
  47.     {  
  48.         return -1;  
  49.     }  
  50.   
  51.     if (0 == pstStack->udwPc)  
  52.     {  
  53.         return -1;  
  54.     }  
  55.     return pstStack->a[--pstStack->udwPc];  
  56. }  
  57.   
  58. /* 栈判空 1-空 0-非空 */  
  59. int stack_is_empty(stStack *pstStack)  
  60. {  
  61.     if (NULL == pstStack)  
  62.     {  
  63.         /* 空指针默认为空栈 */  
  64.         return 1;  
  65.     }  
  66.   
  67.     return (0 < pstStack->udwPc)?0:1;  
  68. }  
  69.   
  70. /* 删除栈 */  
  71. void stack_delete(stStack *pstStack)  
  72. {  
  73.     if (NULL != pstStack)  
  74.     {  
  75.         free(pstStack);  
  76.     }  
  77. }  
  78.   
  79. #ifdef __cplusplus   
  80. }  
  81. #endif  
本套实现只支持正整型数,负数被作为异常值处理。

二、队列的实现

这里队列完全是用上述栈实现的,目的只是练习。

libQueue.h

  1. #ifndef _LIBQUEUE_H   
  2. #define _LIBQUEUE_H   
  3.   
  4. #ifdef __cplusplus   
  5. extern "C" {  
  6. #endif   
  7.   
  8. #include "libStack.h"   
  9.   
  10. #define QUEUE_MAX_LEN STACK_MAX_LEN     /* 队列大小取决与栈的大小 */   
  11.   
  12. typedef struct tagstQueue   
  13. {  
  14.     int     udwCnt;  
  15.     stStack *pstStack1;               
  16.     stStack *pstStack2;  
  17. }stQueue;  
  18.   
  19. /* 创建新队列 */  
  20. extern stQueue *queue_new();  
  21.   
  22. /* 入队,队列已满时忽略此次操作 */  
  23. extern void in_queue(stQueue *pstQueue, int udwTmp);  
  24.   
  25. /* 出队,队列为空时返回-1 */  
  26. extern int out_queue(stQueue *pstQueue);  
  27.   
  28. /* 删除队列 */  
  29. extern void queue_delete(stQueue *pstQueue);  
  30.   
  31. /* 队列判空 1-空 0-非空 */  
  32. extern int queue_is_empty(stQueue *pstQueue);  
  33.   
  34. #ifdef __cplusplus   
  35. }  
  36. #endif   
  37.   
  38. #endif  
libQueue.c
  1. #ifdef __cplusplus   
  2. extern "C" {  
  3. #endif   
  4.   
  5. #include <malloc.h>   
  6. #include <stddef.h>   
  7. #include "libQueue.h"   
  8.   
  9. /* 创建新队列 */  
  10. stQueue *queue_new()  
  11. {  
  12.     stQueue *p;  
  13.     p = (stQueue *)malloc(sizeof(stQueue));  
  14.     if (NULL == p)  
  15.     {  
  16.         return NULL;  
  17.     }  
  18.   
  19.     p->pstStack1 = stack_new();  
  20.     if (NULL == p->pstStack1)  
  21.     {  
  22.         free(p);  
  23.         return NULL;  
  24.     }  
  25.   
  26.     p->pstStack2 = stack_new();  
  27.     if (NULL == p->pstStack2)  
  28.     {  
  29.         free(p);  
  30.         free(p->pstStack1);  
  31.         return NULL;  
  32.     }  
  33.   
  34.     p->udwCnt = 0;  
  35.   
  36.     return p;  
  37. }  
  38.   
  39. /* 入队,队列已满时忽略此次操作 */  
  40. void in_queue(stQueue *pstQueue, int udwTmp)  
  41. {  
  42.     if (NULL == pstQueue)  
  43.     {  
  44.         return;  
  45.     }  
  46.   
  47.     if (QUEUE_MAX_LEN <= pstQueue->udwCnt)  
  48.     {  
  49.         return;  
  50.     }  
  51.   
  52.     stack_push(pstQueue->pstStack1, udwTmp);  
  53.     pstQueue->udwCnt++;  
  54. }  
  55.   
  56. /* 出队,队列为空时返回-1 */  
  57. int out_queue(stQueue *pstQueue)  
  58. {  
  59.     int ret = -1;  
  60.     int tmp = 0;  
  61.   
  62.     if (NULL == pstQueue)  
  63.     {  
  64.         return -1;  
  65.     }  
  66.   
  67.     /* 队列为空 */  
  68.     if (stack_is_empty(pstQueue->pstStack1))  
  69.     {  
  70.         return -1;  
  71.     }  
  72.   
  73.     /* 将栈1内容弹出并压入栈2 */  
  74.     while(0 != pstQueue->pstStack1->udwPc)  
  75.     {  
  76.         tmp = stack_pop(pstQueue->pstStack1);  
  77.         stack_push(pstQueue->pstStack2, tmp);  
  78.     }  
  79.   
  80.     /* 取栈2顶元素出队 */  
  81.     ret = stack_pop(pstQueue->pstStack2);  
  82.   
  83.     /* 将栈2内容压回栈1 */  
  84.     while(0 != pstQueue->pstStack2->udwPc)  
  85.     {  
  86.         tmp = stack_pop(pstQueue->pstStack2);  
  87.         stack_push(pstQueue->pstStack1, tmp);  
  88.     }  
  89.   
  90.     pstQueue->udwCnt--;  
  91.   
  92.     return ret;  
  93. }  
  94.   
  95. /* 删除队列 */  
  96. void queue_delete(stQueue *pstQueue)  
  97. {  
  98.     if (NULL == pstQueue)  
  99.     {  
  100.         return;  
  101.     }  
  102.   
  103.     if (NULL != pstQueue->pstStack2)  
  104.     {  
  105.         free(pstQueue->pstStack2);  
  106.     }  
  107.   
  108.     if (NULL != pstQueue->pstStack1)  
  109.     {  
  110.         free(pstQueue->pstStack1);  
  111.     }  
  112.   
  113.     free(pstQueue);  
  114. }  
  115.   
  116. /* 队列判空 1-空 0-非空 */  
  117. int queue_is_empty(stQueue *pstQueue)  
  118. {  
  119.     return (0 < pstQueue->udwCnt)?0:1;  
  120. }  
  121.   
  122. #ifdef __cplusplus   
  123. }  
  124. #endif  

三、简单的测试函数

这里我只写了队列的测试:

  1. // ListStackQueue.cpp : 定义控制台应用程序的入口点。   
  2. //   
  3. #include "stdafx.h"   
  4. #include "libStack.h"   
  5. #include "libQueue.h"   
  6.   
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9.     int i,tmp;  
  10.   
  11.     stQueue *pstQ1 = queue_new();  
  12.     stQueue *pstQ2 = queue_new();  
  13.   
  14.     for (i = 0; i < 20; i++)  
  15.     {  
  16.         in_queue(pstQ1,i);  
  17.     }  
  18.   
  19.     for (i = 0; i < 20; i++)  
  20.     {  
  21.         tmp = out_queue(pstQ1);  
  22.         printf("%d ",tmp);  
  23.     }  
  24.   
  25.     printf("OK");  
  26.     getchar();  
  27.     return 0;  
  28. }  
输出为:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 OK;实现了队列的先入先出功能。

如果试着将栈的大小调整为5,输出为:0 1 2 3 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 OK。满足超出空间上限不压栈不入队,出栈出队时若栈队为空则返回-1。

linux
【内容导航】
第1页:几个重要关键字 第2页:栈与队列的实现
第3页:Linux中霸道的双向链表源码与应用
相关资讯       C语言 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款