从.txt文件创建一个单链列表,并反转C中每行的奇数

穆斯塔法·伊尔马兹(MustafaYılmaz)

我有一个有关链表的项目,但是很难。老师要我阅读.txt文件并从中创建单链接列表。之后,我需要反转每行的奇数。然后打印。这是我用于打印链表的代码。但是我需要帮助反转每行的奇数。

这是我用来打印列表的代码:

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

struct list {
    char *string;
    struct list *next;
};

typedef struct list LIST;

int main(void) {
    FILE *fp;
    char line[10];
    LIST *current, *head;

    head = current = NULL;
    fp = fopen("data.txt", "r");

    while(fgets(line, sizeof(line), fp)){
        LIST *node = malloc(sizeof(LIST));
        node->string = strdup(line);
        node->next =NULL;

        if(head == NULL){
            current = head = node;
        } else {
            current = current->next = node;
        }
    }
    fclose(fp);
    
    for(current = head; current ; current=current->next){
        printf("%s", current->string);
    }
    
    return 0;
}

这是.txt文件的内容:

10
9,6,11,7,12,18,19,14,15,13
13,14,9,12,15,3,18,20,1,2
4,11,8,17,12,15,20,10,3,16
19,4,11,1,13,17,12,16,20,18
1,6,20,11,13,9,7,16,10,2
12,4,11,16,3,20,9,19,17,15
20,3,10,12,18,2,5,14,15,16
18,19,15,2,6,9,1,3,17,4
7,6,20,1,11,4,3,5,8,16
1,2,16,13,17,10,12,9,4,15
苏瓦吉·帕特拉

希望这段代码能完成这项工作。

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

typedef struct line {
    struct num *first;
    struct line *next;
} LineNode;

typedef struct num {
    int num;
    int order;
    struct num *next;
} NumNode;

int main() {
    FILE *fp;
    char ch;
    int counter = 0;
    NumNode *curr_num, *even_ptr, *odd_ptr, *odd_head, *even_head;
    LineNode *curr_line, *line_head;

    curr_num = even_head = odd_head = even_ptr = odd_ptr = NULL;
    line_head = curr_line = NULL;
    fp = fopen("data.txt", "r");

    if (fp == NULL)
    {
        return 1;
    }
    
    ch = fgetc(fp);
    while(ch != EOF){
        if (ch >= 48 && ch <= 57)
        {
            int n = 0;
            while (ch != EOF && ch != '\n' && ch >= 48 && ch <= 57)
            {
                int x = ch - 48;
                n = n * 10 + x;
                ch = fgetc(fp);
            }
            NumNode *node = malloc(sizeof(NumNode));
            node->num = n;
            node->order = counter;
            node->next =NULL;

            if (n % 2 == 0){
                if(even_head == NULL){
                    even_head = even_ptr = node;
                } else {
                    even_ptr = even_ptr->next = node;
                }
            }else{
                if(odd_head == NULL){
                    odd_head = node;
                } else {
                    node->next = odd_head;
                    odd_head = node;
                }
            }

            counter++;
        }
        

        if (ch == '\n' || ch == EOF)
        {
            NumNode *num_node, *head;
            num_node  = head = NULL;
            even_ptr = even_head;
            odd_ptr = odd_head;
            counter = 0;

            if (even_head != NULL && even_head->order == counter){
                head = num_node = even_ptr;
                even_ptr = even_ptr->next;
            } else {
                head = num_node = odd_ptr;
                odd_ptr = odd_ptr->next;
            }
            
            counter++;

            while (even_ptr != NULL)
            {
                if (even_ptr->order == counter) {
                    num_node = num_node->next = even_ptr;
                    even_ptr = even_ptr->next;
                } 
                else if (odd_ptr != NULL) {
                    num_node = num_node->next = odd_ptr;
                    odd_ptr = odd_ptr->next;
                }

                counter++;
            }

            while (odd_ptr != NULL)
            {
                num_node = num_node->next = odd_ptr;
                odd_ptr = odd_ptr->next;
            }
            
            
            LineNode *node = malloc(sizeof(LineNode));
            node->next =NULL;
            node->first = head;
            if (line_head == NULL)
                line_head = curr_line = node;
            else
                curr_line = curr_line->next = node;

            odd_head = even_head = NULL;
            counter = 0;
        }

        ch = fgetc(fp);
    }
    fclose(fp);

    for(curr_line = line_head; curr_line != NULL ; curr_line=curr_line->next) {
        for(curr_num = curr_line->first; curr_num != NULL ; curr_num=curr_num->next) {
            printf("%d", curr_num->num);
            if (curr_num->next != NULL)
                printf(",");
        }
        printf("\n");
    }
    
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

给定一个节点,如何在单链列表中找到上一个节点

在java geeksforgeeks中的单链列表的末尾插入一个节点

反转C ++中的单链列表

在熊猫中每行创建一个唯一值?

创建一个Txt文件并写入

替换txt文件中每行上匹配文本的第一个实例

C语言中学生的单链接列表,无法访问另一个函数中创建的节点中的所有字段

读取CSV文件中的行并附加一个列表会为每个值创建一个列表列表

我如何在新的单链接列表中返回单链接列表的奇数索引节点?假定第一个节点的索引为1

如何从一个文件中创建多个列表?

代码在一个.py中创建新的.txt文件,但不在另一个.py中创建

从文本文件中读取行并为每行中的每个名称创建一个文本文件

通过外壳每行创建一个文件

C:将数据从文本文件添加到单链列表中

创建一个数字序列,文件中每行一个

将数据帧的内容写到.txt文件中,每行一个文件吗?

如何删除列表中的最后一个奇数

每行第一,第二和第三行读取一个txt文件,并保存在3个不同的列表中

如何从列表中的另一个文件创建新的txt文件?

从单链列表中删除最后一个节点

单链列表删除最后一个节点

在C ++中创建一个最小和最大范围为x和y的单链接列表

C-交换单链列表中的第一个和最后一个元素

从一个文件中需要一个猫鼬模型,该文件在一系列 promise.then.then 链中创建模型。

C# 如何从 IEnumerable 中创建一个列表

在新行中批量合并两个 .txt 文件,每行交替一个

创建一个字符串的副本,但在 C 中反转

如何在 txt 文件中创建一个中断以在 R 中创建一个新行

从 txt 文件中创建字典。每行一个字母。键是字母,值应该是循环的迭代