2012-10-11 71 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功能。當我更換add_node內容與無法打印二叉樹中的所有元素

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); 
} 

什麼似乎是在第一code.someone問題,請幫我out.thanks **

+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->previouspnode->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->上一個/下一個'不是'NULL',上面的代碼將破壞樹。 –

+0

由於每當'pnode!= NULL'時,add_node()都會返回輸入'pnode',所以這個賦值實際上什麼都不做。因此,樹不會被破壞。 – timrau

+0

糟糕,我錯過了退貨聲明。這也應該起作用,儘管它會做一些不必要的任務。 –

1

問題是,add_node()做遞歸樹,但不修改它。在函數中沒有一個地方會改變某個節點的指針previousnext。如何修改樹?它不能。

它應該是這樣,而不是:

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; } '。請糾正我,如果我wrong.thanks .. – starkk92

+0

我不明白你在說什麼,以及如何涉及到我指出並修復的錯誤。 –