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

最初のコードの問題と思われるもの.誰か助けてください.thanks**

4

2 に答える 2

1

問題はここにありますadd_node()

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

pnode->previousまたはpnode->nextがの場合NULL、 によって作成されたノード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;
}
于 2012-10-11T17:22:20.553 に答える
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
于 2012-10-11T17:30:58.563 に答える