Thread: If fuction in Linked Lists

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    If fuction in Linked Lists

    hello this its me again. im having trouble using the if function in a linked list. so basically i wanted to count how many nodes contains a character as its element in a linked list. basically it has 5 nodes and all of them has a char element. this is what i had come up with and i am stuck because it seems that the statement for total is not being executed.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    void main()
    {
    int total=0;
    struct list{
    char value;
    struct list *next;
    }a,b,c,d,e;
    struct list *p;
    a.value='A';
    a.next=&b;
    b.value='B';
    b.next=&c;
    c.value='C';
    c.next=&d;
    d.value='D';
    d.next=&e;
    e.value='E';
    e.next=NULL;
    p=&a;
    
    struct list *rec_ptr;
    rec_ptr=p;
    printf("The elements are:\n");
    while(rec_ptr!=NULL){
    printf("\t%c\n",rec_ptr->value);
    rec_ptr=rec_ptr->next;}
    
    
    
    rec_ptr=p;
    while(rec_ptr!=NULL){
    if(isalpha(rec_ptr->value)==0){
    total=total+1;}
    rec_ptr=rec_ptr->next;}
    
    printf("\nTotal Nodes with Character Elements is %d",total);
    getch();
    }
    the output for total is 0, instead of 5. pls anyone can help me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read this -> SourceForge.net: Indentation - cpwiki
    Read this -> SourceForge.net: Void main - cpwiki
    Read this -> isalpha(3): char classification routines - Linux man page
    Code:
    #include<stdio.h>
    #include<ctype.h>
    int main()                      //!! fixed
    {
      int total = 0;
      struct list {
        char value;
        struct list *next;
      } a, b, c, d, e;
      struct list *p;
      a.value = 'A';
      a.next = &b;
      b.value = 'B';
      b.next = &c;
      c.value = 'C';
      c.next = &d;
      d.value = 'D';
      d.next = &e;
      e.value = 'E';
      e.next = NULL;
      p = &a;
    
      struct list *rec_ptr;
      rec_ptr = p;
      printf("The elements are:\n");
      while (rec_ptr != NULL) {
        printf("\t%c\n", rec_ptr->value);
        rec_ptr = rec_ptr->next;
      }
    
      rec_ptr = p;
      while (rec_ptr != NULL) {
        if (isalpha(rec_ptr->value) != 0) { //!! read the manual, what does isalpha() return?
          total = total + 1;
        }
        rec_ptr = rec_ptr->next;
      }
    
      printf("\nTotal Nodes with Character Elements is %d", total);
      return 0;                     //!! fixed
    }
    Some things (like strcmp) return 0 when things match.
    Other things (like isalpha) return 0 when things DO NOT match.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    thank you salem. sorry for the late reply. your links that you shared help a lot. im now on my final term in programming c. i was given a proect to make a hangman game in C. i have seen a few here on the site. if i encounter some problems ( im pretty sure i will! ) ill post it here and i hope you will help me again. you are very kind salem. thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM