C程序颠倒链表 [英] C program reversed linked list
本文介绍了C程序颠倒链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试着用C编写一个程序,增加了大的数字与链表。我用反向补充的数字,但我不能让它再次逆转。它应该被用来多次(IOW,环问数字,直到出口)
的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括function.c诠释的main()
{
炭n1的[500];
INT lenSum,LEN;
的printf(欢迎\\ n该程序执行另外的大整数,可以包含高达最多500个数字。);
的printf(\\ n输入第一个数字:); scanf函数(%S,N1);
LEN = strlen的(N1);
节点*根=创建(N1 [0]);
节点*头=根;
根,头部= createList(N1,根,头,LEN);
根=头; 的printf(请输入第二个数字:);
scanf函数(%S,N1);
LEN = strlen的(N1);
节点* root2 =创建(N1 [0]);
节点* HEAD2 = root2;
root2,HEAD2 = createList(N1,root2,HEAD2,LEN);
root2 = HEAD2; 根=头;
的printf(\\ n您的第一个数字是:\\ t的);
而(根!= NULL){
的printf(%d个\\ t的则root>数字);
根=根 - >接下来,
} root2 = HEAD2;
的printf(\\ n您的第二个数字是:);
而(root2!= NULL){
的printf(%d个\\ t的,root2->数字);
root2 = root2->接下来,
}
的printf(\\ nCalculating总和:\\ n); 根=头;
root2 = HEAD2;
节点*总和=创建('0');
节点* headSum =总和;
总之,headSum = addIntegers(根,root2,总之,headSum); 的printf(\\ n此之和为:);
总和= headSum;
而(sum->!下次= NULL){
的printf(%d个,sum->数字);
综上所述= sum->接着,
} 的printf(\\ n); 总和= headSum;
的printf(\\ n此号码);
saveResult(总和);
输出(已保存的文件RESULTS.TXT'\\ n的); 根,头部= reverseLinkedList(总和,headSum);
printDigit(根);
根=头;
的printf(\\ n \\ n \\ t的);
而(根!= NULL){
的printf(%d个\\ t的则root>数字);
根=根 - >接下来,
} 免费(根);
免费(root2);
//自由(sumDigit); //
返回0;
}
function.h:
的#ifndef function.h
#定义function.htypedef结构节点{
INT数字;
结构节点*接下来的;
}节点;
节点*创建(字符数字);
节点* createList(字符数[500],节点*根,节点*头,INT长度);
节点* addIntegers(节点*根,节点* root2,节点*总之,节点* headSum);
#万一
function.c:
的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括function.h节点* createList(字符数[500],节点*根,节点*头,INT长度){
INT I;
I = 0;
对于(i = 1; I< =长度为1;我++){
根=(节点*)malloc的(的sizeof(节点));
根 - >数字=号[I] - '0';
根 - >接下来=头;
头=根;
}
返回根,头部;
}
无效printDigit(节点*根){
而(根!= NULL){
的printf(%d个根 - >数字);
根=根 - >接下来,
}
}
节点* addIntegers(节点*根,节点* root2,节点*总之,节点* headSum){
INT携带= 0;
INT sumDigit = 0;
而((根!= NULL || root2!= NULL)){
如果(根== NULL || root2 == NULL){
如果(根== NULL){
sumDigit = root2->数字+随身携带;
root2 = root2->接下来,
转到添加;}
如果(root2 == NULL){
sumDigit =根 - >数字+随身携带;
根=根 - >接下来,
转到添加;
}
}
其他{
sumDigit =根 - >数字+ root2->数字+随身携带;
}
root2 = root2->接下来,
根=根 - >接下来,
加:
总和=(节点*)malloc的(的sizeof(节点));
sum->数字= sumDigit%10;
sum->接下来= headSum;
headSum =总和; 如果(sumDigit> 9){
携带= 1;}
其他{
携带= 0;
}
}
如果(携带== 1){
总和=(节点*)malloc的(的sizeof(节点));
sum->位= 1;
sum->接下来= headSum;
headSum =总和;
}
返回总和,headSum;
}节点*创建(字符数字)
{
节点*根=(节点*)malloc的(的sizeof(节点));
如果(根== NULL){
的printf(ERROR \\ n);
出口(1);
}
根 - >数字=数字-'0';
根 - >接着= NULL; //默认为空
返回根;
}无效saveResult(结构节点*和)
{
FILE *计划生育=的fopen(的Result.txt,W +);
而(sum->!下次= NULL)
{
的printf(%d个,sum->数字);
fprintf中(FP,%D,sum->数字);
综上所述= sum->接着,
}
FCLOSE(FP);
的printf();
}节点* reverseLinkedList(节点*根,节点*头){
节点*逆转=创建(根 - >数字);
节点* reversedTail =反转;
而(根!= NULL){
//的printf(%d个根 - >数字);
根=根 - >接下来,
reversed->数字=根;
reversed->接下来=流浆>接下来,
逆转= reversed->接下来,
头=根;
}
逆转= reversedTail;
返回逆转,reversedTail;
}
解决方案
链接列表扭转这样的样本
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;typedef结构节点{
INT数字;
结构节点*接下来的;
}节点;节点* reverse_copy(节点*名单){
节点* new_list = NULL;
而(名单){
节点* new_node =的malloc(sizeof运算(节点));
new_node->数字=列表 - >数字;
new_node->接下来= new_list;
new_list = new_node;
列表=列表 - >接下来,
}
返回new_list;
}无效reverse_not_copy(节点**头){
节点* TMP,*名单,* newList = NULL;
如果(头== NULL || *头== NULL)回报;
名单= *头;
而(名单!= NULL){
TMP =清单;
列表=列表 - >接下来,
tmp->接下来= newList;
newList = tmp目录;
}
*头= newList;
}无效打印(节点*头){
而(头){
的printf(%d个,流浆>数字);
头=流浆>接下来,
}
的printf(\\ n);
}诠释主要(无效){
节点* NP;
节点n [3] = {{1,NULL},{2,NULL},{3,NULL}};
N [0]的.next =安培; N [1];
N [1]。接下来=安培; N [2]; 打印(安培; N [0]); // 1 2 3
NP = reverse_copy(安培; N [0]);
打印(NP); // 3 2 1
reverse_not_copy(安培; NP);
打印(NP); // 1 2 3
返回0;
}
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! \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;
}
解决方案
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("\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;
}
这篇关于C程序颠倒链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文