69,368
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#define maxsize 100
struct SNode
{
int ptr;
int *data;
int Maxsize;
};
typedef struct SNode *Stack;
void initStack(Stack s)
{
s->ptr=-1;
s->Maxsize=maxsize;
s->data=(int *)malloc (maxsize *sizeof(int));
printf("%d\n",s->ptr);
}
void Push(Stack s,int x)
{
if(s->ptr==maxsize-1)
{
printf("栈满");
}
else
{
s->ptr++;
s->data[s->ptr]=x;
printf("%d\n",s->data[s->ptr]);
}
}
void pop(Stack s,int x)
{
if(s->ptr==-1)
{
printf("栈空");
}
else
{
x=s->data[s->ptr];
s->ptr--;
}
}
void main()
{
struct SNode stack;
Stack s=&stack;
int x;
scanf("%d",&x);
initStack(s);
printf("%d\n",s->ptr);
Push(s,x);
printf("%d\t%d",s->data[s->ptr],s->ptr);
}
这样就可以啦!同意的话赏点分啊!void initStack(Stack s)
{
s =(Stack ) malloc (sizeof (struct SNode)) ; //去掉这一行 要不然主函数struct SNode stack;Stack s=&stack;传进来的值就没用了。
s->ptr=-1;
printf("%d\n",s->ptr);
}
printf("%d\t%d",s->data,s->ptr); //改为:printf("%d\t%d",s->data[s->ptr],s->ptr);
#include<stdio.h>
#include<stdlib.h>
#define maxsize 100
struct SNode
{
int ptr;
int *data;
int Maxsize;
};
typedef struct SNode *Stack;
void initStack(Stack s)
{
s->ptr=-1;
s->Maxsize=maxsize;
s->data=(int *)malloc (maxsize *sizeof(int));
printf("%d\n",s->ptr);
}
void Push(Stack s,int x)
{
if(s->ptr==maxsize-1)
{
printf("栈满");
}
else
{
s->ptr++;
s->data[s->ptr]=x;
printf("%d\n",s->data);
}
}
void pop(Stack s,int x)
{
if(s->ptr==-1)
{
printf("栈空");
}
else
{
x=s->data[s->ptr];
s->ptr--;
}
}
void main()
{
struct SNode stack;
Stack s=&stack;
int x;
scanf("%d",&x);
initStack(s);
printf("%d\n",s->ptr);
Push(s,x);
printf("%d\t%d",s->data,s->ptr);
}
我已经改成这样了,s->ptr是没问题了,但是s->data一直出错void initStack(Stack *s)
{
*s =(Stack ) malloc (sizeof (struct SNode)) ;
(*s)->ptr=-1;
printf("%d\n",(*s)->ptr);
}
initStack(&s);