zoukankan      html  css  js  c++  java
  • 数据结构学习第六天

    17:07:38 2019-08-21

    学习

     18:11:59 2019-08-21

    完善了对栈 队列的测试

    栈的数组实现

    StackInArray.h

     1 #ifndef _STACK_IN_ARRAY_H
     2 #define _STACK_IN_ARRAY_H
     3 #define Empty -1
     4 struct AStack;
     5 typedef struct AStack* Stack;
     6 int IsEmpty(Stack S);
     7 int IsFull(Stack S);
     8 Stack CreatAStack(int MaxSize);
     9 void DisposeAStack(Stack S);
    10 void MakeEmpty(Stack S);
    11 void Push(int Element, Stack S);
    12 int Top(Stack S);
    13 void Pop(Stack S);
    14 int TopAndPop(Stack S);
    15 #endif // !_STACK_IN_ARRAY_H
    View Code

    StackInArray.c

     1 #include"StackInArray.h"
     2 #include<malloc.h>
     3 #include<stdio.h>
     4 struct AStack
     5 {
     6     int Capacity;
     7     int TopOfStack;
     8     int* Array;
     9 };
    10 int IsEmpty(Stack S)
    11 {
    12     return S->TopOfStack == Empty;
    13 }
    14 
    15 int IsFull(Stack S)
    16 {
    17     return S->TopOfStack == (S->Capacity - 1);
    18 }
    19 
    20 Stack CreatAStack(int MaxSize)
    21 {
    22     Stack S = (Stack)malloc(sizeof(AStack));
    23     S->Capacity = MaxSize;
    24     S->Array = (int*)malloc(sizeof(int) * S->Capacity);
    25     MakeEmpty(S);
    26     return S;
    27 }
    28 
    29 void DisposeAStack(Stack S)
    30 {
    31     if (S)
    32     {
    33         free(S->Array);
    34         free(S);
    35     }
    36 }
    37 
    38 void MakeEmpty(Stack S)
    39 {
    40     S->TopOfStack = Empty;
    41 }
    42 
    43 void Push(int Element, Stack S)
    44 {
    45     if (IsFull(S))
    46     {
    47         printf("out of space
    ");
    48         return;
    49     }
    50     else
    51     {
    52         S->Array[++S->TopOfStack] = Element;
    53     }
    54 }
    55 
    56 int Top(Stack S)
    57 {
    58     if (IsEmpty(S))
    59     {
    60         printf("stack is empty
    ");
    61         return -1;
    62     }
    63     else
    64     {
    65         return S->Array[S->TopOfStack];
    66     }
    67 }
    68 
    69 void Pop(Stack S)
    70 {
    71     if (IsEmpty(S))
    72     {
    73         printf("stack is empty
    ");
    74         return;
    75     }
    76     else
    77     {
    78         S->TopOfStack--;
    79     }
    80 }
    81 
    82 int TopAndPop(Stack S)
    83 {
    84     if (IsEmpty(S))
    85     {
    86         printf("stack is empty
    ");
    87         return -1;
    88     }
    89     else
    90     {
    91         /*int Element = S->Array[S->TopOfStack];
    92         Pop(S);
    93         return Element;*/
    94         return S->Array[S->TopOfStack--];
    95     }
    96 }
    View Code

    main.c

     1 #include<stdio.h>
     2 #include"StackInArray.h"
     3 
     4 int main()
     5 {
     6     Stack S;
     7     S = CreatAStack(20);
     8     printf("%10d %10d
    ", IsEmpty(S), IsFull(S));
     9     Push(25, S);
    10     printf("%10d %10d
    ", IsEmpty(S), IsFull(S));
    11     printf("%10d
    ", Top(S));
    12     Push(30, S);
    13     printf("%10d ",TopAndPop(S));
    14     printf("%10d
    ", Top(S));
    15     Pop(S);
    16     printf("%10d
    ", IsEmpty(S));
    17     Pop(S);
    18     return 0;
    19 }
    View Code

    写的过程中注意到了 printf 对参数压栈顺序

    查了下资料 有__cdecl 这样的一个默认的调用约定(对c和c++来说都是)  表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些参数由调用者清除,称为手动清栈。

    我用的ide是vs2019

    调试时会发现 先进入 fun2() 再进入fun1()  

     函数读参数也是如同进栈一般  

    先进入fun2() 返回值为1 把1进栈

    再进入fun1() 返回值为2 把2进栈  

    那么出栈后是2 1

  • 相关阅读:
    MySQL设计之三范式的理解
    git基本操作命令和安装
    MySQL 中<=>用法(长知识)
    举个栗子看如何做MySQL 内核深度优化
    我们来说一说TCP神奇的40ms
    一览js模块化:从CommonJS到ES6
    Vtiger CRM 几处SQL注入漏洞分析,测试工程师可借鉴
    做优化的数据库工程师请参考!CynosDB的计算层设计优化揭秘
    1个开发如何撑起一个过亿用户的小程序
    教你一个vue小技巧,一般人我不说的
  • 原文地址:https://www.cnblogs.com/57one/p/11389859.html
Copyright © 2011-2022 走看看