需要帮助:堆栈ADT链表与VOID STAR实现(C编程)

发布于 2024-10-03 20:23:48 字数 2712 浏览 6 评论 0原文

首先,我无法使用“代码示例”,因此我添加代码如下:

Header.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 

Header.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;
    }

Project.cpp< /strong>

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 中的值(5)并将其打印为 Z,它是一个整数。但我看到像 4247612 这样的数字,只要编译器窗口没有关闭,它就不会改变。

First of all, i couldn't use "code sample" therefore i am adding my codes as stated below:

Header.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 

Header.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;
    }

Project.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;
}

EDIT: I want to put X, into S(tack) by using PushStack function. And then i want to take the value(5) stored in S and print it as Z which is an integer. But i see a numbers like 4247612, which does not change as long as the compiler window is not closed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下壹個目標 2024-10-10 20:23:48

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

Note that PushStack((void *)x, S); int z = (int)PopStack(S); can incur truncation, and so, puncasting types like this is not guaranteed to work in all cases: void * is not a magic type that can hold each and every possible value in the universe. (It would especially go up in flames if you started to use doubles instead of ints on contemporary implementations.) It may however point to one.

别低头,皇冠会掉 2024-10-10 20:23:48

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

The problem is that you've defined Stack as a struct Node * and are trying to update its value inside the PushStack() and PopStack() functions. When you assign a value to s inside either function, it only updates the local variable, and not S in your main function. Instead, PushStack() and PopStack() may need to take a pointer to a stack object (Stack *) so that you can update the caller's value too.

沧桑㈠ 2024-10-10 20:23:48

您的 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; 
}

因为 S > main 中的内容从未更新,它仍然为 NULL。

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

Your PushStack and PopStack routines is not doing what you want

To exhibit it explicitly in the PopStack function:

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; 
}

Because the S in main is never updated it continues to be NULL.

You would fix this by passing a Stack *s, and accessing it using *s in both function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文