Thread: where to put this?

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

    where to put this?

    So, in my code, a user names a resistor for example "1" and then also inserts its values for example 2. And then they can add as many as they want. However no two resistors can have the same name. So their can't be two resistors named 1. This is the part I am getting stuck at. I know what to write as I have written it in my code, however I don't know where to put it. Also, since an error message displays I do not want the program to display the second resistor 1 or to add its sum.
    I tried to write this as clearly as I can.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct node{
      int data;
      int id;
      struct node *next;
    } NODE;
    
    
    typedef NODE *NODEptr;
    
    
    void insert (NODEptr *sPtr,int , int );// prototype
    void delete (NODEptr *sPtr, int value);// prototype
    void printList (NODEptr currentPtr);
    void sumList (NODEptr currentPtr);
    
    
    int main(void)
    {
      NODEptr startPtr = NULL; /* initially there are no nodes */
      int item; /* character entered by user */
      int choice; /* user's choice */
      int id;
      printf("Enter your choice: 1 insert; 2 delete; 3 exit\n");
      printf("? ");
      scanf("%d", &choice);
    
    
      while (choice != 3) {
        if (choice == 1) {
          printf ("Enter an id followed by resistance to insert: ");
          scanf("\n%d %d",&id, &item);
          if(item <= 0){
            fprintf(stderr,"\nImpossible resistor value\n");}
          /*      if (startPtr!=NULL)
            {
              fprintf (stderr,"ID already exists\n");
              }*/
     else{
          insert (&startPtr,id,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", &id);
            delete (&startPtr, id);
            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 ("id%d value%d --> ",currentPtr->id, currentPtr -> data );
     currentPtr = currentPtr -> next;
        }
        printf("NULL\n\n");
      }
    }
    
    
    void insert (NODEptr *sPtr,int id, 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->id = id;
     newPtr -> data = value;
      newPtr -> next = NULL;
    
    
      previousPtr = NULL;
      currentPtr = *sPtr;
      /*loop to find the current location in the list */
      while (currentPtr != NULL && currentPtr->id != id ) {
        previousPtr = currentPtr;
        currentPtr = currentPtr->next;
      }
      /* if (currentPtr!=NULL)
            {
            printf ("ID already exists");
            }*/
      /* 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) -> id) {
        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 -> id != 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 -> data;
            printf("the sum is %d\n", sum);
            currentPtr = currentPtr -> next;
    }
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    210
    2 places in the code in the main and in insert function I have put this:
    Code:
     /*      if (startPtr!=NULL)
            {
              fprintf (stderr,"ID already exists\n");
              }*/
    


    but if I put it in the main it makes everything "ID already exists" and if I put it in the insert function although it displays the error it still prints it in the list and counts it in the sum

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by kiwi101 View Post
    if I put it in the insert function although it displays the error it still prints it in the list and counts it in the sum
    In insert() you add the duplicate node because you just print the error message and on the following lines you nevertheless insert it into the list. You should return from the function when you come across a duplicate id like you do in delete() when you can't find the id.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed