c언어 stack관련하여 질문드립니다.

oeo0750의 이미지

c언어에서 stack을 이용하여 괄호의 쌍이 맞는지 검사하는 함수를 구성해 보았습니다.
현재

main.c: In function ‘parser’:
main.c:21:4: error: unknown type name ‘stack_ptr’
stack_ptr top = NULL;
^
main.c:21:20: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
stack_ptr top = NULL;
^
main.c:28:5: warning: implicit declaration of function ‘push’ [-Wimplicit-function-declaration]
push(&top, expr[i]);
^
main.c:31:19: warning: implicit declaration of function ‘IS_EMPTY’ [-Wimplicit-function-declaration]
case ']': if (IS_EMPTY(top))
^
main.c:33:21: warning: implicit declaration of function ‘pop’ [-Wimplicit-function-declaration]
ch = pop(&top);
^

이런 오류들이 발생하고 있는데, 스택 개념에 대한 이해가 부족한 건지
error: unknown type name ‘stack_ptr’여기서 선언을 어떻게 해야할지 잘 모르겠습니다.
스택을 사용하려면 괄호를 배열로 받으면 안되는 건가요?

도움 주시면 보면서 공부하고 싶습니다. 감사합니다.

 
 
#include <stdio.h>
#include <stdbool.h>
#include <string.h> 
 
int main(){   
   int parser();
   char arr1[20] = "()()(())" ; 
   bool result = parser(arr1);
   if(result==true) {
   printf("괄호의 쌍이 맞습니다."); 
   }
   else {
   printf("괄호의 쌍이 맞지 않습니다.");
   }
 }
 
int parser(char expr[]){
   int i;
   char ch;
   stack_ptr top = NULL;
 
   for (i = 0; i < strlen(expr); i++)
       switch (expr[i]) {
	   case '[': 
	   case  '{': 
	   case '(': 
	   push(&top, expr[i]);
	           break;
 
	   case ']': if (IS_EMPTY(top))
	               return false;
	              ch = pop(&top);
				  if (ch != '[') return false;
				  break;
		case '}': if (IS_EMPTY(top))
	               return false;
	              ch = pop(&top);
				  if (ch != '{') return false;
				  break;	
		case ')': if (IS_EMPTY(top))
	               return false;
	              ch = pop(&top);
				  if (ch != '(') return false;
	   }
    if (IS_EMPTY(top))
        return true;
    return false;
}
세벌의 이미지

stack_ptr 구조체에 무엇을 넣을 건가 생각해 보셔요.

스택개념 뿐 아니라 C 언어도 공부하셔야 될 거 같아요.

typedef struct {
...
...
...
} stack_ptr;

{ 와 } 사이에 들어갈 부분은 님께서 뭘 하고자 하느냐에 따라 달라지겠죠.

hui1601의 이미지

소스코드를 고치셔도 "())("입력하면 "괄호의 쌍이 맞습니다."나오실 겁니다.아마도...

2019년 기준 중2

bushi의 이미지

빈 곳 메워오는, 흔한 학기말 숙제일 뿐인 것 같구요.
제대로 메우기만 했다면 그런 식으로는 동작할 리가 없는 코드로 보입니다.