연결 스택 프로그램 (LinkedStack)
하예라
질문 제목 :
연결 스택 프로그램 구현하기
질문 내용 :
#include stdio.h
#include stdlib.h
#include malloc.h
typedef int element;
typedef struct stacknode {
element item;
struct stacknode *link;
} stacknode;
typedef struct {
stacknode *top;
} linkedstacktype;
void init(linkedstacktype *s)
{
s-top = null;
}
int is_empty(linkedstacktype *s)
{
return (s-top == null);
}
int is_full(linkedstacktype *s)
{
return 0;
}
void push(linkedstacktype *s, element item)
{
stacknode *temp=(stacknode *)malloc(sizeof(stacknode));
if(temp == null) {
fprintf(stderr, 메모리 할당에러입니다 \n);
return;
}
else {
temp-item = item;
temp-link = s-top;
s-top = temp;
}
}
element pop(linkedstacktype *s)
{
if(is_empty(s)) {
fprintf(stderr, 스택이 비어있습니다.\n);
exit(1);
}
else
{
stacknode *temp = s-top;
element item = temp-item;
s-top = s-top-link;
free(temp);
return item;
}
}
element peek(linkedstacktype *s)
{
if(is_empty(s)) {
fprintf(stderr, 스택이 비어있습니다.\n);
exit(1);
}
else
{
return s-top-item;
}
}
void main()
{
linkedstacktype s;
init(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
printf(%d \n, pop(&s));
printf(%d \n, pop(&s));
printf(%d \n, pop(&s));
printf(%d \n, is_empty(&s));
}
이런식으로는 했습니다만 scanf로 값을 3개정도를 입력받아서 출력하는 프로그램을 짜려고하는데
생각처럼 잘 되지않네요. 도와주세요~ ㅜㅜ
-
벚꽃
이렇게 바꿔도 입력이 안되길래 struct 부분을 수정하니까 더 엉망이 된거같아요 ㅎ....
-
알
아뇨. 문자열을 받으려면 char형 배열을 사용해야 합니다.
char str[100];
scanf(\%s\ -
은별
값을 정수말고 문자열로 받으려면
char v;
scanf(\%s\ -
방방
int v;
scanf(\%d\