能通过编译,但却不能成功运行
来源:百度知道 编辑:UC知道 时间:2024/05/16 13:18:24
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node* next;
}stacknode;
typedef struct stack{
stacknode *top;
int length;
}linkstack;
void InitStack(linkstack *s)
{
s->top=NULL;
s->length=0;
}//初始化堆栈
void push(linkstack *S,int value)
{
stacknode *newptr=(struct stacknode *)malloc(sizeof(stacknode));
if (newptr!=NULL) /*如果有可用的内存*/
{
newptr->data=value;
newptr->next=S->top;
S->top=newptr;
}
else printf("%d not inserted. No memory available.\n",value);
}
char pop(linkstack *S)
{
stacknode *temp;
temp=S->top;
char elem=temp->data;
S->top=temp->next;
通不过MS VC++ 编译器。
(1)stacknode *newptr=(struct stacknode *)malloc(sizeof(stacknode));
应当是:
stacknode *newptr=(stacknode *)malloc(sizeof(stacknode));
(2)linkstack *s; s->top=NULL;
s没有初始化,就使用了。
(3)char elem;
scanf("%d",&elem); char 型不能用%d 格式
要么改成 unsigned char elem
要么改成 %s
我用DEV C++跟VC++ 6.0测试,也不能通过编译
Compiling...
sdfsadf.cpp
D:\程序\ascd\sdfsadf.cpp(260) : error C2027: use of undefined type 'stacknode'
D:\程序\ascd\sdfsadf.cpp(260) : see declaration of 'stacknode'
D:\程序\ascd\sdfsadf.cpp(260) : error C2440: 'initializing' : cannot convert from 'struct push::stacknode *' to 'struct node *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
D:\程序\ascd\sdfsadf.cpp(308) : warning C4508: 'main' : function should return a value; &