배열 스택과 2 개의 기능을 생성합니다. 스택에 무언가를 추가하는 기능은 스택이 비어 있지만 작동하지 않는지 확인하는 기능은 비어 있으므로 실수는 어디에 있습니까?
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 10
struct stack
{
int items[MAX];
int top;
};
typedef struct stack st;
int isempty(st *s)
{
if (s->top==-1)
{
printf("it's empty");
}
else
{
printf("it's not empty");
}
}
void push(st *s)
{
int newitem;
printf("Enter item: ");
scanf("%d",&newitem);
s->top++;
s->items[s->top]=newitem;
}
int main()
{
st *s;
push(s);
isempty(s);
return 0;
}
함수에 isempty
무효가 아닌 반환 유형이 있습니다. 따라서 값을 반환해야합니다. 예를 들면
int isempty(st *s)
{
if (s->top==-1)
{
printf("it's empty");
}
else
{
printf("it's not empty");
}
return s->top==-1;
}
두 번째 문제는 구조의 객체에 대한 포인터를 초기화하지 않았다는 것입니다.
st *s;
포인터를 선언 할 필요가 없습니다. 당신은 쓸 수 있습니다
st s = { { 0 }, -1 };
다음과 같은 함수를 호출합니다.
push( &s );
isempty( &s );
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다