Chinaunix首页 | 论坛 | 博客
  • 博客访问: 803120
  • 博文数量: 274
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 862
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-24 15:31
个人简介

不合格的程序猿

文章分类

全部博文(274)

文章存档

2019年(3)

2018年(1)

2017年(4)

2016年(160)

2015年(106)

我的朋友

分类: C/C++

2016-04-06 15:14:18

原理: 压栈时从链表头结点开始压,弹栈时从头结点往后弹。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. /* 栈的最大深度 */
  5. #define STACK_DEEP_MAX 256U

  6. typedef struct tagStackNode{
  7.     struct tagStackNode *pstNext;
  8.     int iData;
  9. } STACK_NODE_S;

  10. typedef struct tagStackHead{
  11.     STACK_NODE_S *pstNext;
  12.     int iStackDeep;
  13. } STACK_HEAD_S;

  14. static STACK_HEAD_S g_stHead;

  15. static void stack_AddHead(STACK_HEAD_S *pstHead, STACK_NODE_S *pstAddNode)
  16. {
  17.     pstAddNode->pstNext = pstHead->pstNext;
  18.     pstHead->pstNext = pstAddNode;

  19.     return;
  20. }

  21. /* 压栈 */
  22. void stack_push(STACK_HEAD_S *pstHead, int iData)
  23. {
  24.     STACK_NODE_S *pstNode;

  25.     if (NULL == pstHead || pstHead->iStackDeep > STACK_DEEP_MAX)
  26.     {
  27.         return;
  28.     }

  29.     pstNode = (STACK_NODE_S *)malloc(sizeof(STACK_NODE_S));
  30.     if (NULL != pstNode)
  31.     {
  32.         memset(pstNode, 0, sizeof(*pstNode));
  33.         pstNode->iData = iData;
  34.         stack_AddHead(pstHead, pstNode);
  35.         pstHead->iStackDeep++;
  36.     }
  37.     
  38.     return;
  39. }

  40. /* 弹栈 */
  41. int stack_popd(STACK_HEAD_S *pstHead)
  42. {
  43.     STACK_NODE_S *pstNode;
  44.     int iData;

  45.     if (NULL == pstHead || NULL == pstHead->pstNext)
  46.     {
  47.         return;
  48.     }

  49.     pstNode = pstHead->pstNext;
  50.     pstHead->pstNext = pstHead->pstNext->pstNext;
  51.     iData = pstNode->iData;
  52.     free(pstNode);
  53.     pstHead->iStackDeep--;
  54.     
  55.     return iData;
  56. }

  57. /* 获取栈深度 */
  58. int stack_deep(STACK_HEAD_S *pstHead)
  59. {
  60.     int iStackDeep = -1;
  61.     
  62.     if (NULL != pstHead)
  63.     {
  64.         iStackDeep = pstHead->iStackDeep;
  65.     }
  66.     
  67.     return iStackDeep;
  68. }

  69. /* 测试代码 */
  70. void stack_test(void)
  71. {
  72.     int iData, iLoop, iDeep, iCount;

  73.     for (iLoop = 0; iLoop < STACK_DEEP_MAX; iLoop++)
  74.     {
  75.         stack_push(&g_stHead, iLoop);
  76.     }

  77.     iCount = 0;
  78.     iDeep = stack_deep(&g_stHead);
  79.     printf("the deep of the stack is %d\n", iDeep);
  80.     for (iLoop = 0; iLoop < iDeep; iLoop++)
  81.     {
  82.         iData = stack_popd(&g_stHead);
  83.         printf("%d\t", iData);
  84.         iCount++;
  85.         if (8 == iCount)
  86.         {
  87.             iCount = 0;
  88.             printf("\n");
  89.         }
  90.     }
  91.     printf("\n");

  92.     return;
  93. }

  94. void main(void)
  95. {
  96.     stack_test();

  97.     return;
  98. }

阅读(960) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~