Thread: sum function problem

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    210

    sum function problem

    My code compiles perfectly when I want to insert, display and delete using linked lists. However I have added a function "sum" which is supposed to add up the integer values stored in the linked lists but it is not working. Please tell me what I am doing wrong. I have commented out the sum function but you can see it. Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct node{
      int data;
      struct node *next;
    } NODE;
    
    
    typedef NODE *NODEptr;
    
    
    void insert (NODEptr *sPtr, int value);// prototype
    void delete (NODEptr *sPtr, int value);// prototype
    void printList (NODEptr currentPtr);
    //void sumList (NODEptr sPtr);
    
    
    int main(void)
    {
      NODEptr startPtr = NULL; /* initially there are no nodes */
      int item; /* character entered by user */
      int choice; /* user's choice */
    
    
      printf("Enter your choice: 1 insert; 2 delete; 3 exit\n");
      printf("? ");
      scanf("%d", &choice);
    
    
      while (choice != 3) {
        if (choice == 1) {
          printf ("Enter a number to insert: ");
          scanf("\n%d", &item);
          if(item <= 0){
            fprintf(stderr,"\nImpossible resistor value\n");}
            else{
          insert (&startPtr, item);
          printList (startPtr);
          //    sum(startPtr);
        }
        }
     else if (choice == 2) {
          if (startPtr == NULL) {
            printf("List is empty.\n\n");
          }
          else {
            //printList (startPtr);
            printf("Enter a character to delete: ");
            scanf("\n%d", &item);
            delete (&startPtr, item);
            printList (startPtr);
          }
        }
        else {
          printf("Invalid Choice.\n\n");
        }
        printf("Enter your choice: 1 insert; 2 delete; 3 exit\n");
        printf("? ");
        scanf("%d", &choice);
      }
      return 0;
    }
    void printList (NODEptr currentPtr)
    {
      /* if list is empty */
      if (currentPtr == NULL) {
        printf("List is empty. \n\n");
      }
      else {
        printf("The list is: \n");
    
    
        while (currentPtr != NULL) {
          printf ("%d --> ", currentPtr -> data );
          currentPtr = currentPtr -> next;
        }
        printf("NULL\n\n");
      }
    }
    void insert (NODEptr *sPtr, int value)
    {
      NODEptr newPtr; /* pointer to new node */
      NODEptr previousPtr; /* pointer to previous node in list */
      NODEptr currentPtr; /* pointer to current node in list */
    
    
      newPtr = (NODEptr) malloc (sizeof(NODE)); /* create a new node */
      if (newPtr == NULL) {
        fprintf(stderr, "No memory available. Insertion failed. \n");
        return;
      }
    
    
      newPtr -> data = value;
      newPtr -> next = NULL;
    
    
      previousPtr = NULL;
      currentPtr = *sPtr;
      /*loop to find the current location in the list */
     while (currentPtr != NULL && currentPtr->data < value ) {
        previousPtr = currentPtr;
        currentPtr = currentPtr->next;
      }
    
    
      /* insert new node at beginning of list */
      if (previousPtr == NULL) {
        newPtr->next = *sPtr;
        *sPtr = newPtr;
      }
      else { /*insert new node between previousPtr and currentPtr */
        previousPtr->next = newPtr;
        newPtr->next = currentPtr;
      }
    
    
    }
    void delete (NODEptr *sPtr, int value)
    {
      NODEptr tempPtr; /* temporary node pointer */
      NODEptr previousPtr; /* pointer to previous node in list */
      NODEptr currentPtr; /* pointer to current node in list */
    
    
      /* delete the first node */
      if (value == (*sPtr) -> data) {
        tempPtr = *sPtr;
        *sPtr = (*sPtr) -> next;  /*delete the node */
        free  (tempPtr); /* free the delete node */
      }
      else {
        previousPtr = *sPtr;
        currentPtr = (*sPtr) ->next;
    
    
        /*loop to find the correct location in the list */
        while (currentPtr != NULL && currentPtr -> data != value ) {
          previousPtr = currentPtr;
          currentPtr = currentPtr -> next;
        }
        if (currentPtr == NULL) {
     printf( "%d not found.\n", value);
          return;
        }
    
    
        /*delete node at currentPtr */
        tempPtr = currentPtr;
        previousPtr -> next = currentPtr -> next;
        free  (tempPtr);
      }
    }
    /*void sumList (NODEptr currentPtr)
    {
      int sum =0;
      if (currentPtr == NULL) {
        sum = sum + currentPtr;
        currentPtr = currentPtr ->next;
      }
      printf("the sum is %d\n", sum);
    }
    */

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Do you mean to put sum=sum+currentPtr->data?

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    sum = sum + currentPtr;
    So you add an integer and a pointer to a struct... Maybe you want to add an int to an int, like this
    Code:
    sum = sum + currentPtr->data;
    where you could write it more elegant like this
    Code:
    sum += currentPtr->data;
    Also here is a reason that may discourage you from typedef'ing the pointer to struct .

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Second thought :
    Code:
    int sum =0;
      if (currentPtr == NULL) {
        sum = sum + currentPtr;
        currentPtr = currentPtr ->next;
      }
    So now, even though you modify your code as mentioned above ( camel-man also stated but we did it the same time, sorry camelMan ), you will add to the sum the data only if the current node is NULL.
    This is bad why :
    • This is not what you want to do.
    • When the pointer actually points to NULL (empty list, last node .. ) what will happen? You will request for member named data of NULL and for member named next of NULL. But NULL is not a struct , so this will emerge an error.


    How to fix it :

    You want to traverse trough your list, again and again until you reach the end.
    In other words you want to loop through your nodes, but you do not know how many nodes you have and you also do not want to request for a member named data of something that it is NULL, so you have to use a while loop structure.
    You want the loop to be executed as long as the pointer is not NULL, which in code means
    Code:
    while(currentPtr != NULL)
    {
           /* sum */
    }
    Hope this helps

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    210
    So I did what you said but now I am getting an infinite loop whether I do ->next or ->data
    even though I believe it should be ->next in the sum function.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct node{
      int data;
      struct node *next;
    } NODE;
    
    
    typedef NODE *NODEptr;
    
    
    void insert (NODEptr *sPtr, int value);// prototype
    void delete (NODEptr *sPtr, int value);// prototype
    void printList (NODEptr currentPtr);
    void sumList (NODEptr sPtr);
    
    
    int main(void)
    {
      NODEptr startPtr = NULL; /* initially there are no nodes */
      int item; /* character entered by user */
      int choice; /* user's choice */
    
    
      printf("Enter your choice: 1 insert; 2 delete; 3 exit\n");
      printf("? ");
      scanf("%d", &choice);
    
    
      while (choice != 3) {
        if (choice == 1) {
          printf ("Enter a number to insert: ");
          scanf("\n%d", &item);
          if(item <= 0){
            fprintf(stderr,"\nImpossible resistor value\n");}
            else{
          insert (&startPtr, item);
          printList (startPtr);
          sumList(startPtr);
        }
        }
        else if (choice == 2) {
     if (startPtr == NULL) {
            printf("List is empty.\n\n");
          }
          else {
            //printList (startPtr);
            printf("Enter a character to delete: ");
            scanf("\n%d", &item);
            delete (&startPtr, item);
            printList (startPtr);
          }
        }
        else {
          printf("Invalid Choice.\n\n");
        }
        printf("Enter your choice: 1 insert; 2 delete; 3 exit\n");
        printf("? ");
        scanf("%d", &choice);
      }
      return 0;
    }
    void printList (NODEptr currentPtr)
    {
      /* if list is empty */
      if (currentPtr == NULL) {
        printf("List is empty. \n\n");
      }
      else {
        printf("The list is: \n");
    
    
        while (currentPtr != NULL) {
          printf ("%d --> ", currentPtr -> data );
          currentPtr = currentPtr -> next;
        }
        printf("NULL\n\n");
      }
    }
    void insert (NODEptr *sPtr, int value)
    {
      NODEptr newPtr; /* pointer to new node */
      NODEptr previousPtr; /* pointer to previous node in list */
      NODEptr currentPtr; /* pointer to current node in list */
    
    
      newPtr = (NODEptr) malloc (sizeof(NODE)); /* create a new node */
      if (newPtr == NULL) {
        fprintf(stderr, "No memory available. Insertion failed. \n");
        return;
      }
    
    
      newPtr -> data = value;
      newPtr -> next = NULL;
    
    
      previousPtr = NULL;
      currentPtr = *sPtr;
      /*loop to find the current location in the list */
     while (currentPtr != NULL && currentPtr->data < value ) {
        previousPtr = currentPtr;
        currentPtr = currentPtr->next;
      }
    
    
      /* insert new node at beginning of list */
      if (previousPtr == NULL) {
        newPtr->next = *sPtr;
        *sPtr = newPtr;
      }
      else { /*insert new node between previousPtr and currentPtr */
        previousPtr->next = newPtr;
        newPtr->next = currentPtr;
      }
    
    
    }
    void delete (NODEptr *sPtr, int value)
    {
      NODEptr tempPtr; /* temporary node pointer */
      NODEptr previousPtr; /* pointer to previous node in list */
      NODEptr currentPtr; /* pointer to current node in list */
    
    
      /* delete the first node */
      if (value == (*sPtr) -> data) {
        tempPtr = *sPtr;
        *sPtr = (*sPtr) -> next;  /*delete the node */
        free  (tempPtr); /* free the delete node */
      }
      else {
        previousPtr = *sPtr;
        currentPtr = (*sPtr) ->next;
    
    
        /*loop to find the correct location in the list */
        while (currentPtr != NULL && currentPtr -> data != value ) {
          previousPtr = currentPtr;
          currentPtr = currentPtr -> next;
        }
     if (currentPtr == NULL) {
          printf( "%d not found.\n", value);
          return;
        }
    
    
        /*delete node at currentPtr */
        tempPtr = currentPtr;
        previousPtr -> next = currentPtr -> next;
        free  (tempPtr);
      }
    }
    void sumList (NODEptr currentPtr)
    {
      int sum =0;
      while(currentPtr != NULL)
        {
       sum += currentPtr -> next;
       printf("the sum is %d\n", sum);
    }
    }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Try moving currentPtr to the next node.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    210
    I actually did not want to be type defi'ing (if thats how you say it). But some of this code was given to me and it is mandatory to use it

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    sum += currentPtr -> next;
    You still have not changed currentPtr->next to currentPtr->data.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    210
    what do you mean?
    current pointer is already pointing to the next node?

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    210
    I tried that first but I also got an infinite loop so I decided to try it with ->next.
    but could you tell me why it should be ->data?

  11. #11
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    I would think that your compiler would complain about the function name delete since I thought that was a keyword but perhaps not.

    It should be data because you are adding up integers you say, where in your structure are the integers? Data, right? You are trying to add up inadequate data types when you say sum+=currentPtr->next....

    That is like saying integer + pointer to struct... It does not make sense.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    210
    Omgsh you're so right I don't know why i thought it should be ->next

    I know why I am getting an infinite loop it should be while(currentPtr == NULL)
    but the sum keeps on equaling to 0 no matter what.

  13. #13
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    If currentPtr == NULL then there will be no data to add up.

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    210
    I get an infinte loop if currentPtr!=NULL

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    210
    with the sum equal to1397184

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  3. function inside function (most likely) problem
    By vlaskiz in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2011, 05:10 AM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM