Thread: Linked List Issues

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    126

    Linked List Issues

    Really struggling with this project, when I go to use my display function it errors out, I know I'm missing something as my understanding is not all that high.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct N{
    char shipName[25];
    char shipType;
    int hostage;
    int shipValue;
    int ransom;
    struct N* next;
    }*LOG;
    
    void add(char ship[25], char type, int hostage, int shipValue, int ransom);
    void display(); 
    int length();
    
    int main()
    {
    
    LOG=NULL;
    
    int choice, hostage, ransom, shipValue;
    
    char ship[25], type;
    
    
        while(1){
    
    printf("Main Menu\n");
    printf("1. See A Captured Ship's Information\n");
    printf("2. Add a Ship\n");
    printf("3. Remove a Ship\n");
    printf("4. Generate Report (textfile)\n");
    printf("5. Exit\n");
    scanf("%d", &choice);
     
     
     switch(choice)  
        {  
        case 1:
                printf(" \nThere Are %d ships in the list\n",length()); 
                display();
                
        break;
        case 2:
        printf("Shipname:\n");
        scanf("%s", &ship);
        printf("Ship Type(T = Tanker, S = Schooner, Y= Yacht):\n");
        scanf("%s", &type);
        printf("Hostage:\n");
        scanf("%d", &hostage);
        printf("Ship Value:\n");
        scanf("%d", &ransom);
        printf("RANSOM:\n");
        scanf("%d", &shipValue);
      
                add(ship, type, hostage, shipValue, ransom); 
        break;
        case 3:
        break;
        case 4:
        break;
        case 5:
            exit(0);
        }
    }
    }
    
    void add(char ship[], char type, int hostage, int shipValue, int ransom)  
    {  
      struct N *temp1, *temp2;  
      
      temp1=(struct N *)malloc(sizeof(struct N)); 
      temp1->shipName[25]=ship[25];
      temp1->shipType=type;
      temp1->hostage=hostage;
      temp1->shipValue=shipValue;
      temp1->ransom=ransom;
     
      temp2=LOG;  
      
      if(LOG == NULL)  
      {  
         //First Node.  
         LOG=temp1;  
         LOG->next=NULL;  
      }  
      else  
      {  
         // End of the list.  
         while(temp2->next != NULL)  
         temp2=temp2->next;  
      
         // End of the list.  
         temp1->next=NULL;  
         temp2->next=temp1;  
      }  
    }  
    
    int length()  
    {  
      struct N *ptr;  
      int count=0;  
      
      ptr=LOG;  
      
      while(ptr != NULL)  
      {  
         ptr=ptr->next;  
         count++;  
      }  
      return(count);  
    }  
    
    void display()  
    {  
      struct N *ptr;  
      ptr=LOG;  
      
      if(ptr==NULL)  
      {  
         printf("\nNo Ship Logs");  
      }  
      else  
      {  
    
          while(ptr != NULL)  
          {  
                printf("Ship Name: %s\n", ptr->shipName);
                printf("Type: %s\n", ptr->shipType);  
                printf("Number of Hostages: %d\n", ptr->hostage);  
                printf("Ship Value: %d\n", ptr->shipValue);  
                printf("Ransom Demanded: %d\n", ptr->ransom);  
              ptr=ptr->next;  
          }  
          printf("\n");  
      }  
    }
    Last edited by Sorinx; 12-10-2012 at 01:02 AM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
       
       scanf("%s", &ship);
    Should be
        scanf("%s", ship);
    It's already an address to something
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Fix your indentation!

    Code:
    char shipType;
    ...
    char ship[25], type;
    "type" and "shipType" are chars but you treat them in your printf()- and scanf()-calls as strings.

    Code:
    printf("Ship Value:\n");
    scanf("%d", &ransom);
    printf("RANSOM:\n");
    scanf("%d", &shipValue);
    You're sure about these lines?

    Code:
    temp1->shipName[25]=ship[25];
    ???

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by Click_here View Post
    Code:
       
       scanf("%s", &ship);
    Should be
        scanf("%s", ship);
    It's already an address to something
    No it's not.... ship is an int main function variable...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "ship" is a character array variable in "main()" -> "ship[25]"

    So when you use "ship" in this fashion, it acts as a pointer to the first element of that array.

    So Click_here is correct.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The scanfs from lines 50 to 56 are using the wrong format specifier, and the one on line 48 is slightly wrong for the reason already given.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by iMalc View Post
    The scanfs from lines 50 to 56 are using the wrong format specifier, and the one on line 48 is slightly wrong for the reason already given.
    well when I use %c on line 50 it skips the input altogether, how are the others using the wrong specifier?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Sorinx View Post
    well when I use %c on line 50 it skips the input altogether...
    FAQ-Draft: Why scanf("%c", &c) doesn't work as expected

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Yeah I knew it had to do with newline I wasn't aware that it was as simple as that to fix, lol, now everything works but the shipName variable

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Sorinx View Post
    Yeah I knew it had to do with newline I wasn't aware that it was as simple as that to fix, lol, now everything works but the shipName variable
    That was already brought up in this thread, in case you missed it.

    Quote Originally Posted by AndiPersti View Post
    You're sure about these lines?

    Code:
    temp1->shipName[25]=ship[25];
    ???
    strcpy - C++ Reference

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by Matticus View Post
    That was already brought up in this thread, in case you missed it.



    strcpy - C++ Reference
    Ok but how would I do that with a pointer?
    Code:
    strcpy(temp1->shipName, ship);
    doesn't seem to work.
    Last edited by Sorinx; 12-10-2012 at 01:21 PM.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    This is where I'm at right now, output for shipName isn't working correctly
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct N{
    int j;
    char shipName[25];
    char shipType;
    int hostage;
    int shipValue;
    int ransom;
    struct N* next;
    }*LOG;
    
    void add(char ship[], char type, int hostage, int shipValue, int ransom);
    void display(); 
    int length();
    
    int main()
    {
    
    LOG=NULL;
    
    int choice, hostage, ransom, shipValue, loc;
    
    char ship[25], type;
    
    
        while(1){
    
    printf("Main Menu\n");
    printf("1. See A Captured Ship's Information\n");
    printf("2. Add a Ship\n");
    printf("3. Remove a Ship\n");
    printf("4. Generate Report (textfile)\n");
    printf("5. Exit\n");
    scanf("%d", &choice);
     
     
     switch(choice)  
        {  
        case 1:
                printf(" \nThere Are %d ships in the list\n",length()); 
                display();
                
        break;
        case 2:{
        printf("Shipname:\n");
        scanf("%s", ship);
        printf("Ship Type(T = Tanker, S = Schooner, Y= Yacht):\n");
        scanf("%s", &type);
        printf("Hostage:\n");
        scanf("%d", &hostage);
        printf("Ship Value:\n");
        scanf("%d", &ransom);
        printf("RANSOM:\n");
        scanf("%d", &shipValue);
                add(ship, type, hostage, shipValue, ransom); }
        break;
        case 3: {
              printf(" \nWhich Entry Do You Wish To Delete ");  
              scanf("%d",&loc);  
              delLoc(loc);  
              display();  }
        break;
        case 4:{
        FILE *fp;
        fp=fopen("log.txt", "w");
        struct N *this;
    
        if(LOG == NULL)
    
        {
            printf("This is Empty\n");
           break;
        }
         for ( this = LOG ; this != NULL ; this = this->next ) {
        fwrite(this, sizeof(*this), 1, fp);
    }
        
        }
        
        break;
        case 5:
            exit(0);
        }
    }
    }
    
    void add(char ship[], char type, int hostage, int shipValue, int ransom)  
    {  
      struct N *temp1, *temp2;  
      static int j=0;
      
      temp1=(struct N *)malloc(sizeof(struct N)); 
      temp1->j=j++;
      strcpy(temp1->shipName, ship);
      temp1->shipType=type;
      temp1->hostage=hostage;
      temp1->shipValue=shipValue;
      temp1->ransom=ransom;
     
      temp2=LOG;  
      
      if(LOG == NULL)  
      {  
         //First Node.  
         LOG=temp1;  
         LOG->next=NULL;  
      }  
      else  
      {  
         // End of the list.  
         while(temp2->next != NULL)  
         temp2=temp2->next;  
      
         // End of the list.  
         temp1->next=NULL;  
         temp2->next=temp1;  
      }  
    }  
    
    int length()  
    {  
      struct N *ptr;  
      int count=0;  
      
      ptr=LOG;  
      
      while(ptr != NULL)  
      {  
         ptr=ptr->next;  
         count++;  
      }  
      return(count);  
    }  
    
    void display()  
    {  
    
      struct N *ptr;  
      ptr=LOG;  
      
      if(ptr==NULL)  
      {  
         printf("\nNo Ship Logs");  
      }  
      else  
      {  
    
          while(ptr != NULL)  
          {  
                printf("Entry Number %d\n", ptr->j);
                printf("Ship Name: %s\n", ptr->shipName);
                printf("Type:%c\n", ptr->shipType);  
                printf("Number of Hostages: %d\n", ptr->hostage);  
                printf("Ship Value: %d\n", ptr->shipValue);  
                printf("Ransom Demanded: %d\n", ptr->ransom);  
                
              ptr=ptr->next;  
          }  
          printf("\n");  
      }  
    }  
    
    int delLoc(int loc)  
    {  
      struct N *prev_ptr, *ptr;  
      int i;  
      
      ptr=LOG;  
      
      if(loc > (length()) || loc <= 0)  
      {  
          printf("\nDeletion of LOG is not possible\n ");  
      }  
      else  
      {  
          // If the location is starting of the list  
          if (loc == 1)  
          {  
              LOG=ptr->next;  
              free(ptr);  
              return 0;  
          }  
          else  
          {  
              for(i=1;i<loc;i++)  
              {  
                  prev_ptr=ptr;  
                  ptr=ptr->next;  
              }  
      
              prev_ptr->next=ptr->next;  
              free(ptr);  
          }  
      }  
      return 1;  
    }
    Last edited by Sorinx; 12-10-2012 at 01:34 PM.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you sprinkled some "printf()" statements in your "case 2" block, you might find that the string is being read correctly initially, but is then being cleared somehow.

    This might lead to you the following line:

    Code:
    scanf("%s", &type);
    "type" is a character (%c), not a character array (%s).

    You'd also have some newline issues when this is corrected (see post #8 again).

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Oh man I didn't even notice that little oversight, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM