#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct stackNode
{
int data;
struct stackNode *nextPtr;
};
void instructions()
{
printf("[1]Push a value on the stack\n");
printf("[2]Pop a value off the stack\n");
printf("[3]Display the whole stack\n");
printf("[4]Exit");
}
void push(struct stackPtr *topPtr, int info)
{
struct stackPtr *newPtr;
newPtr= malloc(sizeof(struct stackNode));
if(newPtr !=NULL)
{
newPtr->data = info;
newPtr->nextPtr=*topPtr;
*topPtr=newPtr;
}
else
{
printf("%d not inserted no memory available");
}
int main()
{
struct StackNodePtr *stackPtr;
stackPtr = NULL;
int choice, value;
do
{
instructions();
printf("\nEnter Your Choice: ");
scanf("%d",&choice);
if(choice == 1)
{
printf("Enter a value for the stack");
}
if(choice == 2)
{
printf(" ");
}
if(choice == 3)
{
printf(" ");
}
if(choice == 4 )
{
printf("bye!");
return 0;
}
} while(choice !=4);
system("pause");
}
我为我的链表和堆栈代码做了一个函数推送,但事情是它没有工作在函数中有很大的错误推它有什么问题吗?它不允许使用malloc为什么会这样?
答案 0 :(得分:2)
这对你有用吗?
struct stackNode { int data; struct stackNode *nextPtr; };
int main() { struct stackNode * stackPtr = NULL; }
答案 1 :(得分:2)
struct stackNode
{
int data;
struct stackNode *nextPtr;
};
int main()
{
struct stackNode *stackPtr = NULL;
}
答案 2 :(得分:1)
// just precede the struct type with ... struct
struct stackNode* stackPtr = NULL;
快乐的编码。
答案 3 :(得分:1)
这一行:
typedef struct StackNode *StackNodePtr;
顺便说错了。你的typedef应该是:
typedef struct stackNode StackNode;
typedef StackNode* StackNodePtr;
不知道为什么你反对使用typedef
- 它往往会使代码更具可读性。