2012-10-11 4 views
0
#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#include<ctype.h> 

struct node *create_node(int); 
struct node *add_node(struct node *,int); 
void asce_order(struct node *); 
void desc_order(struct node *); 


struct node 
{ 
    int data; 
    int count; 
    struct node *next,*previous; 
}; 

struct node *create_node(int value) 
{ 
    struct node *pnode=(struct node *)malloc(sizeof(node)); 
    pnode->data=value; 
    pnode->count=1; 
    pnode->next=pnode->previous=NULL; 
return pnode; 
} 

struct node *add_node(struct node *pnode,int value) 
{ 
    if(pnode==NULL) 
    { 
     pnode=create_node(value); 
     return pnode; 
    } 

    else if(pnode->data == value) 
    { 
     (pnode->count)++; 
     return pnode; 
    } 
    else 
    { 
     if(pnode->data>value) 
     { 
      return add_node(pnode->previous,value); 
     } 
     else 
     { 
      return add_node(pnode->next,value); 
     } 
    } 


} 

void asce_order(struct node *pnode) 
{ 
    int i; 
    if(pnode->previous!=NULL) 
    asce_order(pnode->previous); 

    for(i=0;i<pnode->count;i++) 
    printf("%d\n",pnode->data); 

    if(pnode->next!=NULL) 
    asce_order(pnode->next); 
} 

void desc_order(struct node *pnode) 
{ 

    int i; 
    if(pnode->next!=NULL) 
    desc_order(pnode->next); 

    for(i=0;i<pnode->count;i++) 
    printf("%d\n",pnode->data); 

    if(pnode->previous!=NULL) 
    desc_order(pnode->previous); 
} 

void free_variables(struct node *pnode) 
{ 
    if(pnode==NULL) 
    return; 
    if(pnode->next!=NULL) 
    free_variables(pnode->next); 
    if(pnode->previous!=NULL) 
    free_variables(pnode->previous); 

    free(pnode); 
} 

int main() 
{ 
    int data; 
    struct node *head=NULL; 
    char option='y'; 
    int choice; 

    while(tolower(option) == 'y') 
    { 
     printf("enter the data:"); 
     scanf("%d",&data); 
     if(head==NULL) 
     head=create_node(data); 
     else 
     add_node(head,data); 
     fflush(stdin); 
     printf("enter the option:"); 
     scanf("%c",&option);  
    } 

    printf("enter the choice:\n1.ascending order\n2.Descending order"); 
    scanf("%d",&choice); 
    switch(choice) 
    { 
     case 1: 
     printf("the ascending order:\n"); 
     asce_order(head); 
     break; 

     case 2: 
     printf("the descending order:\n"); 
     desc_order(head); 
     break; 

     default : 
     printf("you have entered the wrong choice"); 
     break; 
    } 

    free_variables(head); 

return 0; 
} 

** 이진 트리를 사용하여 숫자를 정렬하기 위해 작성한 코드입니다.이 트리의 헤드 노드 만 인쇄합니다. 문제는 add_node function.When 나는 문제는 add_node()에 여기 ** 저를 탈 도와주세요 처음 code.someone에서 문제가 될 것으로 보인다 무엇이진 트리의 모든 요소를 ​​인쇄 할 수 없습니다.

if(value==pnode->data) 
{ 
    (pnode->count)++; 
    return pnode; 
} 
if(value<pnode->data) 
{ 
    if(pnode->previous==NULL) 
    { 
     pnode->previous=create_node(value); 
     return pnode->previous; 
    } 
    else 
    { 
     return add_node(pnode->previous,value); 
    } 
} 
else 
{ 
    if(pnode->next==NULL) 
    { 
     pnode->next=create_node(value); 
     return pnode->next; 
    } 
    else 
    return add_node(pnode->next,value); 
} 

+0

가 있습니까

그것은 대신 같은 것을해야한다 당신은 당신의 문제가 I/O에 있지 않은 것이 틀림 없습니까? 또한'fflush()'는 출력 스트림에서만 작동합니다. 'fflush (stdin)'이 정의되지 않았습니다. – timrau

+0

no.its는 두 번째 코드에서 정상적으로 작동하지만 아침에 멈춘 첫 번째 code.am에 대해 작동하지 않습니다. – starkk92

답변

1

을 add_node의 내용을 대체 :

else 
{ 
    if(pnode->data>value) 
    { 
     return add_node(pnode->previous,value); 
    } 
    else 
    { 
     return add_node(pnode->next,value); 
    } 
} 

pnode->previous 또는 pnode->nextNULL 인 경우 add_node()으로 생성 된 노드는 pnode에 전혀 연결되어 있지 않습니다. 다음과 같이 수정할 수 있습니다 :

else 
{ 
    if(pnode->data>value) 
    { 
     pnode->previous = add_node(pnode->previous,value); 
    } 
    else 
    { 
     pnode->next = add_node(pnode->next,value); 
    } 
    return pnode; 
} 
+0

'pnode-> previous/next'가'NULL'이 아닌 경우 위의 코드는 트리를 손상시킵니다. –

+0

'add_node()'는'pnode! = NULL' 때마다 입력'pnode' 자체를 반환하기 때문에 할당은 실제로 아무것도하지 않습니다. 따라서 트리가 손상되지 않습니다. – timrau

+0

죄송합니다. 반환 명세서를 놓쳤습니다. 이것은 불필요한 과제를 수행 할지라도 잘 작동 할 것입니다. –

1

문제는 add_node()이 트리를 재귀하지만 수정하지 않습니다. 함수에서 하나의 노드가 어떤 노드의 previous 또는 next 포인터를 바꿀 위치가 하나도 없습니다. 어떻게 그 나무를 수정할 수 있습니까? 그것은 할 수 없다.

struct node *add_node(struct node *pnode, int value) 
{ 
    if (pnode == NULL) 
    { 
     pnode = create_node(value); 
     return pnode; 
    } 
    else if (pnode->data == value) 
    { 
     pnode->count++; 
     return pnode; 
    } 

    if (pnode->data > value) 
    { 
     if (pnode->previous == NULL) 
     { 
      return pnode->previous = create_node(value); 
     } 
     return add_node(pnode->previous, value); 
    } 
    else 
    { 
     if (pnode->next == NULL) 
     { 
      return pnode->next = create_node(value); 
     } 
     return add_node(pnode->next, value); 
    } 
} 

전체 프로그램 사소한 수정, 변경 및 향상된 서식 :

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

struct node *create_node(int); 
struct node *add_node(struct node *, int); 
void asce_order(struct node *); 
void desc_order(struct node *); 

struct node 
{ 
    int data; 
    int count; 
    struct node *next, *previous; 
}; 

struct node *create_node(int value) 
{ 
    struct node *pnode = malloc(sizeof(struct node)); 
    pnode->data = value; 
    pnode->count = 1; 
    pnode->next = pnode->previous = NULL; 
    return pnode; 
} 

struct node *add_node(struct node *pnode, int value) 
{ 
    if (pnode == NULL) 
    { 
     pnode = create_node(value); 
     return pnode; 
    } 
    else if (pnode->data == value) 
    { 
     pnode->count++; 
     return pnode; 
    } 

    if (pnode->data > value) 
    { 
     if (pnode->previous == NULL) 
     { 
      return pnode->previous = create_node(value); 
     } 
     return add_node(pnode->previous, value); 
    } 
    else 
    { 
     if (pnode->next == NULL) 
     { 
      return pnode->next = create_node(value); 
     } 
     return add_node(pnode->next, value); 
    } 
} 

void asce_order(struct node *pnode) 
{ 
    int i; 

    if (pnode->previous != NULL) 
    { 
     asce_order(pnode->previous); 
    } 

    for(i = 0; i < pnode->count; i++) 
    { 
     printf("%d\n", pnode->data); 
    } 

    if(pnode->next != NULL) 
    { 
     asce_order(pnode->next); 
    } 
} 

void desc_order(struct node *pnode) 
{ 
    int i; 

    if (pnode->next != NULL) 
    { 
     desc_order(pnode->next); 
    } 

    for (i = 0; i < pnode->count; i++) 
    { 
     printf("%d\n", pnode->data); 
    } 

    if (pnode->previous != NULL) 
    { 
     desc_order(pnode->previous); 
    } 
} 

void free_variables(struct node *pnode) 
{ 
    if (pnode == NULL) 
    { 
     return; 
    } 

    if (pnode->next != NULL) 
    { 
     free_variables(pnode->next); 
    } 

    if (pnode->previous != NULL) 
    { 
     free_variables(pnode->previous); 
    } 

    free(pnode); 
} 

int main(void) 
{ 
    struct node *head=NULL; 

    head = add_node(head, 2); 
    add_node(head, 0); 
    add_node(head, 6); 
    add_node(head, 7); 
    add_node(head, 4); 
    add_node(head, 2); 
    add_node(head, 8); 
    add_node(head, 3); 
    add_node(head, 7); 
    add_node(head, 5); 
    add_node(head, 0); 
    add_node(head, 1); 
    add_node(head, 6); 
    add_node(head, 9); 

    printf("ascending order:\n"); 
    asce_order(head); 

    printf("descending order:\n"); 
    desc_order(head); 

    return 0; 
} 

출력 (ideone) :

ascending order: 
0 
0 
1 
2 
2 
3 
4 
5 
6 
6 
7 
7 
8 
9 
descending order: 
9 
8 
7 
7 
6 
6 
5 
4 
3 
2 
2 
1 
0 
0 
+0

그걸로 뭐라하고 싶니? –

+0

add_node 함수에서 참조로 전달하여 함수를 호출했습니다. 노드가 NULL이라도 add_node 함수의 첫 번째 명령문을 사용하여 기존 트리에 추가해야합니다. if (pnode == NULL) { pnode = create_node (값); return pnode; } '. 만약 내가 틀렸다면 정정 해주세요. 감사합니다 ... – starkk92

+0

당신이 말하는 것과 내가 지적하고 고치던 버그와 어떻게 관련이 있는지 이해할 수 없습니다. –