구조체에 저장된 요소를 역순으로 출력하려면 어떻게 해야하나요?
조히
질문 제목 : 구조체에 저장된 요소를 역순으로 출력하려면 어떻게 해야하나요? 질문 내용 :
렉스랑 심볼테이블 만들고 있는데요
심볼테이블에 저장이 된 원소를 저장된 순서대로 출력을 하고싶은데 오히려 역순으로 출력이 되요
예를 들면
저장이 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;
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |