C-我想反转一个链表



我想反转一个链表,但我的代码似乎不起作用。我不知道它出了什么问题。我使用了我在这里找到的reverse()函数,我甚至在纸上测试了它,所以我很确定代码很好,但我肯定还是遗漏了一些东西。我很想知道出了什么问题。这是我的代码:

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

typedef struct node{
    int val;
    struct node * next;
}node;
void push(int val, node **head){
    node* temp=(node*)malloc(sizeof(node));
    node* current=*head;
    temp->val=val;
    if(*head==NULL)
        {*head=temp;
        temp->next=NULL;}
    else
        {while(current->next!=NULL)
            current=current->next;
    current->next=temp;
    temp->next=NULL;}
 }
int reverse(node * head){
   node *previous = NULL;
   node *current = head;
   node *forward;
   while (current != NULL) {
       forward = current->next;
       current->next = previous;
       previous = current;
       current = forward;
      }
    return previous;
   }
 void print(node *new_head){
    node* current2=new_head;
    current2=current2->next;
    while(current2!=NULL)
     {
       printf("%d", current2->val);
       current2=current2->next;
      }}
int main()
{   node * head= NULL;
    int n;
    node * new_head;
    scanf("%d", &n);
    push(n,head);
    scanf("%d", &n);
    push(n,head);
    scanf("%d", &n);
    push(n,head);
    new_head=reverse(head);
    print(new_head);
    return 0;}

我只想反转一个输入,比如:1,2,3。所以输出是3,2,1。

尝试以下

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int val;
    struct node * next;
} node;
void push( node **head, int val )
{
    node **current = head;
    while ( *current ) current = &( *current )->next;
    *current = ( node * )malloc( sizeof( node ) );
    ( *current )->val  = val;
    ( *current )->next = NULL;
}
node * reverse( node * head )
{
    node *new_head = NULL;
    node *current = head;
    while ( current ) 
    {
        node *next = current->next;
        current->next = new_head;
        new_head = current;
        current = next;
    }
    return new_head;
}
void print( const node *head )
{
    for ( ; head; head = head->next ) printf( "%d ", head->val );
    printf( "n" );
}
void delete( node *head )
{
    while ( head )
    {
        node *tmp = head;
        head = head->next;
        free( tmp );
    }
}
int main()
{   
    node *head = NULL;
    int n;
    scanf( "%d", &n );
    push( &head, n );
    scanf( "%d", &n );
    push( &head, n );
    scanf( "%d", &n );
    push( &head, n );
    print( head );
    head = reverse( head );
    print( head );
    delete( head );
    return 0;
}

如果输入1 2 3,输出将为

1 2 3 
3 2 1 

实际上,您的反向操作正在运行。push方法不符合规范:它将新元素移动到列表的末尾,而不是当前头之前。因此,在调用reverse方法之前,您已经有了反向列表(这会迅速将其反向)。

#include <stdio.h>
#include <stdlib.h>
typedef struct node{
    int val;
    struct node * next;
}node;
void push(int val, node **head){
    node* temp=(node*)malloc(sizeof(node));
    node* current=*head;
    temp->val=val;
    temp->next=NULL;
    if(*head==NULL){
        *head=temp;
    } else {
        while(current->next!=NULL)
            current=current->next;
        current->next=temp;
    }
}
node *reverse(node * head){
    node *rev = NULL;
    node *current = head;
    node *temp;
    while (current != NULL) {
        temp = current->next;
        current->next =rev;
        rev = current;
        current = temp;
    }
    return rev;
}
void print(node *new_head){
    node* current2=new_head;
    while(current2!=NULL){
        printf("%d ", current2->val);
        current2=current2->next;
    }
    printf("n");
}
int main(){
    int n;
    node * head= NULL;
    node * new_head;
    scanf("%d", &n);
    push(n, &head);
    scanf("%d", &n);
    push(n, &head);
    scanf("%d", &n);
    push(n, &head);
    //print(head);
    new_head=reverse(head);
    print(new_head);
    return 0;
}

push( n, &head);是您必须执行的操作,以便逐个指针传递head。否则,在push方法中,node* current=*head;将导致分段错误,因为您传递的是空指针本身,而不是指向它的指针。

相关内容

  • 没有找到相关文章

最新更新