C - 我想要反转链接列表

时间:2014-11-01 18:33:42

标签: c linked-list

我想要反转链接列表,我的代码似乎无法正常工作。我不知道它有什么问题。我使用了我在这里找到的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。

4 个答案:

答案 0 :(得分:0)

实际上,你的反面正在发挥作用。这是根据规范不起作用的push方法:它将新元素移动到列表的末尾,而不是在当前头部之前。因此,在调用reverse方法之前,您已经有了相反的列表(反过来又反过来了)。

答案 1 :(得分:0)

#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;
}

答案 2 :(得分:0)

push( n, &head);是为了通过指针传递head指针所必须做的。否则,push方法node* current=*head;将导致分段错误,因为您传递空指针本身而不是指向它的指针。

答案 3 :(得分:0)

尝试以下

#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