구조체에 저장된 요소를 역순으로 출력하려면 어떻게 해야하나요?
조히
질문 제목 : 구조체에 저장된 요소를 역순으로 출력하려면 어떻게 해야하나요? 질문 내용 :
렉스랑 심볼테이블 만들고 있는데요
심볼테이블에 저장이 된 원소를 저장된 순서대로 출력을 하고싶은데 오히려 역순으로 출력이 되요
예를 들면
저장이 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;
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2699024 | C언어 공부하려는데 도와주세요!!! (2) | 달님 | 2025-06-22 |
2698994 | 날짜 계산하는 C 코드 짜고 있는데 꽉 막혀서 질문드립니다.. (6) | 별 | 2025-06-22 |
2698967 | 파일삭제 윈도우 폴더까지 접근하게하는 함수가 뭔가요 (2) | 샤인 | 2025-06-21 |
2698938 | c언어 메모리질문 (3) | 나래 | 2025-06-21 |
2698909 | 서비스 요청 고객 관리 프로그램 짜는것좀 도와주세요ㅜㅜ (4) | 궁수자리 | 2025-06-21 |
2698882 | 프로그래밍좀 짜주세요 (3) | 황예 | 2025-06-21 |
2698855 | 카프-라빈 알고리즘 코딩 분석좀 도와주세요.. | 꽃봄 | 2025-06-20 |
2698829 | 학점계산기 (7) | MyWay | 2025-06-20 |
2698782 | 기초적인 함수 질문이요ㅠㅠㅠㅠ | 내담 | 2025-06-20 |
2698749 | 프로그램 짜던 도중 패닉입니다...ㅜ | 파랑 | 2025-06-19 |
2698719 | 조건부컴파일 질문입니다.~ (2) | 큐트 | 2025-06-19 |
2698693 | 재귀 함수 에러 | 바닐라 | 2025-06-19 |
2698673 | 고민이있는데 들어좀주세요!! (1) | 초코맛캔디 | 2025-06-19 |
2698644 | 1부터 n까지의 합을 구하는데 엄청긴숫자의 합을 구할때는 어떻게 해야하나요? (4) | 슬우 | 2025-06-18 |
2698616 | 다른 함수로 안넘어갑니다..;;; | 도1도캣 | 2025-06-18 |
2698587 | 배열하다 막혀서... (3) | WhiteCat | 2025-06-18 |
2698559 | 문자열을 비우는방법 (2) | 하늘 | 2025-06-18 |
2698528 | 착하고 친절한 선생씌구해염~ㅋㅋ (4) | 옆집언니야 | 2025-06-17 |
2698502 | 자료구조 큐 | 캔서 | 2025-06-17 |
2698477 | 실행화면 배경문의요 | 선아 | 2025-06-17 |