簡體   English   中英

C程序反向鏈接列表

[英]C program reversed linked list

我試圖用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;
}

由於問題並不是針對您所面臨的問題,因此請注意以下幾點,

在函數reverse()

return reversed, reversedTail; 將始終返回reversedTail

您正在嘗試從main()調用此函數,如下所示:

root, head = reverseLinkedList(sum, headSum); 再次head將獲得返回值。 這是不正確的。 通常,您一次應該分配給一個成員。

鏈接列表反向樣本是這樣的

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM