栈,递归
- 棧的基本操作
注意:是從后往前連接的
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\n",pop(&pStack)); 19 printf("%d\n",pop(&pStack)); 20 printf("%d\n",pop(&pStack)); 21 printf("%d\n",pop(&pStack)); 22 if( isEmpty(pStack)) 23 printf("棧為空\n"); 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 }基本操作- ?遞歸
?
轉載于:https://www.cnblogs.com/Lune-Qiu/p/7879118.html
總結
- 上一篇: 梦到鱼掉鳞是什么意思
- 下一篇: 乱花渐欲迷人眼-杜绝设计的视噪