Connecting Tech Pros Worldwide Forums | Help | Site Map

need help on linked list

Newbie
 
Join Date: Oct 2006
Posts: 8
#1: Oct 22 '06
here is my code but generating errors:


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





struct doc_words
{
char words[30];
struct doc_words *next;
};

struct doc_words *head=NULL;;
struct doc_words *current;
//struct doc_words *head
struct doc_words* newNode;

//char *calloc();

struct doc_words *getmem()
{
struct doc_words *p;
//p = malloc(sizeof(struct doc_words));
p = (struct doc_words*)malloc(sizeof( doc_words));
if(!p)
{printf("out of memory");
return(0);
}
else
return(p);
}

struct doc_words *AddWords( char text[30])
{

newNode=getmem();
newNode->words[20]= text[30];
newNode->next=head;
head=newNode;
}

void extract_words_from_file()
{

char word[30];

FILE *ifp;

if ((ifp=fopen("C:\\collins_tirivamwe\\DSA\\text.txt" ,"r"))!=NULL)
{

while (fscanf(ifp,"%s",word)!=EOF)

{
printf("%s",word);
AddWords(word);
}
fclose(ifp);
}
else
printf("error");
}

void read()
{
printf("words in list");
struct doc_words *current= head;
while (current!=NULL)
{

printf("%s\n",current->words);
current=current->next;
}

}

int getCount()
{
int count=0;
struct doc_words *current= head;
while (current!=NULL)
{
count++;
current=current->next;

}

printf("\n %d",count);
return (count);
}

/*int getCount(char word)
{
int count=0;
struct doc_words *p = head;
while (p!=NULL)
{
if (strcmp(p->word,word)==0)
{
count++;
p=p->next;
}
else
{
p=p->next;
}
}
return(count);
}

*/



int main()
{
int p=0;
extract_words_from_file();

p=getCount();
// printf("\nnumber of words are %d",p);
read();
system("PAUSE");
return 0;
}

arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#2: Oct 22 '06

re: need help on linked list


Quote:

Originally Posted by tirivamwe

here is my code but generating errors:

You mean compile errors?

You may change the line

Expand|Select|Wrap|Line Numbers
  1.     p = (struct doc_words*)malloc(sizeof(doc_words));
  2.  
by
Expand|Select|Wrap|Line Numbers
  1.     p = (struct doc_words*)malloc(sizeof(struct doc_words));
  2.  
This is the only error my C-compiler finds. There some more warnings, however.

If you're talking about something else, please be a little more specific about the errors.
Newbie
 
Join Date: Oct 2006
Posts: 8
#3: Oct 22 '06

re: need help on linked list


when trying tp display what is stored in list that where it is giving wrong results:
it is printing the following:

my name is collins
4 words in list
♦=
8♦=
h♦=
ÿ♦=

Press any key to continue . . .
arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#4: Oct 22 '06

re: need help on linked list


Quote:

Originally Posted by tirivamwe

when trying tp display what is stored in list that where it is giving wrong results:
it is printing the following:

my name is collins
4 words in list
♦=
8♦=
h♦=
ÿ♦=

Press any key to continue . . .

Only a little mistake: you copy only one char, instead of the whole string in the line
Expand|Select|Wrap|Line Numbers
  1. newNode->words[30]= text[30];
  2.  
In addition, the destination as well as the target are beyond your buffer boundaries.

Try
Expand|Select|Wrap|Line Numbers
  1. strcpy( newNode->words, text );
  2.  
at that place ...
Newbie
 
Join Date: Oct 2006
Posts: 8
#5: Oct 23 '06

re: need help on linked list


hie

the code is now saving in the linked list but the problem now is that it is giving error when trying to compare the word from the list with the one supplied by the user:the line of code giving errors is:

Expand|Select|Wrap|Line Numbers
  1.   if (strcmp(p->word,word)==0)
it is giving error of :

passing `char' to argument 2 of `strcmp(const char *, const char *)' lacks a cast
arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#6: Oct 23 '06

re: need help on linked list


Quote:

Originally Posted by tirivamwe

hie

the code is now saving in the linked list but the problem now is that it is giving error when trying to compare the word from the list with the one supplied by the user:the line of code giving errors is:

Expand|Select|Wrap|Line Numbers
  1.   if (strcmp(p->word,word)==0)
it is giving error of :

passing `char' to argument 2 of `strcmp(const char *, const char *)' lacks a cast

Have a look at the (parameter) declaration of 'word'. It's a char, not a char*.
In addition: the member of p is called 'words', not 'word' ... :-)
Reply