Thread: help in my program on linked lists

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    4

    Smile help in my program on linked lists

    An encoded file will be decoded by the program using a key input from the user. This key is a sequence of numbers that will scramble the message in the file into its appropriate order.

    I have to use linked lists for this. I already started it, but I can't seem to find a correct way to jumble the words and display them correctly.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct _node_{
         char word[30];
         struct _node_ *pPtr_word;
         }node_t;
         
    int main(void)
    {
    FILE *pInput, *pOutput;
    int a, b, count, num, group_num, counter, data[10], i, j;
    node_t node[100]; 
    node_t *pPtr;
    
         pInput=fopen("input.txt","r");
         if(pInput==NULL){
         printf("Error: can't open file.\n");
         return 1;
         }
         else{
              if('\n')
              count=count++;
              for(a=0;a<count+1;a++)
              fgets(node[a].word,30,pInput);
              for(b=0;b<count+1;b++)
              printf("%s\n", node[b].word);
         }
    
         printf("Enter number of numbers for key sequence: ");
         scanf("%d", &num);
         printf("Enter key sequence: ");     
         for(counter=0;counter<num;counter++)
         scanf("%d", &data[counter]);
    
         group_num=((count+1)/num);
    
         for(i=0;i<group_num;i++)
         for(j=0,pPtr=node;j<num-1;j++,pPtr++)
         node[(data[j]-1)+num*i].pPtr_word=&node[((data[j]-1)+num*i)+1];
         pPtr->pPtr_word=NULL;
    
         for(pPtr->word;pPtr!=NULL;pPtr=pPtr->pPtr_word)
         printf("%s", pPtr->word);
    
    getchar();
    getchar();
    
         return 0; 
    }
    Example, the file input [from a text file] is-- brown quick The over jumped fox dog. lazy the
    Then the key sequence entered would be 321
    The output should be-- The quick brown fox jumped over the lazy dog.

    Also, for displaying my linked list, it seems like it doesn't traverse the whole thing.

    Please help me out
    Last edited by zerozerozero; 10-16-2008 at 07:23 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your indentation is pretty bad.

    Code:
             if('\n')
              count=count++;
    What do YOU think this does - I'm convinced what it ACTUALLY does isn't what you THINK it does, so I'd be interested in understanding what you meant by it.

    count = count++ is superfluous, and may well result in incorrect results (because count is set to count, then the original count is incremented (but probably lost).

    The part with if ('\n') is always true - because '\n' is non-zero - it is the same as "if (true)".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Quote Originally Posted by matsp View Post
    Your indentation is pretty bad.

    Code:
             if('\n')
              count=count++;
    What do YOU think this does - I'm convinced what it ACTUALLY does isn't what you THINK it does, so I'd be interested in understanding what you meant by it.

    count = count++ is superfluous, and may well result in incorrect results (because count is set to count, then the original count is incremented (but probably lost).

    The part with if ('\n') is always true - because '\n' is non-zero - it is the same as "if (true)".

    --
    Mats
    I'm trying to get the words in the file by line. I can't get it by word.
    In my text file, it looks like this:
    brown
    quick
    The
    over
    jumped
    fox
    dog.
    lazy
    the

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, let me understand this: You have written a bunch of code - you have no idea what any of it does, and you want one of us to fix it up for you?

    I have identified two lines that ABSOLUTELY doesn't do anything meaningfull - if you at least explain what you want those two lines to do, maybe we can help you get to that point.

    here is another two lines that are really bad:
    Code:
    int a, b, count, num, group_num, counter, data[10], i, j;
    node_t node[count];
    count is not initialized, so it may be absolutely anything: -1, 4711, 16231021, 5 or whatever other number between -2 billion and +2 billion - then you create an array of nodes based on that. How many nodes do you actually want to have?

    Also, on the other lines I pointed to, you are using count, which has still not been given a value - so it is still some completely random value - between negative and positive 2 billion.

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It seems to me like using a linked list for this is like using seat belts on an escalator -- not really impossible, but kind of silly.

    Does your prof also make you put your name on assignment covers twice (once on the right margin, and once in the center)?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    count is used to get the number of words which I might be able to use later on

    I changed that array of nodes to a limit of 100..

    the for loop is used to link them.. the array index is my initial "formula" for the correct sequence to be linked [but the formula is not perfect yet]

    the next for loop is used to display, but it doesn't display the whole linked list.. it displays only a word.

    I have been trying to do this for a week already, so I am asking help now because I am desperate. I'm not an expert programmer, so I still have lots of loopholes, hoping that someone could help out because I'm running out of time.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    count is used to get the number of words which I might be able to use later on

    I changed that array of nodes to a limit of 100..

    the for loop is used to link them.. the array index is my initial "formula" for the correct sequence to be linked [but the formula is not perfect yet]

    the next for loop is used to display, but it doesn't display the whole linked list.. it displays only a word.

    I have been trying to do this for a week already, so I am asking help now because I am desperate. I'm not an expert programmer, so I still have lots of loopholes, hoping that someone could help out because I'm running out of time.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am going to fix the indentation first so all of us can see your code clearly:

    ORIGINAL CODE!!
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct _node_{
      char word[30];
      struct _node_ *pPtr_word;
    }node_t;
         
    int main(void)
    {
      FILE *pInput, *pOutput;
      int a, b, count, num, group_num, counter, data[10], i, j;
      node_t node[100] = {0}; 
      node_t *pPtr;
    
      pInput=fopen("input.txt","r");
    
      if(pInput==NULL){
        printf("Error: can't open file.\n");
        return 1;
      }
      else{
        if('\n')
          count=count++;
    
        for(a=0;a<count+1;a++)
          fgets(node[a].word,30,pInput);
    
        for(b=0;b<count+1;b++)
          printf("&#37;s\n", node[b].word);
      }
    
      printf("Enter number of numbers for key sequence: ");
      scanf("%d", &num);
      printf("Enter key sequence: ");     
    
      for(counter=0;counter<num;counter++)
        scanf("%d", &data[counter]);
    
      group_num=((count+1)/num);
    
      for(i=0;i<group_num;i++)
         for(j=0,pPtr=node;j<num-1;j++,pPtr++)
           node[(data[j]-1)+num*i].pPtr_word=&node[((data[j]-1)+num*i)+1];
    
      pPtr->pPtr_word=NULL;
    
      for(pPtr->word;pPtr!=NULL;pPtr=pPtr->pPtr_word)
        printf("%s", pPtr->word);
    
      getchar();
      getchar();
    
      return 0; 
    }

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't know dude... the way this parses is VERY screwy. It calls an entire line a word. Which may be how your input file is, but who would type like that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Replies: 2
    Last Post: 01-18-2003, 01:32 AM
  4. Replies: 1
    Last Post: 03-21-2002, 06:10 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM