Thread: Help with linked list function

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    10

    Help with linked list function

    The purpose of the function is to print the number of vowels in a linked list. The function compiles fine but will not print the correct number. I am not sure what is wrong. Any help would be great.

    Code:
    #include "mystuff.h"
    
    void vowels (NODE* pList)
    
    {
      char w;
      char a;
      char e;
      char i;
      char o;
      char u;
      char A;
      char E;
      char I;
      char O;
      char U;
    
      int sum = 0;
      NODE* pWalker;
      pWalker = pList;
      printf("c. The total number of vowels in the linked list is: ");
    
       while (pWalker)
       {
        w = (pWalker->data.key);
        if (w = a, e, i, o, u, A, E, I, O, U)
        sum++;
        pWalker = pWalker->link;
       }
       printf("%d", sum);
       printf("\n");
       return;
    }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    To compare two values (ONLY two values) you use ==. What you'll want to do for this is to create an array like
    Code:
    vowels[] = "aeiouAEIOU";
    then you can search the data.key using each char from the list.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void vowels (NODE* pList)
    
    {
      char w;
      char a;
      char e;
      char i;
      char o;
      char u;
      char A;
      char E;
      char I;
      char O;
      char U;
    These do not make variables holding any value. The line 'char U;' doesn't make a variable with the value of 'U'. It makes a variable named U.

    Also this:
    Code:
     if (w = a, e, i, o, u, A, E, I, O, U)
    Is not how you test for a variable. First off = assigns a value, while == tests for a value. Furthermore, you would have to test every value separately.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    Now the code works.

    Code:
    #include "mystuff.h"
    
    void vowels (NODE* pList)
    
    {
      int w = w;
      int a = 97;
      int e = 101;
      int i = 105;
      int o = 111;
      int u = 117;
      int A = 65;
      int E = 69;
      int I = 73;
      int O = 79;
      int U = 85;
    
      int sum = 0;
      NODE* pWalker;
      pWalker = pList;
    
      printf("c. The total number of vowels in the linked list is: ");
    
       while (pWalker)
       {
        w = (pWalker->data.key);
        if (w == a)
        sum++;
        if (w == e)
        sum++;
        if (w == i)
        sum++;
        if (w == o)
        sum++;
        if (w == u)
        sum++;
        if (w == A)
        sum++;
        if (w == E)
        sum++;
        if (w == I)
        sum++;
        if (w == O)
        sum++;
        if (w == U)
        sum++;
        pWalker = pWalker->link;
       }
       printf("%d\n", sum);
       return;
    Last edited by schmidtc; 07-12-2010 at 03:51 PM.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    how about
    Code:
      int w = 'w';
      int a = 'a';
      int e = 'e';
      int i = 'i';
      int o = 'o';
      int u = 'u';
      int A = 'A';
      int E = 'E';
      int I = 'I';
      int O = 'O';
      int U = 'U';
    Just a thought.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
       if (w == a)
        sum++;
        if (w == e)
        sum++;
        if (w == i)
        sum++;
        if (w == o)
        sum++;
        if (w == u)
        sum++;
        if (w == A)
        sum++;
        if (w == E)
        sum++;
        if (w == I)
        sum++;
        if (w == O)
        sum++;
        if (w == U)
        sum++;
    Why are you making variables for all of these?
    Code:
    if( w == 'o' )

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    thanks
    Last edited by schmidtc; 07-12-2010 at 05:16 PM.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I have the same idea as Kennedy.
    Code:
    #include <string.h>
    #include <ctype.h>      // tolower()...
    #define VOWELS        "aeiou"
    #define IS_VOWEL(ch)     ( strchr(VOWELS,tolower(ch) ) != NULL )
    /* better use function ;) */
    Last edited by Bayint Naung; 07-13-2010 at 05:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  5. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM