D
duaa shloul
Guest
i have to write a c code to read a billions of integer from textfile .. each integer is of 10 digit .. then i have to build a linkedlist to store these integer ...
... now my question is : If i have to search for a number in the linked list, how much time it takes to traverse the linkedlist ???
i have already tried this code :
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp = 0;
int count = 0;
int choice = 1;
first = 0;
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
int i = 0;
for (i = 0; i < 100000; i++)
{
fprintf(f, " %d ", i);
}
fclose(f);
FILE *file = fopen ("file.txt", "r");
fscanf (file, "%d", &head-> num);
while (!feof (file))
{ head = (NODE *)malloc(sizeof(NODE));
printf ("%d ", i);
fscanf (file, "%d", &head-> num);
if (first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
}
fclose (file);
temp->ptr = 0;
... now my question is : If i have to search for a number in the linked list, how much time it takes to traverse the linkedlist ???
i have already tried this code :
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp = 0;
int count = 0;
int choice = 1;
first = 0;
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
int i = 0;
for (i = 0; i < 100000; i++)
{
fprintf(f, " %d ", i);
}
fclose(f);
FILE *file = fopen ("file.txt", "r");
fscanf (file, "%d", &head-> num);
while (!feof (file))
{ head = (NODE *)malloc(sizeof(NODE));
printf ("%d ", i);
fscanf (file, "%d", &head-> num);
if (first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
}
fclose (file);
temp->ptr = 0;