1

首先,我不能使用“代码示例”,因此我要添加我的代码,如下所述:

头文件.h

    #ifndef MYHEADER_H
    #define MYHEADER_H

    #define EMPTY_TOS -1
    #define MIN_STACK_SIZE 5
    #define FALSE 0
    #define TRUE 1


    struct Node;
    typedef struct Node *Stack;


    void *PopStack(Stack);
    void *TopOfStack(Stack);
    void PushStack(void *val, Stack); 
    int IsEmptyStack(Stack);
    int IsFullStack(Stack);

    #endif 

头文件.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include "myheader.h"

    #define EMPTY_TOS -1 
    #define MIN_STACK_SIZE 5
    #define FALSE 0
    #define TRUE 1

    struct Node
    {
        void *val; 
        struct Node *next;
    };

    void PushStack(void *x, Stack s)
    {
         printf("/n a1");
        struct Node *insert;
               printf("/n a2");
        insert = (struct Node *) malloc(sizeof(struct Node));
               printf("/n a3");

        if (insert == NULL)
               printf("Out of memory space!\n");
        else
        {      printf("\n a4");
               insert->val = x;printf("\n a5");
               insert->next= s;printf("\n a6");
               s = insert;printf("\n a7");
        }
        printf("\n a8");
    }

    void *PopStack(Stack s)
    {                    printf("\n pop1");
        struct Node *remove; printf("\n pop2");
        void *val;           printf("\n pop3");

        if (IsEmptyStack(s))
        {
               printf("\nThe stack is empty!\n");
        }
        else   
        {      printf("\n pop4");
               remove = s;       printf("\n pop5");
               val = remove->val;     printf("\n pop67");
               s = s->next;      printf("\n pop7");
               free(remove);     printf("\n pop8");
        }
        return val;              printf("\n pop9");
    }

    void *TopOfStack(Stack s)
    {
        if (!IsEmptyStack(s))
               return s->next->val;
        else
        {
               printf("\nThe stack is empty\n");
               return 0;
        }

    }

    int IsEmptyStack(Stack s)
    {
        printf("empty");
        return (s == NULL);
    }


    int IsFullStack(Stack s)
    {
        return FALSE;
    }

项目.cpp

int main()
{

 Stack S = NULL;
 int x = 5;
 PushStack((void *)x, S);
 int z = (int)PopStack(S);
 printf("\n z = %d \n", z);



 system("PAUSE");
 return 0;
}

编辑:我想通过使用 PushStack 函数将 X 放入 S(tack) 中。然后我想取存储在 S 中的 value(5) 并将其打印为 Z ,它是一个整数。但是我看到一个像 4247612 这样的数字,只要编译器窗口没有关闭,它就不会改变。

4

3 回答 3

2

请注意,这PushStack((void *)x, S); int z = (int)PopStack(S);可能会导致截断,因此,不能保证像这样的 puncasting 类型在所有情况下都有效:void *它不是一种可以保存宇宙中每个可能值的魔法类型。(如果您开始在当代实现中使用doubles 而不是ints ,它尤其会火上浇油。)但是它可能指向一个。

于 2010-11-22T03:51:29.750 回答
1

问题是您已定义Stack为 astruct Node *并试图在PushStack()andPopStack()函数中更新其值。当您在任一函数内部分配一个值时s,它只会更新局部变量,而不是S在您的主函数中。相反,PushStack()并且PopStack()可能需要获取指向堆栈对象 ( Stack *) 的指针,以便您也可以更新调用者的值。

于 2010-11-22T03:53:08.787 回答
0

PushStackPopStack例行公事没有做你想做的事

PopStack函数中明确展示它:

void *PopStack(Stack s) { // <-- The s declared here is a LOCAL copy of the s your pass
  struct Node *remove;
  void *val;

  if (IsEmptyStack(s)) {
    printf("\nThe stack is empty!\n");
  } else {
    remove = s; 
    val = remove->val; 
    s = s->next;          // <-- Changes the local copy, but the original is unchanged
    free(remove);   
  }
  return val; 
}

因为Sin main 永远不会更新,所以它继续为 NULL。

您可以通过传递一个, 并在这两个函数中Stack *s使用它来解决这个问题。*s

于 2010-11-22T03:54:57.850 回答