2014-01-17 3 views
0

bubble sort 함수를 사용하여 링크 된 목록을 정렬했지만 인쇄 할 때 실제로 정렬 목록을 인쇄하지 않습니다.링크 목록으로 텍스트 파일 읽기 (인쇄 순서 오류)

전체 실행 프로그램 : 당신은 모든 분야에서 같은 일을 복사 한

#include <stdlib.h> 
#include <string.h> 
#include <time.h> 
#include <stdio.h> 

struct employee 
{ 
char name[30]; 
char surname[30]; 
char address[30]; 
char cell[30]; 
struct emp *next; 
}*start=NULL; 

struct employee *new_node; 
struct employee *current; 
FILE *fp; 

int main(char *argv[],int argc) 
{ 
file_open(); 
printf("Before bubble sort:\n"); 
print_list(start); 
printf("After bubble sort:\n"); 
bubble_sort(); 
print_list(start); 
return 0; 
} 

file_open() 
{ 
char fname[20]; 
char line[128]; 
printf("ENTER THE FILE NAME:"); 
scanf("%s",fname); 
fp=fopen(fname,"r"); 
if(fp != NULL) 
{ 
    while(fgets(line,sizeof(line),fp) != NULL) 
    { 
     //printf("FILE OPEN SUCCESSFULL"); 
     splitline(line); 
     //printf("%s",line); 
    } 
    fclose (fp); 
} 
else 
{ 
    printf("ERROR OPEN FILE\n"); 
    return (1); 
} 
return 0; 

} 

int splitline(char str[]) 
{ 
new_node=(struct employee*)malloc(sizeof(struct employee)); 
char *store; 
store=strtok(str,", "); 
strcpy(new_node->name,store); 
store=strtok(NULL,", "); 
strcpy(new_node->surname,store); 
store=strtok(NULL,", "); 
strcpy(new_node->address,store); 
store=strtok(NULL,", "); 
strcpy(new_node->cell,store); 
new_node->next=NULL; 

if(start == 0) 
{ 
start=new_node; 
current=new_node; 
} 
else 
{ 
current->next=new_node; 
current=new_node; 
} 
//print_list(current); 
return 0; 
} 

void print_list(struct employee *start) 
{ 
struct employee *ptr; 
ptr=start; 
while(ptr!=NULL) 
{ 
printf("%s\n%s\n%s\n%s\n",ptr->name,ptr->surname,ptr->address,ptr->cell); 
ptr=ptr->next; 
} 
printf("\n"); 

} 

void bubble_sort(struct employee *start) 
{ 
struct employee *a = NULL; 
struct employee *b = NULL; 
struct employee *c = NULL; 
struct employee *e = NULL; 
struct employee *tmp = NULL; 


while(e != start->next) { 
c = a = start; 
b = a->next; 
while(a != e) { 
if(a->name > b->name) { 
if(a == start) { 
tmp = b -> next; 
b->next = a; 
a->next = tmp; 
start = b; 
c = b; 
} else { 
tmp = b->next; 
b->next = a; 
a->next = tmp; 
c->next = b; 
c = b; 
} 
} else { 
c = a; 
a = a->next; 
} 
b = a->next; 
if(b == e) 
e = a; 
} 
} 

}

+0

거품 형 정렬은 아무 것도 정렬하지 않습니다. 여기서 이름을 비교합니까? 그리고 이렇게 질문을 바꾸면 안됩니다. 버블 정렬 방식으로 또 다른 질문을 올리세요. 질문을 완전히 변경하면 이전 검색어를 처리 한 답변은 어떻게됩니까? 버블 정렬을 사용하여 새로운 질문을 게시하고 문제를 해결 한 대답을 수락하십시오. 감사하지 않으면 몸이 당신에게 대답 할 것입니다. 동의 방법을 모르는 경우 FAQ 페이지를 읽으십시오. – Dipto

+0

죄송합니다. 실제로 다른 섹션에 게시하려고했지만 제대로 작동하지 못했습니다. 그러나 다음에 나는 그 지시를 따르려고 노력할 것입니다. 제안 해 주셔서 감사합니다. –

답변

3

을 나는 더 이상 갈 수 있도록 당신은 친절하게 나에게 오류를 표시 할 수 있습니다.

strcpy(new_node->name,store); 
strcpy(new_node->surname,store); 
strcpy(new_node->address,store); 
strcpy(new_node->cell,store); 

당신은 마지해야 당신의 splitlinecreate_node.

int AddNodeFromLine(char str[]) 
{ 
new_node=(struct employee*)malloc(sizeof(struct employee)); 
new_node->next=NULL; 
char *store; 
store=strtok(str,", "); 
strcpy(new_node->name,store); 
store=strtok(NULL,", "); 
strcpy(new_node->surname,store); 
store=strtok(NULL,", "); 
strcpy(new_node->address,store); 
store=strtok(NULL,", "); 
strcpy(new_node->cell,store); 
new_node->next=NULL; 

if(start == 0) 
{ 
    start=new_node; 
    current=new_node; 
} 
else 
{ 
    current->next=new_node; 
    current=new_node; 
} 
print_list(current); 
return 0; 
} 
+0

헤이 디토, 당신의 대답은 정말로 나를 안심 시켰고 이해했습니다. 이 프로그램은 해결하는데 오랜 시간이 걸렸습니다. 나는 물러 섰다. 그러나 이제 나는 나아갈 수 있습니다. 그리고 한 가지 더 물어보고 싶은 것은 print_list (현재)에서 현재를 보았지만 main에서는 print_list (new_node)에 new_node를 보냅니다. 그것은 충돌하지 않습니다! –

+0

필자는 main에서 print_list (start)를 호출하여 전체 목록을 인쇄해야한다고 생각한다. 'print_list (current)'와'print_list (new_node)'모두 마지막 노드를 출력 할 것이다. 확실히 이것을 시도하십시오. – Dipto

+0

안녕하세요 딥토, 다시 한번 당신의 도움이 필요합니다. 사실 이제 링크 된 목록을 알파벳순으로 정렬하고 두 번째로 번호 오름차순에 따라 정렬하려고합니다. 저를 이끌 수 있습니까? 나는 정말로 당신의 도움에 감사 할 것입니다. –