C语言中栈的最小值

huangapple go评论95阅读模式
英文:

Minium value of a stack in C

问题

我正在尝试实现一个用数组在C中实现的栈的最小值计算函数,只能使用push()和pop()函数。以下是我的代码(findMin()是最后一个函数)。

这个函数不起作用,如果我将5 3 5 5 5放入栈中,它会打印"最小值1"。我认为这是因为我找不到正确的基本条件。

  1. void init(int S[]) {
  2. S[0] = 0;
  3. }
  4. int push(int S[], int x) {
  5. if (!fullStack(S)) {
  6. S[0] = S[0] + 1;
  7. S[S[0]] = x;
  8. } else {
  9. printf("栈已满\n");
  10. }
  11. }
  12. int pop(int S[]) {
  13. if (!emptyStack(S)) {
  14. int x = S[S[0]];
  15. S[0]--;
  16. return x;
  17. } else {
  18. printf("栈为空\n");
  19. return -1;
  20. }
  21. }
  22. int emptyStack(int S[]) {
  23. return S[0] == 0;
  24. }
  25. int fullStack(int S[]) {
  26. return S[0] == SIZE;
  27. }
  28. int findMin(int S) {
  29. if (emptyStack(S)) return -1;
  30. else {
  31. int x = pop(S);
  32. int min = findMin(S);
  33. if (min == -1 || x < min) {
  34. push(S, x);
  35. return x;
  36. } else {
  37. push(S, x);
  38. return min;
  39. }
  40. }
  41. }

这是修复后的代码,修复了查找最小值的问题。在递归中,我们需要保留栈的状态,因此在递归之前出栈,然后在递归之后入栈。此外,如果栈为空,我们应该返回一个特定的值(例如-1)来表示没有找到最小值。

英文:

i'm trying to implement a funcion who calculate the minium value of a stack in C implemented with an array, using only push() and pop() function.
here is my code: (findMin() is the last function).

The function doesn't work, if i put 5 3 5 5 5 in the stack, it prints "minium value 1". I think it's because i can't find a correct base condition

  1. void init(int S[]){
  2. S[0] = 0;
  3. }
  4. int push(int S[], int x) {
  5. if(!fullStack(S)){
  6. S[0] = S[0] + 1;
  7. S[S[0]] = x;
  8. }
  9. else printf(&quot;stack full\n&quot;);
  10. }
  11. int pop(int S[]) {
  12. if(!emptyStack(S)){
  13. int x = S[S[0]];
  14. S[0]--;
  15. return x;
  16. }
  17. else {
  18. printf(&quot;stack empty\n&quot;);
  19. return -1;
  20. }
  21. }
  22. int emptyStack(int S[]) {
  23. return S[0] == 0;
  24. }
  25. int fullStack(int S[]) {
  26. return S[0] == SIZE;
  27. }
  28. int findMin(int S[]){
  29. if(emptyStack(S)) return ;
  30. else{
  31. int x = pop(S);
  32. if(x &lt; findMin(S)) return x;
  33. else return findMin(S);
  34. push(S, x);
  35. }
  36. }

答案1

得分: 1

有趣的逻辑...

根据我理解的,以下是你需要做的事情:

  1. #define LARGE_NUMBER 1000; // 你可以查看 INT_MAX
  2. int findMin(int S[]){
  3. if(emptyStack(S)) return LARGE_NUMBER;
  4. else{
  5. int x = pop(S);
  6. int y = findMin(S); // 将结果存储在单独的变量中以返回
  7. if(x < y) return x;
  8. else return y;
  9. push(S, x); // 这是无法到达的代码,因此堆栈变为空
  10. }
  11. }

经过这些更改,当输入为 "5 3 5 5 5" 时,findMin() 返回 "3"。

英文:

Interesting logic...

By what i could understand, here is what you need to do,

  1. #define LARGE_NUMBER 1000; //You could look into INT_MAX
  2. int findMin(int S[]){
  3. if(emptyStack(S)) return LARGE_NUMBER;
  4. else{
  5. int x = pop(S);
  6. int y = findMin(S); // Store result in separate variable to return
  7. if(x &lt; y) return x;
  8. else return y;
  9. push(S, x); // This is unreachable code, so stack becomes empty
  10. }
  11. }

After these changes, findMin() returns "3" in your case when input is "5 3 5 5 5".

huangapple
  • 本文由 发表于 2023年2月23日 22:58:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定