C程序反向链表 [英] C program reversed linked list

查看:26
本文介绍了C程序反向链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 c 编写一个程序,用链表添加大数字.我用反向来添加数字,但我不能让它再次反向.应该多次使用(哎呀,循环问号直到退出)

Im trying to write a program in c that adds big numbers with linked list. I used reverse to add the numbers, but i cant get it to reverse again. It should be used several times(iow, looped to ask numbers until exit)

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

int main()
{
    char n1[500];
    int lenSum, len;
    printf("Welcome! 
This program performs addition of big whole numbers that can contain upto a maximum of 500 digits.");
    printf("
Enter 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("
Your first number is:	 ");
        while(root!=NULL){
        printf("%d	",root->digit);
        root=root->next;
    }

    root2=head2;
    printf("
Your second number is: ");
        while(root2!=NULL){
        printf("%d	",root2->digit);
        root2=root2->next;
    }
    printf("
Calculating sum:
");

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

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

    printf("
");

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

    root, head = reverseLinkedList(sum, headSum);
    printDigit(root);
    root=head;
    printf("

	 ");
        while(root!=NULL){
        printf("%d	",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
");
    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;
}

推荐答案

link list reverse sample like this

link list reverse sample like this

#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("
");
}

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

这篇关于C程序反向链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆