data.txt에서 번호와 이름을 읽어와서 이름 알파벳 순으로 연결리스트를 작성(함수로 넘겨서) 하고, 연결리스트를 순서대로 출력하고, 이름을 입력받아 해당 번호와 이름을 출력(이것도 함수로 넘겨서)하려고 하는데
다음 코드를 빌드하면 "이름을 입력하세요: "밖에 안뜸 ㅠㅠ
구조체랑 포인터 나름 잘 이해했다고 생각했는데 ㅠㅠ
뭐가 잘못된거지좀 알ㄹ려져 ㅠㅠㅠ
+그리고 번호는 다르고 동명이인일 경우 번호 순서대로 연결리스트를 연결하려고 하는데 그부분은 아직 코드를 못짰는데 힌트좀줘
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> #include <stdlib.h> #include <string.h> struct info { int no; char name[20]; struct info* link; }; void insert(struct info** q, struct info* p); void search(struct info** q, char findname[20]); int main() { struct info* p; struct info* root = NULL; struct info* find = NULL; char findname[20]; FILE* fp; fp = fopen("data.txt", "r"); if (fopen == NULL) { fprintf(stderr, "file open error\n"); exit(1); } while (!feof(fp)) { p = (struct info*)malloc(sizeof(struct info)); fscanf(fp, "%d %s\n", &(p->no), p->name); p->link = NULL; insert(&root, p); } find = root; while (find != NULL) { printf("%d %s\n", find->no, find->name); find = find->link; } printf("\n이름을 입력하세요 : "); scanf("%s", findname); search(&root, findname); system("pause"); return 0; } void insert(struct info** q, struct info* p) { struct info* t = NULL; struct info* pre = NULL; if (*q == NULL) *q = p; //맨 처음 데이터 else if (strcmp(p->name, (*q)->name) == -1) { p->link = *q; *q = p; }// 첫 알파벳의 데이터 else { t = *q; while (t->link != NULL && strcmp(p->name, t->name) == 1) { pre = t; t = t->link; } if (t->link == NULL && strcmp(p->name, t->name) == 1) t->link = p; //맨 뒤 알파벳의 데이터 else if (t->link == NULL && strcmp(p->name, t->name) == -1) { pre->link = p; p->link = t; }//뒤에서 두번째 알파벳의 데이터 else { pre->link = p; p->link = t; }//중간에 삽입 } } void search(struct info** q, char findname[20]) { struct info* find; int cnt = 0; find = *q; while (find != NULL) { if (strcmp(findname, find->name) == 0) { printf("%d %s\n", find->no, find->name); cnt++; } find = find->link; } if (cnt == 0) printf("없는 이름입니다\n"); }
댓글 0