zoukankan      html  css  js  c++  java
  • 数据结构之栈的实现

    View Code
     1 #include <stdio.h>
     2 #include "SqStack.h"
     3 #include <stdlib.h>
     4 int main()
     5 {
     6     SqStack s;
     7     InitStack(s);
     8     int num;
     9     int e;
    10 scanf("%d",&num);
    11 DtoBcovert(s,num);
    12 while(!IsEmpty(s))
    13 {
    14     Pop(s,e);
    15     printf("%d",e);
    16 }
    17 printf("\n");
    18     
    19     
    20 
    21     return 0;    
    22 }
    23 
    24 /*
    25 **构造空栈
    26 */
    27 int InitStack(SqStack &s)
    28 {
    29     s.base = (SElemType *)malloc(InitStackSize*sizeof(SElemType));
    30     if(s.base  == NULL) return 0;
    31     s.top = s.base;
    32     s.stacksize = InitStackSize;
    33     return 1;
    34 }
    35 
    36 /* 
    37 **输出栈顶元素
    38 */
    39 
    40 int GetTop(SqStack s,SElemType &e)
    41 {
    42     if(s.top == s.base) return 0;
    43     e = *(s.top - 1);
    44     return OK;
    45 }
    46 
    47 /*
    48 **元素入栈
    49 */
    50 
    51 int Push(SqStack &s,SElemType e)
    52 {
    53     if(s.top >=s.base + s.stacksize)
    54     {
    55         s.base = (SElemType*)realloc(s.base,(InitStackSize + InitCream)*sizeof(SElemType)) ;
    56         if(s.base == NULL) return 0;
    57     s.top = s.base + s.stacksize;
    58     s.stacksize = InitStackSize + InitCream;
    59     }
    60     
    61  *s.top = e;
    62  s.top++;
    63     return 1;
    64 }
    65 
    66 /**
    67 **元素出栈
    68 **/
    69 int Pop(SqStack &s,SElemType &e)
    70 {
    71     if(s.top == s.base) return 0;
    72     e = *(--s.top);
    73     return OK;
    74 }
    75 
    76 /*
    77 **栈的应用,十进制转二进制
    78 */
    79 
    80 void DtoBcovert(SqStack &s,int num)
    81 {
    82     while(num)
    83     {
    84         Push(s,num %2);
    85         num = num/2 ;
    86     }
    87 }
    88 int IsEmpty(SqStack s)
    89 {
    90     if(s.top ==s.base) return OK;
    91     else return 0;
    92 
    93 }
    一切源于对计算机的热爱
  • 相关阅读:
    canvas---HTML5新特性
    flex布局之兼容
    前端Blob对二进制流数据的处理方式
    execCommand的复制
    express快速入门
    react+redux+webpack+git技术栈
    react开发
    gulp工程化工具
    python---django安装
    vue+webpack+element-ui+git
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/2672668.html
Copyright © 2011-2022 走看看