Hello !
I have a text file of strings like;
kumar
sinha
sinha
kapoor
gupta
I want to read this into a linked list and want to remove the duplicates.
My problem is I dont know how to read it in Linked list.
Please suggest something here!
Thanks.
Hello !
I have a text file of strings like;
kumar
sinha
sinha
kapoor
gupta
I want to read this into a linked list and want to remove the duplicates.
My problem is I dont know how to read it in Linked list.
Please suggest something here!
Thanks.
Do you know how to implement a linked list?
With say methods such as
- add
- find
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Yes , I know how to implement Linked list for the finding and removing the duplicates for integers but have problems with strings!
So use strcmp instead of == to compare strings instead of numbers. Everything else is the same.
Quzah.
Hope is the first step on the road to disappointment.
Even that fine, but the primary issue is with importing this file in linked list directly.
I mean I can read it an array but i wanna do it with Linked list .
So read it into an array, and copy that into your linked list node. Then go on to the next record. I thought you said you already knew how to do a linked list with numbers? Ok, so, store your record instead of a number. What's the problem?
Quzah.
Hope is the first step on the road to disappointment.
my code looks like
Code:main () { FILE *fp; fp = fopen ("Names.txt","r"); int i, names =0; char c, *pN; while((c=getc(fp))!=EOF) { if(c=='\n') names++ } printf("\tThe total number of members are : %d\n",names); pN = (char*) calloc(names, sizeof(char)); rewind (fp1); for (i=0;i<names;i++) { fscanf(fp,"%s", &pN[i]); printf("%s", pN[i]); }
I am able to the file , but no clue how to change it to Linked list.
Like I said, what part don't you know how to do? Do you know how to make a linked list? You said you did. So, write some code that makes a linked list, then worry about filling it.
Quzah.
Hope is the first step on the road to disappointment.
ok, I tried but doesnt work....
Code:#include<stdlib.h> #include<stdio.h> struct node { char data; struct node *next; }; typedef struct node *item; main () { FILE *fp; fp = fopen ("Names.txt","r"); int i, names =0; char c, *pN; while((c=getc(fp))!=EOF) { if(c=='\n') names++ ; } printf("\tThe total number of members are : %d\n",names); pN = (char*) calloc(names, sizeof(char)); rewind (fp); for (i=0;i<names;i++) { fscanf(fp,"%c", &pN[i]); printf("%c", pN[i]); } item curr, head = NULL, tail = NULL; head = NULL; for(i=1;i<=names;i++) { curr = (item *)malloc(sizeof(item)); curr->data = pN[i]; curr->next = tail; tail = curr; if (!head) head = curr; } curr = head; while(curr) { printf("%d\n", curr->data); curr = curr->next ; } }
Also, the strings are captured as Characters,
i mean the file is printed as
S
i
n
h
a
k
a
p
o
o
r
n Am I copying the array in Linked list correctly.
thanks.
Are you just trying to store one character in a node, or a word? If you want a word, you want an array of character in your structure, not just a single char. And, if that's the case, then you can use something like fgets instead of fgetc. Or you can change up fscanf to read a word instead of a single character.
Quzah.
Hope is the first step on the road to disappointment.
I had tried that too..
but then i get a warning like
warning: this program uses gets(), which is unsafe.
and then there is no output.
Yes, I want to store the word.
n When u say, an array of characters in structure...
then would be something like
Code:struct node { char data[10] ; struct node *next; };
Notice that quzah mentioned (and even highlighted) fgets, not gets.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ohh !
yes, I did that.
I am able to read the whole string but in the following way...
But I still am not able to get it into a linked list.Code:FILE *fp; char line[LINE_LENGTH]; int count=0,i; char names[10]; fp = fopen ("Names.txt","r"); while(feof(fp)==0) { fgets(names,10,fp) ; count++; printf("%s",names); } printf("File contains %d lines.\n", count-1); fclose(fp);
though i tried it something like this
have no idea , if it is correct because there seems some issue with "Push".Code:head = malloc(sizeof(struct node)); for(i=1;i<=count;i++) { Push(&head,names[i]); } printf("%s", head);
can u plz help me with some code to make it linked list.
thanks.
i wrote a function to remove the duplicates but i get the Segmentation error
While my main function looks like:Code:removeDuplicates(struct node* head) { struct node* current = head; struct node* next_next; while(current->next != NULL) if (strcmp (current->data, current->next->data) ==0) { next_next = current->next->next; free(current->next); current->next = next_next; } { current = current->next; } } }
Code:int main () { FILE *fp; int i; char line[LINE_LENGTH]; int count = 0; char names[10]; fp = fopen ("Workbook2.txt","r"); while(feof(fp)==0) { gets(names,10,fp) ; count++; //printf("%s",names); } printf("%d\n", count-1); struct node* head = NULL; for(i = 0; i <count;i++) { push(&head,names[i]); } removeDuplicates(head); fclose(fp); }
Am i doing something really stupid here!??
Question 12.2
You don't need array to store them temporarily. Isn't the point of linked list!
Pseudo Code.
Code:while( Not EOF ) { add( list , data ); } removeDuplicate(list); printList(list);
Last edited by Bayint Naung; 07-28-2010 at 07:14 AM.