【问题标题】:C program reversed linked listC程序反向链表
【发布时间】:2014-03-06 13:53:57
【问题描述】:

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

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

【问题讨论】:

  • 你必须更具体。
  • 请显示您期望哪个输入的哪个输出。
  • 我的程序基本上是用链表来加数的,但是需要用倒数,否则加法会出错。当我在链表中​​有我的反向编号时,我想再次反转它,使其位于“右侧”,但我不知道该怎么做。
  • 没有很仔细的看你的代码,但是一看你的代码我可以告诉你,倒序链表的时候不需要malloc新节点。可以使用 2 个指针来完成 - 一个用于存储下一个节点,另一个用于存储前一个节点。反转列表只是操纵指针。

标签: c linked-list reverse


【解决方案1】:

由于您所面临的问题不是很具体,以下是需要注意的几点,

在函数reverse()

return reversed, reversedTail; 将始终返回 reversedTail

您正试图从main() 调用此函数

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

【讨论】:

    【解决方案2】:

    这样的链接列表反向示例

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 2013-07-01
      相关资源
      最近更新 更多