Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1176064
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 14:51:51


点击(此处)折叠或打开

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

  4. #define SZ_STRING 32

  5. typedef char STRING[SZ_STRING];

  6. /*链式栈存储结构:栈元素节点的定义*/
  7. /*好处:栈结点元素的成员可以方便扩充*/
  8. typedef struct stack
  9. {
  10.     STRING string;
  11.     struct stack * next;
  12. }STACKNODE;

  13. typedef STACKNODE * StackNodePtr;

  14. /*栈的要素的定义*/
  15. typedef struct linkstack
  16. {
  17.     StackNodePtr top; /*栈顶指针:始终指向链表头节点*/
  18.     StackNodePtr base; /*栈底指针*/
  19. }LINKSTACK;

  20. int InitSatck(LINKSTACK * linkstack);
  21. int StackIsEmpty(LINKSTACK linkstack);
  22. int CreatStackNode(StackNodePtr * stacknodeptr, STRING string);
  23. int Push(LINKSTACK * linkstack, STRING string);
  24. int Pop(LINKSTACK * linkstack, STRING string);
  25. int StackLen(LINKSTACK linkstack);
  26. int PrintSatck(LINKSTACK linkstack);
  27. int GetStackHead(LINKSTACK linkstack, STRING string);
  28. int DestroyStack(LINKSTACK * linkstack);

  29. int main(int argc, char ** argv)
  30. {
  31.     /*
  32.     链式存储栈的特点:
  33.     1,栈空间是动态分配的,每一个栈的节点都包括真实的数据域和指针域
  34.     2,栈顶指针:指向链表的空头节点,空头节点的下一个元素就是栈顶元素
  35.     3,栈底指针:指向链表的最后一个节点
  36.     3,栈顶元素始终是头节点的下一个元素
  37.     4,栈底元素始终是链表的最后一个元素
  38.     */
  39.     printf("链式存储栈的实现!\n");
  40.     
  41.     LINKSTACK linkstack;
  42.     InitSatck(&linkstack);
  43.     
  44.     int flag = -1;
  45.     flag = StackIsEmpty(linkstack);
  46.     if(flag == 1)
  47.         printf("是空栈!\n");
  48.     else if(flag == 2)
  49.         printf("是非空栈!\n");
  50.     
  51.     Push(&linkstack, "AAA");
  52.     
  53.     flag = StackIsEmpty(linkstack);
  54.     if(flag == 1)
  55.         printf("是空栈!\n");
  56.     else if(flag == 2)
  57.         printf("是非空栈!\n");
  58.         
  59.     Push(&linkstack, "BBB");
  60.     Push(&linkstack, "CCC");
  61.     Push(&linkstack, "DDD");
  62.     Push(&linkstack, "EEE");
  63.     Push(&linkstack, "FFF");
  64.     Push(&linkstack, "GGG");
  65.     
  66.     PrintSatck(linkstack);
  67.     
  68.     STRING string;
  69.     memset(string, 0, sizeof(STRING));
  70.     Pop(&linkstack, string);
  71.     printf("出栈元素1=[%s]\n", string);
  72.     memset(string, 0, sizeof(STRING));
  73.     Pop(&linkstack, string);
  74.     printf("出栈元素2=[%s]\n", string);
  75.     PrintSatck(linkstack);
  76.     
  77.     memset(string, 0, sizeof(STRING));
  78.     Pop(&linkstack, string);
  79.     memset(string, 0, sizeof(STRING));
  80.     GetStackHead(linkstack, string);
  81.     printf("取得栈顶元素1=[%s]\n", string);
  82.     memset(string, 0, sizeof(STRING));
  83.     Pop(&linkstack, string);
  84.     memset(string, 0, sizeof(STRING));
  85.     GetStackHead(linkstack, string);
  86.     printf("取得栈顶元素2=[%s]\n", string);
  87.     //DestroyStack(&linkstack);
  88.     //DestroyStack(&linkstack);
  89.     //DestroyStack(&linkstack);
  90.     //PrintSatck(linkstack);
  91.     memset(string, 0, sizeof(STRING));
  92.     Pop(&linkstack, string);
  93.     memset(string, 0, sizeof(STRING));
  94.     Pop(&linkstack, string);
  95.     memset(string, 0, sizeof(STRING));
  96.     Pop(&linkstack, string);
  97.     memset(string, 0, sizeof(STRING));
  98.     Pop(&linkstack, string);
  99.     memset(string, 0, sizeof(STRING));
  100.     Pop(&linkstack, string);
  101.     
  102.     PrintSatck(linkstack);
  103.     
  104.     return 0;
  105. }

  106. /*功能:判断链式栈是否为空,1:空, 2:非空*/
  107. int StackIsEmpty(LINKSTACK linkstack)
  108. {
  109.     if(linkstack.top == linkstack.base)
  110.         return 1;
  111.     else
  112.         return 2;
  113. }

  114. /*功能;初始化链栈,即创建空表头(空表头不是栈顶元素)*/
  115. int InitSatck(LINKSTACK * linkstack)
  116. {
  117.     linkstack->base = (StackNodePtr)malloc(sizeof(STACKNODE));
  118.     if(linkstack->base == NULL)
  119.     {
  120.         printf("InitSatck malloc err!\n");
  121.         exit(-1);
  122.     }
  123.     linkstack->top = linkstack->base; /*栈顶指针始终指向空头节点*/
  124.     memset(linkstack->base->string, 0 , sizeof(STRING)); /*空头*/
  125.     linkstack->base->next = NULL;
  126.     
  127.     return 0;
  128. }

  129. /*功能:创建一个栈节点*/
  130. int CreatStackNode(StackNodePtr * stacknodeptr, STRING string)
  131. {
  132.     *stacknodeptr = (StackNodePtr)malloc(sizeof(STACKNODE));
  133.     if(*stacknodeptr == NULL)
  134.     {
  135.         printf("CreatStackNode malloc失败\n");
  136.         exit(-1);
  137.     }
  138.     memset((*stacknodeptr)->string, 0 , sizeof(STRING));
  139.     strcpy((*stacknodeptr)->string, string);
  140.     (*stacknodeptr)->next = NULL;
  141.     return 0;
  142. }

  143. /*功能:求链栈中元素的个数*/
  144. int StackLen(LINKSTACK linkstack)
  145. {
  146.     int len = 0;
  147.     StackNodePtr p = NULL;
  148.     p = linkstack.top;
  149.     if(p == NULL)
  150.     {
  151.         //printf("链式栈不存在,或者已经销毁!\n");
  152.         return len;
  153.     }
  154.     while(p->next != NULL)
  155.     {
  156.         len++;
  157.         p = p->next;
  158.     }
  159.     return len;
  160. }

  161. /*功能:入栈操作:从栈顶加入元素*/
  162. int Push(LINKSTACK * linkstack, STRING string)
  163. {
  164.     StackNodePtr stacknodeptr = NULL;
  165.     CreatStackNode(&stacknodeptr, string);
  166.     
  167.     /*新插入的栈节点,在空表头之后,作为新的栈顶元素*/
  168.     if(StackIsEmpty(*linkstack) == 1) /*空栈*/
  169.     {
  170.         linkstack->base = stacknodeptr;
  171.     }

  172.     stacknodeptr->next = linkstack->top->next; /*新元素连接到第一个元素之后*/
  173.     linkstack->top->next = stacknodeptr; /*空头指向新连接的元素*/
  174.     
  175.     printf("入栈一个元素=[%s],链栈中元素个数=[%d]\n", stacknodeptr->string, StackLen(*linkstack));
  176.     
  177.     return 0;
  178. }

  179. /*功能:出栈操作:栈顶元素删除*/
  180. int Pop(LINKSTACK * linkstack, STRING string)
  181. {
  182.     if(StackIsEmpty(*linkstack) == 1) /*空栈*/
  183.     {
  184.         printf("栈为空,无元素可出栈!\n");
  185.         return -1;
  186.     }
  187.     /*将栈顶元素出栈,栈顶元素即是空表头之后的第一个节点元素*/
  188.     StackNodePtr stempnodeptr = linkstack->top->next; /*指向第一个节点元素*/
  189.     if(linkstack->top->next == linkstack->base) /*当栈中只有一个栈底元素时*/
  190.     {
  191.         linkstack->base = linkstack->top;
  192.     }
  193.     linkstack->top->next = linkstack->top->next->next;
  194.     
  195.     strcpy(string, stempnodeptr->string);
  196.     printf("出栈一个元素=[%s],链栈中剩余元素个数=[%d]\n", stempnodeptr->string, StackLen(*linkstack));
  197.     free(stempnodeptr);
  198.     stempnodeptr == NULL;
  199.     
  200.     return 0;
  201. }

  202. /*功能:打印栈中所有元素*/
  203. int PrintSatck(LINKSTACK linkstack)
  204. {
  205.     int num = 0;
  206.     
  207.     if(StackIsEmpty(linkstack) == 1) /*空栈*/
  208.     {
  209.         printf("栈为空,无需打印!\n");
  210.         return 1;
  211.     }
  212.     printf("链式栈中元素个数=[%d]\n", StackLen(linkstack));
  213.     
  214.     StackNodePtr p = NULL;
  215.     p = linkstack.top;
  216.     printf("从链表头,即栈顶开始:\n");
  217.     while(p->next != NULL)
  218.     {
  219.         num++;
  220.         p = p->next;
  221.         printf("(第%02d个元素)=[%s]\n", num, p->string);    
  222.     }

  223.     return 0;
  224. }

  225. /*功能:取得栈顶元素*/
  226. int GetStackHead(LINKSTACK linkstack, STRING string)
  227. {
  228.     if(linkstack.top == linkstack.base)
  229.     {
  230.         printf("栈为空,无栈顶元素可取!\n");
  231.         return -1;
  232.     }
  233.     strcpy(string, linkstack.top->next->string);
  234.     
  235.     return 0;
  236. }

  237. /*功能:销毁栈链表*/
  238. int DestroyStack(LINKSTACK * linkstack)
  239. {
  240.     StackNodePtr p = NULL;
  241.     StackNodePtr q = NULL;
  242.     p = linkstack->top;
  243.     q = linkstack->top;
  244.     if(p == NULL)
  245.     {
  246.         printf("链式队列不存在,或者已经销毁!\n");
  247.         return 0;
  248.     }
  249.     while(q->next != NULL)
  250.     {
  251.         p = q->next;
  252.         free(q);
  253.         q = p;
  254.     }
  255.     free(p);
  256.     p = NULL;
  257.     q = NULL;
  258.     linkstack->top = NULL;
  259.     linkstack->base = NULL;
  260.     
  261.     printf("链式栈已销毁!\n");
  262.     
  263.     return 0;
  264. }

阅读(476) | 评论(0) | 转发(0) |
0

上一篇:顺序队列

下一篇:顺序栈

给主人留下些什么吧!~~