c - 需要帮助 : Stack ADT Linked list with VOID STAR implementation (C Programming)

标签 c pointers stack void

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

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

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

最佳答案

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

关于c - 需要帮助 : Stack ADT Linked list with VOID STAR implementation (C Programming),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4241828/

相关文章:

c - #define REG(x) (*((volatile unsigned int *)(x)))

pointers - 当 Vec 被 move 时,我可以(不安全地)持有一个指向 Vec 元素的指针吗?

C - 使用数组实现的堆栈中的大多数成员

java - 堆栈和哈希联合

c++ - Wii MotionPlus 支持

c++ - 标准库 stdio.h 重载函数的第二个 C 链接

c - c 中的指针在这里如何工作?

c - 用指针计算后缀表达式?

c - 地址到字符串常量

c - 在 C 中编译 parse_command() 函数时出错