C中的链接列表反转不起作用

时间:2015-05-30 18:26:39

标签: c linked-list singly-linked-list

我想在C中使用链表进行一些操作。我已经为此编写了一些函数 - 在开头插入,在尾部插入,从开头删除等。在我的代码中,我试图实现链表的反转。但它没有按预期工作。它始终只打印第一个值。

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *link;
}*new_node,*ptr,*head = NULL,*tmp;

void create_list(int data)
{
    new_node = (struct node*) malloc(sizeof(struct node));
    head = new_node;
    new_node->link=NULL;
    new_node->data=data;
}

void insert_beginning(int data)
{

    if(head==NULL)
    {
        create_list(data);
    }
    else
    {
        new_node = (struct node*) malloc(sizeof(struct node));
        new_node->link = head;
        new_node->data=data;
        head=new_node;
    }
}
void insert_tail(int data)
{
    if(head==NULL)
    {
        create_list(data);
    }
    else
    {
        new_node = (struct node*) malloc(sizeof(struct node));
        ptr = head;
        while(ptr->link!=NULL)
        {
            ptr=ptr->link;
        }
        new_node->link = NULL;
        new_node->data=data;
        ptr->link=new_node;
    }
}
void insert_any_position(int data)
{
    int pos;
    printf("\nEnter the position: ");
    scanf("%d",&pos);
    if(pos==1)
    {
        insert_beginning(data);
    }
    else
    {
        int i=1;
        new_node = (struct node*) malloc(sizeof(struct node));
        ptr = head;
        while(ptr->link!=NULL&&i<(pos-1))
        {
            ptr=ptr->link;
            i++;
        }
        new_node->link = ptr->link;
        new_node->data=data;
        ptr->link=new_node;
    }
}
void insert_node()
{
    int choice,data;
    printf("\n1.Insert at beginning\n2.Insert at tail\n3.Insert at any position\n4.Exit\n");
    scanf("%d",&choice);
    if(choice<4)
    {
        printf("\nEnter the data to be inserted: ");
        scanf("%d",&data);
        switch(choice)
        {
        case 1:
            insert_beginning(data);
            break;
        case 2:
            insert_tail(data);
            break;
        case 3:
            insert_any_position(data);
            break;
        default:
            return;
        }
    }
    else
        return;

}

void delete_beginning()
{
    head = head->link;
}

void delete_tail()
{
    ptr=head;
    while(ptr->link!=NULL)
    {
        tmp=ptr;
        ptr=ptr->link;
    }
    tmp->link=NULL;
}
void delete_any_position()
{   int pos;
    printf("\nEnter the position: ");
    scanf("%d",&pos);
    if(pos==1)
    {
        delete_beginning();
    }
    else
    {
        ptr=head;
        int i=1;
        while(ptr->link!=NULL && i<(pos-1))
        {
            ptr=ptr->link;
            i++;
        }
        ptr->link=(ptr->link)->link;
    }

}
void delete_data()
{

}

void delete_node()
{
    int choice,data;
    printf("\n1.Delete from beginning\n2.Delete from tail\n3.Delete from any position\n4.Delete data\n5.Exit\n");
    scanf("%d",&choice);
    if(choice<5)
    {

        switch(choice)
        {
        case 1:
            delete_beginning();
            break;
        case 2:
            delete_tail();
            break;
        case 3:
            delete_any_position();
            break;
        case 4:
            printf("\nEnter the data to be deleted: ");
            scanf("%d",&data);
            delete_data(data);
            break;
        default:
            return;
        }
    }
    else
        return;
}
void display_list()
{
    ptr = head;
    while(ptr!=NULL)
    {
        printf("%d ",ptr->data);
        ptr = ptr->link;
    }
}

void display_list_recursion(struct node* ptr)
{
    if(ptr==NULL)
        return;
    else
    {
        printf("%d ",ptr->data);
        display_list_recursion(ptr->link);
    }
}
void display_list_recursion_reverse(struct node* ptr)
{
    if(ptr==NULL)
        return;
    else    
    {
        display_list_recursion_reverse(ptr->link);
        printf("%d ",ptr->data);
    }   

}

这是无效的功能:

void reverse_list_iteration()
{
    struct node *current,*prev,*next;
    current=head;
    prev=NULL;
    while(current->link!=NULL)
    {
        next = current->link;
        current->link = prev;
        prev=current;
        current=next;
    }
    head=prev;
}

main()
{
    int choice;
    do
    {
        printf("\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Display using recursion");
        printf("\n5.Display Reverse\n6.ReverseListIteration\n7.Exit\n\t\t\t");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
            insert_node();
            break;
        case 2:
            delete_node();
            break;
        case 3:
            display_list();
            break;
        case 4:display_list_recursion(head);
            break;
        case 5:display_list_recursion_reverse(head);
            break;
        case 6:reverse_list_iteration();
            break;
        case 7:
            exit(0);
        }
    }
    while(choice<7);
}

其他功能正常。在调用reverse_list_iteration()然后调用display_list()后,它只显示一个数据。我认为有些链接已被破坏,但我无法弄清楚。

1 个答案:

答案 0 :(得分:3)

只需将current->link!=NULL更改为current!=NULL中的reverse_list_iteration()即可解决问题。

希望它对你有所帮助。快乐的编码。