-1

So, I've done a little reading and consulted help from a friend. And I think I get it? Actually, my part in the project is just to store the table of characters and frequencies into a linked list. I've written down some codes, so is it possible if anyone can improve it.

Sample Input .txt file (the table of characters and frequencies):
B1
D3
E7
J9

The struct:

struct node {
  char info;
  int freq;
  struct node * next;   
  struct node * left, *right, *father;
};

typedef struct node * nodeptr;

nodeptr getnode(){  
return malloc(sizeof(struct node));
}

The main program (just until the part of storing the table into a linked list):

string input;
nodeptr list = NULL;
FILE *fopen();
int c;
list = fopen("Huffman Table.txt","r");
c = getc(input) ;
while (c!= EOF)
{
    putchar(c);
    c = getc(input);
}
getch();
fclose(input);

for (node * row = table; row != NULL; row = row->next){
fprintf(file, "%s %i %i", row->info, row->freq);
}

I'm not sure about this part though:

for (node * row = table; row != NULL; row = row->next)

Should I just use this instead?

for(i=0;i<strlen(input);i++){
AstroCB
  • 12,337
  • 20
  • 57
  • 73
  • Your code actually does nothig about storing characters on any list. Try to improve yourself the code reading from file and storing something in any list node, creating it before and linking it to the list. – Tio Pepe Apr 17 '12 at 08:44

2 Answers2

0

I don't think

for (node * row = table; row != NULL; row = row->next) would work because you're suppose to pass in an assignment before the first semicolon.

what you're doing is... i don't really know..

for(i=0;i<strlen(input);i++){

would work better. But i'm not sure, because i don't know what string input is suppose to hold and how you're using row and table

Kelsey Abreu
  • 1,094
  • 3
  • 17
  • 42
0

The used for loop is (syntactically) perfectly OK, and isn't that uncommon. However, what's the "table" pointer? Is it root of your list? Small remark - if you created "nodeptr" typedef, you should use it there. That's what it's for. There are some other parts of your code I'm concerned about

  1. You said it was supposed to be a linked list, so why your struct has "father" pointer? It looks more like a tree to me. If it weren't the intent, remove that field.

  2. I am not sure I understood the last piece of code you wrote. If you want to write contents of the list to a file, you should use the for loop mentioned above.

And as of general improvement to this code (which looks like a homework to me, and i guess should be tagged as such), is to use std::list<>. It can however be done only, if you're allowed to code in C++ rather in C.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • oh that's because it'll be used as a tree later... my part of the project is just to implement the .txt table into a linked list. so does that mean i have to make another struct for the table and another for the tree later? – nutellafella Apr 17 '12 at 08:48
  • Well, you can leave it just for now, but it's a bit misleading. Still, it isn't the main problem here... – Bartek Banachewicz Apr 17 '12 at 12:22