Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算C中的文本文件数时发生数据丢失_C_Count_Linked List_Singly Linked List - Fatal编程技术网

计算C中的文本文件数时发生数据丢失

计算C中的文本文件数时发生数据丢失,c,count,linked-list,singly-linked-list,C,Count,Linked List,Singly Linked List,我尝试创建一个函数,从文本文件中获取数字,并对不重叠且不超过某个数字的数字进行计数。我要把它放在链表里。计算一个与创建一个链表不重叠的数字是一件好事,但是计算超过某个数字的数字会有问题。结果比原来的数字少。在Excel中,有一些数字应该显示出来,但在我编写的C程序函数中,它们小于这个数字 void GetData(headNode* rheadnode) { int dataType; int newData; FILE* fp = NULL; fp = fope

我尝试创建一个函数,从文本文件中获取数字,并对不重叠且不超过某个数字的数字进行计数。我要把它放在链表里。计算一个与创建一个链表不重叠的数字是一件好事,但是计算超过某个数字的数字会有问题。结果比原来的数字少。在Excel中,有一些数字应该显示出来,但在我编写的C程序函数中,它们小于这个数字

void GetData(headNode* rheadnode)
{
    int dataType;
    int newData;
    FILE* fp = NULL;
    fp = fopen("input.txt", "r");
    if (fp != NULL) {

        while (fscanf(fp, "%d", &newData) != EOF)
        {
            dataType = InsertNode(rheadnode, newData);

            if (newData > 5000)
                Data_morethan5000_Count++;

            switch (dataType)
            {
            case 0:
                break;
            case 1:
                NodeCount++;
            }
        }
        fclose(fp);
    }
}
以上代码是整个程序代码的一部分。我正在使用一种方法从input.txt文件中获取一个数字,将其放入newData变量中,如果大于5000,则将Data_的值增加到5000_Count以上。 所以结果应该是45460。但是,C程序输出的结果值为45432。我想知道数据丢失发生在哪里。 下面是整个代码

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

typedef struct Node {
    int key;
    struct Node* link;
} listNode;

typedef struct Head {
    struct Node* head;
}headNode;


int NodeCount = 0;
int Data_morethan5000_Count = 0;

headNode* initialize(headNode* rheadnode);
int DeleteList(headNode* rheadnode);
void GetData(headNode* rheadnode);
int InsertNode(headNode* rheadnode, int key);
void PrintResult(headNode* rheadnode);

int main()
{
    int key;
    headNode* headnode = NULL;

    headnode = initialize(headnode);
    GetData(headnode);
    PrintResult(headnode);

    DeleteList(headnode);

    return 0;
}

headNode* initialize(headNode* rheadnode) {
    headNode* temp = (headNode*)malloc(sizeof(headNode));
    temp->head = NULL;
    return temp;
}

int DeleteList(headNode* rheadnode) {
    listNode* p = rheadnode->head;

    listNode* prev = NULL;
    while (p != NULL) {
        prev = p;
        p = p->link;
        free(prev);
    }
    free(rheadnode);
    return 0;
}


void GetData(headNode* rheadnode)
{
    int dataType;
    int newData;
    FILE* fp = NULL;
    fp = fopen("input.txt", "r");
    if (fp != NULL) {

        while (fscanf(fp, "%d", &newData) != EOF)
        {
            dataType = InsertNode(rheadnode, newData);

            if (newData > 5000)
                Data_morethan5000_Count++;

            switch (dataType)
            {
            case 0:
                break;
            case 1:
                NodeCount++;
            }
        }
        fclose(fp);
    }
}

int InsertNode(headNode* rheadnode, int key) {

    listNode* search, * previous;
    listNode* node = (listNode*)malloc(sizeof(listNode));

    node->key = key;

    search = rheadnode->head;
    previous = NULL;
    while (search != NULL)
    {
        if (node->key < search->key)
        {
            previous = search;
            search = search->link;
        }
        else if (node->key == search->key)
            return 0;
        else
            break;
    }
    if (previous == NULL)
    {
        node->link = rheadnode->head;
        rheadnode->head = node;
    }
    else
    {
        node->link = search;
        previous->link = node;
    }
        return 1;
}

void PrintResult(headNode* rheadnode) {
    /*
    The total number of nodes: 10011
    More than 5000 values: 45460
    Execution time: 1.234567 sec
    */
    printf("The total number of nodes: %d\n", NodeCount);
    printf("More than 5000 values: %d\n", Data_morethan5000_Count);
    printf("Execution time:  sec");
}
#包括
#包括
类型定义结构节点{
int键;
结构节点*链接;
}列表节点;
typedef结构头{
结构节点*头部;
}头节;
int NodeCount=0;
int Data_大于5000_计数=0;
headNode*初始化(headNode*rheadnode);
int DeleteList(头节点*rheadnode);
void GetData(headNode*rheadnode);
int InsertNode(headNode*rheadnode,int键);
作废打印结果(headNode*rheadnode);
int main()
{
int键;
headNode*headNode=NULL;
头节点=初始化(头节点);
GetData(头节点);
打印结果(头节点);
删除列表(头节点);
返回0;
}
headNode*初始化(headNode*rheadnode){
headNode*temp=(headNode*)malloc(sizeof(headNode));
temp->head=NULL;
返回温度;
}
int DeleteList(头节点*rheadnode){
listNode*p=rheadnode->head;
listNode*prev=NULL;
while(p!=NULL){
prev=p;
p=p->link;
免费(上);
}
自由(rheadnode);
返回0;
}
void GetData(headNode*rheadnode)
{
int数据类型;
int新数据;
FILE*fp=NULL;
fp=fopen(“input.txt”,“r”);
如果(fp!=NULL){
while(fscanf(fp、%d、&newData)!=EOF)
{
数据类型=插入节点(rheadnode,newData);
如果(新数据>5000)
数据超过5000个计数++;
交换机(数据类型)
{
案例0:
打破
案例1:
NodeCount++;
}
}
fclose(fp);
}
}
int InsertNode(headNode*rheadnode,int键){
listNode*搜索,*上一个;
listNode*node=(listNode*)malloc(sizeof(listNode));
节点->键=键;
搜索=rheadnode->head;
previous=NULL;
while(搜索!=NULL)
{
如果(节点->键<搜索->键)
{
先前=搜索;
搜索=搜索->链接;
}
else if(节点->键==搜索->键)
返回0;
其他的
打破
}
如果(上一个==NULL)
{
节点->链接=rheadnode->头部;
rheadnode->head=节点;
}
其他的
{
节点->链接=搜索;
上一个->链接=节点;
}
返回1;
}
作废打印结果(头节点*rheadnode){
/*
节点总数:10011
超过5000个值:45460
执行时间:1.234567秒
*/
printf(“节点总数:%d\n”,NodeCount);
printf(“超过5000个值:%d\n”,数据超过5000个计数);
printf(“执行时间:秒”);
}
关于:我想知道数据丢失发生在哪里

没有数据丢失。相反,存在一些重复的数据(键)值

当存在重复时,节点数不会增加