简体   繁体   English

通过使用指针来实现堆栈

[英]Implement a stack by using a pointer to pointer

I am implementing a small program to exercise with double pointers This is the main program: 我正在实现一个使用双指针进行练习的小程序,这是主程序:

#include <stdio.h>
#include "serial.h"
#include "stack.h"

int main(void) {
    serial_init();  

    /* Array to hold stack entries */
    int stack[10]={0};
    /* Stack pointer */
    int *stack_p = stack;

    /* Code to call the functions */

    push(&stack_p, 1); 
    push(&stack_p, 2); 
    push(&stack_p, 3); 
    push(&stack_p, 4); 

    printf("popped value: %d\r\n", pop(&stack_p));
    printf("popped value: %d\r\n", pop(&stack_p));
    printf("popped value: %d\r\n", pop(&stack_p));
    printf("popped value: %d\r\n", pop(&stack_p));
}        

void push(int **sp, int value) {
    /*  Implement it */ 
}

int pop(int **sp) {
    /*  Implement it */ 
}

I have implemented the push function it seems ok. 我已经实现了推功能,看来还可以。 However, the pop return back just the last element and then 10 但是,弹出窗口仅返回最后一个元素,然后返回10

void push(int **sp, int value) {
/* implemented it*/

 int *pt; 
 pt=&value; // Store the value to the pointer 

 printf("Push value is is %d\r\n", *pt);
 sp = &pt; // associate the pointer to the pointer of pointer
 ++(*pt);   
}

int pop(int **sp) {
 /* implemented it */
 int value;
 int *pt;
 pt=&value;

 sp = &pt;
 *pt--;

 return value;
}   

Your push and pop functions are overly complicated and totally wrong: 您的push和pop函数过于复杂且完全错误:

You want this: 你要这个:

void push(int **sp, int value) {
  **sp = value;   // put value onto top of the stack
  (*sp)++;        // increment stack pointer
}

int pop(int **sp) {
  (*sp)--;        // decrement stack pointer
  return **sp;    // return value which is on nthe op of the stack
}

Your wrong code for push with explanations in comments: 您输入的错误代码带有注释说明:

void push(int **sp, int value) {
  int *pt; 
  pt=&value; // here you put the pointer to the local variable value
             // into pt, but local variables disappear as soon
             // as the function has finished

  //  the printf is the only thing one more or less correct
  //  but you could just print directly 'value' like this:
  //    printf("Pushed value is %d\r\n", value);
  //
  printf("Push value is is %d\r\n", *pt);

  sp = &pt;  // this only assigns the pointer to the local variable pt to 
             // the local variable sp

  ++(*pt);   // here you increment actually the local variable
             // value which is pointless 
}

By the way: the initialisation to zero of the whole stack is not necessary, although it might help during the debugging process. 顺便说一句:尽管在调试过程中可能会有所帮助,但不必将整个堆栈初始化为零。 So you can write the declaration of the stack like this: 因此,您可以像这样编写堆栈的声明:

int stack[10];  // no initialisation necessary

Exercise for you: 为您练习:

Explain the exact reason why it is not necessary to initialize all elements of the stack to zero. 说明不需要将堆栈的所有元素初始化为零的确切原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM