递归地反转链表数组并不是按顺序反转所有节点

时间:2017-11-07 06:00:44

标签: c recursion file-io linked-list

我有一个链表的数组,我试图递归反转。当我调用函数反转时,它不会反转所有节点,而是反转几个节点。

反向功能似乎是删除第一个节点(基本情况)并用最后一个节点填充其位置(子情况结束)。我认为问题在于在 reverse_nodes 函数中调用for循环,但这似乎没有解决它。

这是一些输出..

pre-reverse function:
-----
group 0

alice, 2
-----
group 1

martin, 4
-----
group 2

keanu, 6
-----
group 3

miles, 8


post - reverse function
-----
group 0

miles, 8
-----
group 1

martin, 4
-----
group 2

keanu, 6
-----
group 3

miles, 8

我试图让它反转为:8,6,4,2

请注意我只包含相关的代码块,例如结构体系结构,头/尾构造,在读取二进制文件之前删除所有节点,将二进制文件读取到节点以及主要功能。我可以得到一些帮助,找出反向功能导致它不能完全反转的内容吗?谢谢你的时间。请参阅下面的代码!

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

    typedef struct node
    {
        char name[20];
        int size;
        struct node *next;
    }node;

    node* head[4]={NULL,NULL,NULL,NULL};
    node* tail[4]={NULL,NULL,NULL,NULL};


    void wipe_nodes()
    {
        int i;

        node *p=NULL;

        for(i=4; i>0; i--)
        {
            p=head[i];

            if(p == NULL)
            {
                printf("Nodes are clear!\n");
            }

            while(p != NULL)
            {
                delete_party(p->name, p->size); // cant call name and size
                p = p -> next;
            }

        }
    }


    void bin_to_list(char *filename)
{
    FILE *fp;
    int ret;


    fp = fopen(filename, "rb");

    if (fp == NULL)
    {
        printf("Null file!\n");
        return;
    }



    node temp;

    //temp = (node *)malloc(sizeof(node));



    while((ret = fread(&temp, sizeof(node), 1, fp) > 0))
    {
        printf("%s %d", temp.name, temp.size);
        if(temp.size == 0)
        {
            printf("\nThat is not a valid command. Party not added!\n");
        }
        if(temp.size >= 1 && temp.size <= 2)
        {
            add_party(0, temp.name, temp.size);
        }
        else if(temp.size >= 3 && temp.size <= 4)
        {
            add_party(1, temp.name, temp.size);
        }
        else if(temp.size >= 5 && temp.size <= 6)
        {
            add_party(2, temp.name, temp.size);
        }
        else if(temp.size >= 7)
        {
            add_party(3, temp.name, temp.size);
        }
    }

    fclose(fp);
    return;
}


    void reverse_nodes(node *p, node *q)
    {
        int i;

        for(i=0; i<4; i++)
        {
            //node *p=head[i];

            if(p == NULL)
            {
                printf("Error, no nodes!\n");
                return;
            }


            if (p->next == NULL)
            {
                printf("LOL, only one node! Can't reverse!\n");
                head[i] = p;
            }

            else
            {
                reverse_nodes(p->next, p);
            }

            p->next = q;
            return;



    int main(int argc, char *argv[])
    {
        int x, i;
        read_to_list(argv[1]);
        bin_to_list(argv[2]);
        while (1)
        {
            fflush(stdin);
            printf("\n\nEnter 1 to add a party\nEnter 2 to remove a party\nEnter 3 for the list of the party\nEnter 4 to change party size.\nEnter 5 to quit (write to .txt file).\nEnter 6 to read from bin file.\nEnter 7 to reverse the list.\n\n");

            scanf("%d",&x);
            char name[20];
            int size;
            switch(x)
            {

                case 1:
                    printf("\nParty Name: ");
                    scanf("%s", name);
                    printf("\nParty Size: ");
                    scanf("%d", &size);
                    if(size == 0)
                    {
                        printf("\nThat is not a valid command. Party not added!\n");
                    }
                    if(size >= 1 && size <= 2)
                    {
                        add_party(0, name, size);
                    }
                    else if(size >= 3 && size <= 4)
                    {
                        add_party(1, name, size);
                    }
                    else if(size >= 5 && size <= 6)
                    {
                        add_party(2, name, size);
                    }
                    else if(size >= 7)
                    {
                        add_party(3, name, size);
                    }
                    break;

                case 2:
                    printf("\nSize of party to delete: ");
                    scanf("%i", &size);
                    delete_party(NULL, size);
                    break;

                case 3:
                    list_parties();
                    break;

                case 4:
                    change_partysize(name, size);
                    break;

                case 5:
                    write_to_file(argv[1]);
                    write_to_bin(argv[2]);
                    exit(0);
                    break;

                case 6:
                    wipe_nodes();
                    bin_to_list(argv[2]);
                    break;

                case 7:
                    for(i=0; i<4; i++)
                    {
                        reverse_nodes(head[i], NULL);
                    }
                    break;

                default:
                    continue;
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

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

/* Link list node */
struct Node
{
    int data;
    struct Node* next;
};

/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
    struct Node* prev   = NULL;
    struct Node* current = *head_ref;
    struct Node* next;
    while (current != NULL)
    {
        next  = current->next;  
        current->next = prev;   
        prev = current;
        current = next;
    }
    *head_ref = prev;
}

/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node =
            (struct Node*) malloc(sizeof(struct Node));

    /* put in the data  */
    new_node->data  = new_data;

    /* link the old list off the new node */
    new_node->next = (*head_ref);    

    /* move the head to point to the new node */
    (*head_ref)    = new_node;
}

/* Function to print linked list */
void printList(struct Node *head)
{
    struct Node *temp = head;
    while(temp != NULL)
    {
        printf("%d  ", temp->data);    
        temp = temp->next;  
    }
}    

/* Driver program to test above function*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;

     push(&head, 20);
     push(&head, 4);
     push(&head, 15); 
     push(&head, 85);      

     printf("Given linked list\n");
     printList(head);    
     reverse(&head);                      
     printf("\nReversed Linked list \n");
     printList(head);    
     getchar();
}

答案 1 :(得分:0)

您的reverse_nodes函数只是设置next的{​​{1}}成员,而不是当前节点,因此当它到达列表末尾时,它将设置node <} p}。链接列表中前一个成员的最后一个成员,但保持最后一个成员不变。

这对我有用:

next

编辑:

递归通常是一个坏主意,因为你可能最终会破坏堆栈 - 如果可能的话,最好尝试在单个(或多个,如果需要的话)函数中做你需要做的事情,在这种情况下很容易。