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);
}
}