C程序反转链表

时间:2014-03-06 13:53:57

标签: c linked-list reverse

我试图在c中编写一个程序,用链表添加大数字。我用反向添加数字,但我不能让它再次反转。它应该被使用几次(低,循环询问数字直到退出)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "function.c"

int main()
{
    char n1[500];
    int lenSum, len;
    printf("Welcome! \nThis program performs addition of big whole numbers that can contain upto a maximum of 500 digits.");
    printf("\nEnter first number: ");

    scanf("%s", n1);
    len = strlen(n1);
    node *root = create(n1[0]);
    node *head = root;
    root, head = createList(n1,root,head,len);
    root=head;

    printf("Enter second number: ");
    scanf("%s", n1);
    len = strlen(n1);
    node *root2 = create(n1[0]);
    node *head2 = root2;
    root2, head2 = createList(n1,root2,head2,len);
    root2=head2;

    root=head;
    printf("\nYour first number is:\t ");
        while(root!=NULL){
        printf("%d\t",root->digit);
        root=root->next;
    }

    root2=head2;
    printf("\nYour second number is: ");
        while(root2!=NULL){
        printf("%d\t",root2->digit);
        root2=root2->next;
    }
    printf("\nCalculating sum:\n");

    root=head;
    root2=head2;
    node *sum = create('0');
    node *headSum = sum;
    sum,headSum = addIntegers(root, root2, sum, headSum);

    printf("\nThe sum is : ");
    sum=headSum;
        while(sum->next!=NULL){
        printf("%d",sum->digit);
        sum=sum->next;
    }

    printf("\n");

    sum = headSum;
    printf("\nThe number ");
    saveResult(sum);
    printf("has been saved in the file 'results.txt'\n");

    root, head = reverseLinkedList(sum, headSum);
    printDigit(root);
    root=head;
    printf("\n\n\t ");
        while(root!=NULL){
        printf("%d\t",root->digit);
        root=root->next;
    }

    free(root);
    free(root2);
    //free(sumDigit);//
    return 0;
}

function.h:

#ifndef function.h
#define function.h

typedef struct node {
  int digit;
  struct node *next;
}node;
node* create(char digit);
node* createList(char number[500], node* root,node* head, int length);
node* addIntegers(node* root, node* root2, node* sum, node* headSum);
#endif

function.c:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "function.h"

node* createList(char number[500], node* root,node* head, int length){
    int i;
    i=0;
    for(i=1;i<=length-1;i++) {
        root = (node *)malloc(sizeof(node));
        root->digit = number[i]-'0';
        root->next  = head;
        head = root;
    }
    return root, head;
}
void printDigit(node* root){
        while(root!=NULL){
        printf("%d",root->digit);
        root=root->next;
    }
}
node* addIntegers(node* root, node* root2, node* sum, node* headSum){
    int carry = 0;
    int sumDigit = 0;
    while((root!=NULL || root2!=NULL)) {
        if (root==NULL || root2==NULL){
            if (root == NULL){
                sumDigit=root2->digit +carry;
                root2=root2->next;
                goto add;}
            if (root2 == NULL){
                sumDigit = root->digit + carry;
                root=root->next;
                goto add;
            }
        }
        else{
        sumDigit = root->digit + root2-> digit + carry;
        }
        root2 = root2->next;
        root = root->next;
        add:
        sum = (node *)malloc(sizeof(node));
        sum->digit = sumDigit%10;
        sum->next  = headSum;
        headSum = sum;

        if (sumDigit > 9){
            carry = 1;}
        else{
            carry = 0;
        }
    }
    if (carry == 1){
        sum = (node *)malloc(sizeof(node));
        sum->digit = 1;
        sum->next  = headSum;
        headSum = sum;
    }
    return sum, headSum;
}

node* create(char digit)
{
  node *root = (node *) malloc( sizeof(node));
  if (root == NULL){
    printf("ERROR\n");
    exit(1);
  }
  root->digit = digit-'0';
  root->next = NULL; //default is null
  return root;
}

void saveResult(struct node *sum)
{
    FILE * fp = fopen ("result.txt", "w+");
    while(sum->next != NULL)
    {
        printf("%d", sum->digit);
        fprintf(fp, "%d", sum->digit);
        sum = sum->next;
    }
    fclose(fp);
    printf(" ");
}

node* reverseLinkedList(node *root, node* head){
    node* reversed = create(root->digit);
    node* reversedTail = reversed;
    while(root!=NULL){
        //printf("%d", root->digit);
        root = root->next;
        reversed->digit = root;
        reversed->next =head->next;
        reversed = reversed->next;
        head = root;
    }
    reversed = reversedTail;
    return reversed, reversedTail;
}

2 个答案:

答案 0 :(得分:0)

由于问题与您所面临的问题不太一致,以下是需要注意的一些问题,

在函数reverse()中,

return reversed, reversedTail;将始终返回reversedTail

您正尝试将此功能从main()调用为

root, head = reverseLinkedList(sum, headSum);再次head将获得返回值。这是不正确的。通常,您应该一次使用一个成员的作业。

答案 1 :(得分:0)

像这样链接列表反向样本

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

typedef struct node {
  int digit;
  struct node *next;
} node;

node *reverse_copy(node *list){
    node *new_list = NULL;
    while(list){
        node *new_node = malloc(sizeof(node));
        new_node->digit = list->digit;
        new_node->next = new_list;
        new_list = new_node;
        list = list->next;
    }
    return new_list;
}

void reverse_not_copy(node **header){
    node *tmp, *list, *newList = NULL;
    if (header==NULL || *header == NULL) return;
    list = *header;
    while(list != NULL){
        tmp = list;
        list = list->next;
        tmp->next = newList;
        newList = tmp;
    }
    *header = newList;
}

void print(node *head){
    while(head){
        printf("%d ", head->digit);
        head = head->next;
    }
    printf("\n");
}

int main(void){
    node *np;
    node n[3] = { {1,NULL}, {2, NULL}, {3, NULL}};
    n[0].next = &n[1];
    n[1].next = &n[2];

    print(&n[0]);//1 2 3
    np=reverse_copy(&n[0]);
    print(np);//3 2 1
    reverse_not_copy(&np);
    print(np);//1 2 3
    return 0;
}