首页 > 解决方案 > 反转链表中的指针

问题描述

大家好,请您帮我编写一个程序来反转链表中的指针。例如 A->B->C->D 将变为 A<-B<-C<-D 而不使用额外的链表。

编辑:------好吧伙计们,所以我一直在寻找解决这个问题的方法,如果你想要的话,这里是代码:

    void reverse_list(){
    struct node *next, *current,*previous;
    previous = NULL; 
    current =head; 
    while(current != NULL){
    next = current->next; 
    current->next = previous; 
    previous=current; 
    current = next; 
    }
    head = previous; 
    }

标签: c

解决方案


正如您没有提到您是否自己实施链表一样。

所以首先我假设你是自己做的。所以下面是链表的一个实现,它的指针再次反转以使链表反转。你可以从中得到一个想法。

#include<stdio.h>
#include<stdlib.h>//for using malloc

struct Node//Defining a structure for linked list's node
{
    int info;//assuming it is an integer linked list
    struct Node *link;//this pointer links a node to it's immediate neighbor node
};

struct Node *head=NULL,*temp;//some initializations and declarations

void insertion(int data)//To insert elements to linked list
{
    struct Node *ptr;
    ptr=malloc(sizeof(*ptr));//creating a new node for the newcomer
    ptr->info=data;//taking the given integer value for the node to hold

    //initializing with null as the current node may be the last node and
    //if it is then it will point nobody
    //...but if it is not when a new node comes in the future it will eventually be
    //replaced to point the newcomer
    ptr->link=NULL;

    if(head==NULL)//head still null means we are creating the first node
    {             //handling the head separately as it has no parent node
        head=ptr;
        temp=ptr;//copying the current node's pointer to temp such that we can
        //find it as a parent of next node
    }
    else//for the rest of the nodes' creation
    {
        //as temp is the pointer to the previous node, so previous node is linking
        //to its next node, i.e, the current node
        temp->link=ptr;

        //updating the temp to point the current node such that it can act as a parent node
        //when the next node comes
        temp=ptr;
    }
}

void reversePointers()
{
    struct Node *trav,*from=NULL,*temp;
    for(trav=head;;)
    {
        if(trav->link==NULL)//if we have reached to the end
        {
            head=trav;//then the reverse linked list's head should point to the last element
            trav->link=from;//and making the second last node as it's next node
            break;
        }
        temp=trav;//saving current node pointer to update the "from" pointer
        trav=trav->link;//advancing current node pointer to forward
        temp->link=from;//making the current node to point to it's previous node
        from=temp;//saving current node's pointer which will be used in next iteration
    }
}

void traverse()//to traverse the nodes
{
    struct Node *ptr=head;
    while(ptr!=NULL)
    {
        printf("%d ",ptr->info);
        ptr=ptr->link;
    }
    printf("\n");
}

int main(void)
{
    int i,n,t;

    printf("Enter Number of elements: ");
    scanf("%d",&n);

    printf("Enter Elements: ");
    for(i=0;i<n;i++)
    {
        scanf("%d",&t);
        insertion(t);
    }
    printf("Before reversing the pointers the elements are: ");
    traverse();

    //let's reverse the pointers to make the list to go backward
    reversePointers();

    printf("After reversing the pointers the elements are: ");
    traverse();

}

其次,如果您使用的是 STL 列表,那么该方法非常简单。只需使用,

your_list_name.reverse()

同样,如果您只想为了迭代目的而反转 STL 列表,那么实际上不需要反转它。相反,您可以使用反向迭代器,如下所示(例如整数列表):

for(list<int>::reverse_iterator it=your_list_name.rbegin();it!=your_list_name.rend();it++)
{
    //do whatever you want
}

推荐阅读