zoukankan      html  css  js  c++  java
  • 栈,递归

    • 栈的基本操作

    注意:是从后往前连接的

     1 #include <stdio.h>
     2 #include <Windows.h>
     3 typedef struct sStack
     4 {
     5     int num;
     6     struct sStack* pnext;
     7 }Stack;
     8 void push(Stack **pStack,int num);
     9 int pop(Stack **pStack);
    10 BOOL isEmpty(Stack *pStack);
    11 
    12 int main()
    13 {
    14     Stack* pStack = NULL;
    15      push(&pStack,1);
    16      push(&pStack,2);
    17      push(&pStack,3);
    18      printf("%d
    ",pop(&pStack));
    19      printf("%d
    ",pop(&pStack));
    20      printf("%d
    ",pop(&pStack));
    21      printf("%d
    ",pop(&pStack));
    22     if( isEmpty(pStack))
    23         printf("栈为空
    ");
    24      return 0;
    25 }
    26 void push(Stack **pStack,int num)
    27 {
    28     Stack* stack = (Stack*)malloc(sizeof(Stack));
    29     stack->num = num;
    30     stack->pnext = NULL;
    31 
    32     stack->pnext = *pStack;
    33     *pStack = stack;
    34     //不需要判断栈是否为空,直接push就好
    35 }
    36 int pop(Stack **pStack)
    37 {
    38     int a = 0;
    39     Stack* temp;
    40     if(isEmpty( *pStack))
    41         return -1;
    42     a = (*pStack)->num;
    43     temp = *pStack;
    44     (*pStack) = (*pStack)->pnext;
    45     free(temp);//弹出之后要删除
    46     return a;
    47 }
    48 BOOL isEmpty(Stack *pStack)
    49 {
    50     if(pStack == NULL)
    51         return TRUE;
    52     else 
    53         return FALSE;
    54 }基本操作
    •  递归
     1 #include <stdio.h>
     2 int JIECHENG(int);
     3 void Out(int,int );
     4 int main()
     5 {
     6     printf("%d
    ",JIECHENG(5));
     7     Out(1,4);
     8     return 0;
     9 }
    10 int JIECHENG(int num)
    11 {
    12     if(num == 1)
    13     {
    14         return 1;
    15     }
    16     else return num*JIECHENG(num-1);
    17 }
    18 void Out(int begin,int end)
    19 {
    20     if(begin <= end)
    21     {
    22         printf("%d
    ",begin);
    23         Out(begin+1,end);
    24         printf("%d
    ",begin);
    25     }
    26 }
  • 相关阅读:
    一个access连接的处理
    小说
    web版的outlook和project的结合,再和sns 结合,形成组织之间的一个共享信息.还有更多应用
    今天研究了一下window pe
    IMX6ULL开发板文本编辑工具
    Create class and methods in x++
    Image in AX 2009
    IP and userId dislay in AX 2009 title
    helpless....
    about Posted & Unposted of button function
  • 原文地址:https://www.cnblogs.com/Lune-Qiu/p/7879118.html
Copyright © 2011-2022 走看看