구조체에 저장된 요소를 역순으로 출력하려면 어떻게 해야하나요?
조히
질문 제목 : 구조체에 저장된 요소를 역순으로 출력하려면 어떻게 해야하나요? 질문 내용 :
렉스랑 심볼테이블 만들고 있는데요
심볼테이블에 저장이 된 원소를 저장된 순서대로 출력을 하고싶은데 오히려 역순으로 출력이 되요
예를 들면
저장이 1, 2, 3, 4, 5 순으로 되었다면
출력은 5, 4, 3, 2, 1 이렇게 역으로 출력이 되네요..//symbol table
struct symtab
{
char *id; // id
int lines; // where it is used
struct symtab *next; // pointer to symbol
};
struct symtab *first; // first element, first declared symbols
struct symtab *sym_list; //first element, declared symbols
struct symtab *unde; // first element, undeclared symbols
extern void *malloc();
// store the first declared symbol
addfirst(char *word, int where)
{
struct symtab *sym;
sym =(struct symtab*) malloc(sizeof(struct symtab));
sym-next = first;
sym-id=(char*) malloc(strlen(word)+1);
strcpy(sym-id, word);
sym-lines=where;
first=sym;
}
// store declared symbol if it is used
add(char *word, int where)
{
struct symtab *sym;
sym =(struct symtab*) malloc(sizeof(struct symtab));
sym-next = sym_list;
sym-id=(char*) malloc(strlen(word)+1);
strcpy(sym-id, word);
sym-lines=where;
sym_list=sym;
}
// store undefined symbol
addundec(char *word, int where)
{
struct symtab * unsym;
unsym =(struct symtab*) malloc(sizeof(struct symtab));
unsym-next = unde;
unsym-id=(char*) malloc(strlen(word)+1);
strcpy(unsym-id, word);
unsym-lines=where;
unde=unsym;
}
//look for declared symbol
int lookup2(char * word)
{
struct symtab *fp = first;
for( ; fp;fp=fp-next){
if(strcmp(fp-id, word)==0)
{
return fp-lines; //declared, return the declared line
}
}
return undeclared; //not found;
}
// look for lines where declared symbol is used
int lookup(char *word)
{
struct symtab *wp=sym_list;
for( ; wp;wp=wp-next){
if(strcmp(wp-id, word)==0)
{
return wp-lines; //declared, return the declared line
}
}
return undeclared; //not found;
}
//여기부터 출력이 문제..
// print out declared symbols
printdeclared()
{
printf(symbols:\n);
char *tok;
struct symtab *fp = first;
struct symtab *ptr = sym_list;
while(fp!=null)
{
tok = fp-id;
printf(%s - declared on %d, used on , tok, lookup2(tok));
for( ; ptr!=null ; ptr = ptr-next)
{
if(strcmp(ptr-id, tok)==0)
{
printf(%d, , ptr-lines); //저장된 순서의 역순으로 출력이 되요 여기가;
}
}
ptr=sym_list;
fp=fp-next;
printf(\n);
}
// next, printout undeout undeclared symbols
printundeclared();
}제가 원하는 출력하려면 어떻게 고쳐야하나요?
-
꺆잉
사사삭 봤는데
printdeclared() -이함수에서
{
printf(\Symbols:\\n\); --------- 선언보다 먼저 왔네요
char *tok;
struct symtab *fp = first;
struct symtab *ptr = sym_list;