Chinaunix首页 | 论坛 | 博客
  • 博客访问: 565483
  • 博文数量: 127
  • 博客积分: 1169
  • 博客等级: 少尉
  • 技术积分: 1298
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-16 14:29
个人简介

空白

文章分类

全部博文(127)

分类: C/C++

2012-03-11 19:55:27


  1. /*
  2.     时间:2012年3月11日 19:51:58
  3.     目的:学习数据结构,学校c语言
  4.     功能:栈的链式结构实现
  5. */

  6. #include <stdio.h>
  7. #include <stdlib.h>

  8. #define OK 1
  9. #define FALSE 0
  10. #define TRUE 1
  11. #define ERROR 0

  12. typedef int Status;
  13. typedef int SElemType;

  14. //结点类型
  15. typedef struct Node
  16. {
  17.     SElemType data;
  18.     struct Node *next;
  19. }StackNode, *PStackNode;

  20. //链表类型
  21. typedef struct LinkStack
  22. {
  23.     StackNode * base; //栈底指针
  24.     StackNode * top; //栈顶指针
  25. } LinkStack, *PLinkStack;

  26. Status InitLinkStack(LinkStack *s);

  27. Status EmptyLinkStack(LinkStack *s);

  28. void ClearLinkStack(LinkStack *s);

  29. void DestroyStack(LinkStack *s);

  30. int StackLength(LinkStack *s);

  31. Status GetTop(LinkStack *s, SElemType *e);

  32. Status Push(LinkStack *s, SElemType e);

  33. Status Pop(LinkStack *s, SElemType *e);

  34. void TrasverLinkStack(LinkStack *s);

  35. Status InitLinkStack(LinkStack *s)
  36. {
  37.     s->base = (PStackNode)malloc(sizeof(StackNode));
  38.     if (NULL == s->base)
  39.     {
  40.         printf("内存分配失败,程序退出\n");
  41.         exit(-1);
  42.     }
  43.     s->top = s->base;
  44.     s->base->next = NULL;

  45.     return OK;
  46. }

  47. Status EmptyLinkStack(LinkStack *s)
  48. {
  49.     if (s->base == s->top)
  50.         return TRUE;
  51.     else
  52.         return ERROR;
  53. }

  54. void ClearLinkStack(LinkStack *s)
  55. {
  56.     if (s->top == s->base)
  57.     {
  58.         exit(-1);
  59.     }

  60.     StackNode *p = s->top, *q;
  61.     while(p != s->base)
  62.     {
  63.         q = p->next;
  64.         free(p);
  65.         p = q;
  66.     }
  67.     s->top = s->base;
  68. }

  69. void DestroyStack(LinkStack *s)
  70. {
  71.     StackNode *p = s->top, *q;

  72.     while (p != s->base)
  73.     {
  74.         q = p->next;
  75.         free(p);
  76.         q = p;
  77.     }
  78.     s->top = s->base;
  79.     free(s->base);
  80.     s->base = NULL;
  81.     s->top = NULL;
  82. }

  83. int StackLength(LinkStack *s)
  84. {
  85.     int i = 0;
  86.     StackNode *p = s->top;
  87.     while (p != s->base)
  88.     {
  89.         i++;
  90.         p = p->next;
  91.     }
  92.     return i;
  93. }

  94. Status GetTop(LinkStack *s, SElemType *e)
  95. {
  96.     if (EmptyLinkStack(s))
  97.     {
  98.         printf("栈为空\n");
  99.         exit(-1);
  100.     }
  101.     *e = s->top->data;
  102.     return OK;
  103. }

  104. Status Push(LinkStack *s, SElemType e)
  105. {
  106.     PStackNode r = (PStackNode)malloc(sizeof(StackNode)); //建立新结点
  107.     if (NULL == r)
  108.     {
  109.         printf("新结点建立失败,程序退出\n");
  110.         exit(-1);
  111.     }
  112.     r->data = e;
  113.     r->next = s->top;
  114.     s->top = r;

  115.     return OK;
  116. }

  117. Status Pop(LinkStack *s, SElemType *e)
  118. {
  119.     
  120.     if (s->top == s->base)
  121.     {
  122.         printf("空栈\n");
  123.         exit(-1);
  124.     }

  125.     StackNode *q = s->top;
  126.     *e = q->data;
  127.     s->top = q->next;
  128.     free(q);
  129.     q = NULL;
  130.     
  131.     return OK;
  132. }

  133. void TrasverLinkStack(LinkStack *s)
  134. {
  135.     if (EmptyLinkStack(s))
  136.     {
  137.         printf("空栈\n");
  138.         exit(-1);
  139.     }

  140.     StackNode *p = s->top;
  141.     while (p != s->base)
  142.     {
  143.         printf("%d ", p->data);
  144.         p = p->next;
  145.     }

  146.     printf("\n");
  147. }

  148. int main(void)
  149. {
  150.     LinkStack s;
  151.     SElemType e;
  152.     
  153.     //初始化栈
  154.     if (InitLinkStack(&s))
  155.     {
  156.         printf("--->栈初始化成功\n");
  157.     }
  158.     else
  159.     {
  160.         printf("栈初始化失败\n");
  161.     }
  162.     printf("--->栈中元素个数: %d\n", StackLength(&s));

  163.     //入栈
  164.     Push(&s, 1);
  165.     Push(&s, 2);
  166.     Push(&s, 3);
  167.     Push(&s, 4);
  168.     Push(&s, 5);
  169.     Push(&s, 6);
  170.     printf("--->栈中元素个数: %d\n", StackLength(&s));
  171.     TrasverLinkStack(&s);

  172.     //清空栈
  173.     printf("--->清空栈\n");
  174.     ClearLinkStack(&s);
  175.     printf("--->栈中元素个数: %d\n", StackLength(&s));

  176.     
  177.     Push(&s, 2);
  178.     Push(&s, 4);
  179.     Push(&s, 5);
  180.     Push(&s, 6);
  181.     Push(&s, 8);
  182.     Push(&s, 9);
  183.     
  184.     //获取栈顶元素
  185.     TrasverLinkStack(&s);
  186.     GetTop(&s, &e);
  187.     printf("--->栈顶元素: %d\n", e);
  188.     
  189.     //出栈
  190.     printf("--->栈中元素个数: %d\n", StackLength(&s));
  191.     while (!EmptyLinkStack(&s))
  192.     {
  193.         Pop(&s, &e);
  194.         printf("出栈%d\n", e);
  195.     }
  196.     printf("--->栈中元素个数: %d\n", StackLength(&s));
  197.     TrasverLinkStack(&s);
  198.     
  199.     DestroyStack(&s);    
  200.     return 0;
  201. }

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