Bubble Sort from Input File

This is a discussion on Bubble Sort from Input File within the C Programming forums, part of the General Programming Boards category; Im having some trouble implementing a input file to a tree code (NOT bubble sort, I wasnt paying attention and ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Bubble Sort from Input File

    Im having some trouble implementing a input file to a tree code (NOT bubble sort, I wasnt paying attention and messed up the title). The input file is suppose to be named p2_input.txt and will look like the input in the picture in the link. The input will always end in 0, and the output should be sorted alphabetically with the position number before it.
    http://img687.imageshack.us/img687/8157/ccode.jpg

    Basically, I need help implementing the input file and adding the position number beforehand. Here is my code that sorts alphabetically and prints in order, thanks for any help:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct tnode {
     char data;
     struct tnode *left;
     struct tnode *right;
    };
    
    struct tnode *tnode_insert(struct tnode *p, char value);
    void print_inorder(struct tnode *p);
    
    
    int main(void) {
     char demo_nr[] = "acbdejzyxl";
     struct tnode *root = NULL;
     struct tnode *searchval = NULL;
     int querry = 0;
     int i = 0;
    
     for(i = 0; i < 10; i++)
      root = tnode_insert(root, demo_nr[i]);
             print_inorder(root);
             printf("\n");
     return 0;
    }
    
    /* insert a tnode into the binary tree */
    struct tnode *tnode_insert(struct tnode *p, char value) {
     struct tnode *tmp_one = NULL;
     struct tnode *tmp_two = NULL;
    
     if(p == NULL) {
      /* insert [new] tnode as root node */
      p = (struct tnode *)malloc(sizeof(struct tnode));
      p->data = value;
      p->left = p->right = NULL;
     } else {
      tmp_one = p;
      /* Traverse the tree to get a pointer to the specific tnode */
      /* The child of this tnode will be the [new] tnode */
     while(tmp_one != NULL) {
       tmp_two = tmp_one;
       if(tmp_one ->data > value)
        tmp_one = tmp_one->left;
       else
        tmp_one = tmp_one->right;
      }
    
      if(tmp_two->data > value) {
       /* insert [new] tnode as left child */
       tmp_two->left = (struct tnode *)malloc(sizeof(struct tnode));
       tmp_two = tmp_two->left;
       tmp_two->data = value;
       tmp_two->left = tmp_two->right = NULL;
      } else {
       /* insert [new] tnode as left child */
       tmp_two->right = (struct tnode *)malloc(sizeof(struct tnode));
       tmp_two = tmp_two->right;
       tmp_two->data = value;
       tmp_two->left = tmp_two->right = NULL;
      }
     }
    
     return(p);
    }
    
    /* print binary tree inorder */
    void print_inorder(struct tnode *p) {
     if(p != NULL) {
      print_inorder(p->left);
      printf("%c ", p->data);
      print_inorder(p->right);
     }
    }
    Last edited by creilly; 10-09-2010 at 02:04 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, creilly!

    Why do you want a bubble sort code, and where is it, if you do? You just want to use the tree for the sorting, maybe?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    Quote Originally Posted by Adak View Post
    Welcome to the forum, creilly!

    Why do you want a bubble sort code, and where is it, if you do? You just want to use the tree for the sorting, maybe?
    Woops, i didnt mean to say bubble sort haha. But ya, the code I posted works for sorting alphabetically, I just need to implement the read/scan from an input file and print the position number next to the letter as seen in the example link.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not even a FILE pointer?

    C'mon! You're not getting into this programming in C whole heartedly.I'm not going to hold your hand and spoon feed you what you want, when you want it. Get some "skin and sweat" into this effort.

    Go read our files tutorial, and try again:
    Cprogramming.com - Tutorials - C File I/O

    Edit: A recursive "walk" of the tree function example:

    Code:
    void t_walk(struct tree_node *root_p) {
    
          if(root_p == 0)
                  return;
          t_walk(root_p->left_p);
          printf("%d\n", root_p->data);
          t_walk(root_p->right_p);
    }
    Might look daunting, but it's a sweet bit of code, and well worth studying.
    Last edited by Adak; 10-09-2010 at 04:59 PM.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    Alright, I got the code to work, but it only works for inputs that are a single character. How can I get it to work if the input were a word instead of just a letter? I tried using strings but I get segmentations errors.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct tnode {
     char data;
     struct tnode *left;
     struct tnode *right;
    };
    
    struct tnode *tnode_insert(struct tnode *p, char value);
    void print_inorder(struct tnode *p);
    
    
    int main(void) {
     char demo_nr[50], tmp;
     struct tnode *root = NULL;
     int i=0, k=1, count=0, j;
     FILE *fp;
    
     fp=fopen("p2_input.txt", "r");
    
     while(k!=0){
            fscanf(fp, "%d %c", &k, &tmp);
            demo_nr[count]=tmp;
            count++;
     }
    
     for(i=0; i<count; i++)
      root = tnode_insert(root, demo_nr[i]);
     for(j=0; j<count; j++){
            printf("%d ", j);
     }
             printf("\n");
             print_inorder(root);
             printf("\n");
     return 0;
    }
    
    struct tnode *tnode_insert(struct tnode *p, char value) {
     struct tnode *tmp_one = NULL;
     struct tnode *tmp_two = NULL;
    
     if(p == NULL) {
      p = (struct tnode *)malloc(sizeof(struct tnode));
      p->data = value;
      p->left = p->right = NULL;
     } else {
      tmp_one = p;
    
      while(tmp_one != NULL) {
       tmp_two = tmp_one;
       if(tmp_one ->data > value)
        tmp_one = tmp_one->left;
       else
        tmp_one = tmp_one->right;
      }
    
      if(tmp_two->data > value) {
       tmp_two->left = (struct tnode *)malloc(sizeof(struct tnode));
       tmp_two = tmp_two->left;
       tmp_two->data = value;
       tmp_two->left = tmp_two->right = NULL;
      } else {
       /* insert [new] tnode as left child */
       tmp_two->right = (struct tnode *)malloc(sizeof(struct tnode));
       tmp_two = tmp_two->right;
       tmp_two->data = value;
       tmp_two->left = tmp_two->right = NULL;
      }
     }
    
     return(p);
    }
    
    void print_inorder(struct tnode *p){
     int j=0;
     if(p!=NULL) {
        print_inorder(p->left);
        printf("%c ", p->data);
        print_inorder(p->right);
     }
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have your data variable as type char. Of course it won't work as a string of char's - since it only will hold just one char.

    In C, one char never will have the room for the end of string char to be added to the end of it, so one char variables can never be a string.

    Change your data to a string, and try again.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    Im not sure how I would go about this. I tried changing my char variables to strings (char data[50]), but I get a lot of errors when compiling.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You changed this?

    Code:
    struct tnode {
     char data;
     struct tnode *left;
     struct tnode *right;
    };
    
    // To:
    
    struct tnode {
     char data[50];
     struct tnode *left;
     struct tnode *right;
    };
    Any other changes?

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    I also changed the char tmp to tmp[50] and the %c to %s

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, I'm pretty crappy with linked lists, but I'll shoot it into the oven - see what we can bake up here.

    Problem already - post your latest code.
    Last edited by Adak; 10-12-2010 at 12:43 AM.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    This is my latest code, I deleted all the changes I made since it would no longer compile. It should work, make sure you create a p2_input.txt with the format in the picture.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Checking it out with just char's, it has an error, showing 10 items, instead of just 9. You can correct it by changing your while loop for file entry to:

    Code:
      while(k!=0){
        j = fscanf(fp, "%d %c", &k, &tmp);
        if(j>1) {                        //stops erroneous entry
          demo_nr[count]=tmp;
          count++;
        }
      }
    Outside of that, the char program looks OK to me. Try that new line of code, with your strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21