大家好,请您帮我编写一个过程,以反转链接列表中的指针。例如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;
}
答案 0 :(得分:0)
您可以将列表视为堆栈。然后,您可以通过“弹出”节点并将它们“推送”到新列表中来轻松地反转此类列表。
以上操作既可以破坏性地(破坏旧列表),也可以非破坏性地(将新列表创建为原始列表的反向副本)进行。
答案 1 :(得分:0)
您没有提到是否要自己实现链表。
因此,首先我假设您是自己做的。因此,以下是链表的实现,并且其指针再次反转以使链表反转。您可以从中汲取灵感。
#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
}