help me on linked list

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

help me on linked list

 
0
  #1
Oct 24th, 2006
the function first search a word from the linked list. if found it then increment the numWords (number of words in listfor that word) and if not found it shoud create a new node for the word.

the code is:

  1. void AddWords( char text[30])
  2. {
  3.  
  4. check=head;
  5.  
  6. while (check!=NULL)
  7. {
  8. if (strcmp(check->words,text[30])==0)
  9. {
  10. check->numWords++;
  11. break;
  12. }
  13. else
  14. {
  15. newNode=getmem();
  16. strncpy(newNode->words,text, 29);
  17. newNode->next=head;
  18. head=newNode;
  19. }
  20. check=check->next;
  21.  
  22. }


i am having this compiler error:

Warning] passing arg 2 of `strcmp' makes pointer from integer without a cast

for this line of code:

  1. if (strcmp(check->words,text[30])==0)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: help me on linked list

 
0
  #2
Oct 24th, 2006
This is valid
if (strcmp(check->words,text)==0)
and this is not
if (strcmp(check->words,text[30])==0)
Last edited by andor; Oct 24th, 2006 at 9:05 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

Re: help me on linked list

 
0
  #3
Oct 24th, 2006
i tried that but when debugging i am having this error

An Access violation(segment fault) raised in your program.

the line of code crushinhg the program is:

if (strcmp(check->words,temp)==0)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: help me on linked list

 
0
  #4
Oct 24th, 2006
I asume that temp is typo error becouse its text not temp. Is check->words valid? When you debuging what are the values for check->words and for text.
Last edited by andor; Oct 24th, 2006 at 9:50 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

Re: help me on linked list

 
0
  #5
Oct 24th, 2006
temp has value of : "is"

check->value has avue of: Could not watch this variable
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

Re: help me on linked list

 
0
  #6
Oct 24th, 2006
here is the whole program:
line in bold when debugged is giving the value of currunt->words as Could not watch this variable

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

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

struct doc_words *head=NULL;;
struct doc_words *current;
struct doc_words *check;
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(struct doc_words));
if(!p)
{printf("out of memory");
return(0);
}
else
return(p);
}

void AddWords( char text[30])
{
 
current=head;
 char temp[30];
 strncpy(temp,text,29);
      do              
      {
       if (current->words==temp)
          {
            current->numWords++;
            break;
          }
       check=check->next;     
      }while (check!=NULL);
       else
           {                                
             newNode=getmem();
             strncpy(newNode->words,text, 29);
             newNode->next=head;
             head=newNode;
             break;                                
           }
      
}

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[30])
{
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;

}
Last edited by ~s.o.s~; Oct 24th, 2006 at 2:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: help me on linked list

 
0
  #7
Oct 24th, 2006
Originally Posted by tirivamwe View Post
here is the whole program:
line in bold when debugged is giving the value of currunt->words as Could not watch this variable
Wont compile. Fix the errors and read this
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: help me on linked list

 
0
  #8
Oct 24th, 2006
When I post something to this thread its not there. Whats happening here.
FOR tiriamwe: Ok fix the code becouse it wont compile. Read this. Posting errors of your prog
  1. test16.cpp: In function `void AddWords(char*)':
  2. test16.cpp:54: parse error before `else'
  3. test16.cpp:60: break statement not within loop or switch
  4. test16.cpp: At global scope:
  5. test16.cpp:62: parse error before `}' token
  6.  
EDIT: Its ok now but there is some kind a bug when posting, becouse if I reply to 6th post of this thread my post is missing.
Last edited by andor; Oct 24th, 2006 at 11:15 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,899
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: help me on linked list

 
0
  #9
Oct 24th, 2006
  1. do
  2. {
  3. if (current->words==temp)
  4. {
  5. current->numWords++;
  6. break;
  7. }
  8. check=check->next;
  9. }while (check!=NULL);
  10. else
  11. {
You've nested your loops wrong:

do a
if b
end a
else b
end b

gr Niek
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC