944,007 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5533
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 26th, 2008
0

Trie's in C

Expand Post »
I have a homework assignment for a c class I am in currently, the assignment is to write a simple spell checker. I am having a bit of trouble understanding how to write a trie in c. A trie would be composed of nodes, which contain the following I believe:

1. char value (value of the node)
2. boolean isroot (is this the root node)
3. boolean endsword (is this the end of a word)
optionally
4. Node *nextnode ( a pointer to the next node if one exists).

What I am confused on, is how this should be written as an ADT in c. I have found a few samples online based in Java, but they tend to have too many features.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008
Oct 26th, 2008
0

Re: Trie's in C

I would also like to add they need a 2nd pointer to the next sibling.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008
Oct 26th, 2008
0

Re: Trie's in C

So I have been working a bit further but seem to have something wrong in my code. The header file for each node is this "node.h"
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <stdbool.h>
  7.  
  8. typedef struct node {
  9. char value;
  10. bool isroot;
  11. bool isend;
  12. struct node* sibling;
  13. struct node* child;
  14. }

The code for my test of the node structure is this "node.c"
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdbool.h>
  6. #include "node.h"
  7.  
  8. int main(void){
  9. return 0;
  10. }

The makefile reads as such:
  1. CC=gcc
  2. CFLAGS=-Wall -g
  3.  
  4. eread: node.o
  5. $(CC) $(CFLAGS) -o $@ node.o
  6.  
  7. node.c: node.h

And finally the error message is this
  1. $ make
  2. gcc -Wall -g -c -o node.o node.c
  3. node.c:6: error: syntax error at end of input
  4. make: *** [node.o] Error 1

Any ideas what is going on here?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008
Oct 26th, 2008
0

Re: Trie's in C

Missing ; at end of typedef.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 26th, 2008
0

Re: Trie's in C

so new header file is this:
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <stdbool.h>
  7.  
  8. typedef; struct node {
  9. char value;
  10. bool isroot;
  11. bool isend;
  12. struct node* sibling;
  13. struct node* child;
  14. }

And the error message then becomes:
  1. $ make
  2. gcc -Wall -g -c -o node.o node.c
  3. In file included from node.c:6:
  4. node.h:8: warning: useless storage class specifier in empty declaration
  5. node.h:8: warning: empty declaration
  6. node.c:8: error: two or more data types in declaration specifiers
  7. node.c:8: warning: return type of ‘main’ is not ‘int
  8. node.c: In function ‘main’:
  9. node.c:9: error: incompatible types in return
  10. node.c:10: warning: control reaches end of non-void function
  11. make: *** [node.o] Error 1

The warnings are not as troubling to me as the errors, though I am pretty sure that whatever is causing them is related.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008
Oct 26th, 2008
0

Re: Trie's in C

Funny:
  1. typedef struct node {
  2. char value;
  3. bool isroot;
  4. bool isend;
  5. struct node* sibling;
  6. struct node* child;
  7. }; //<== if semicolon goes here, it's much more reasonable
Last edited by Sci@phy; Oct 26th, 2008 at 7:34 pm.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 26th, 2008
0

Re: Trie's in C

typedef struct node { 
	char value;
	bool isroot;
	bool isend;
	struct node* sibling;
	struct node* child; 
};
He meant there.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 26th, 2008
0

Re: Trie's in C

Wow... now I feel... dumb . Anyway brings me to a final warning
  1. $ make
  2. gcc -Wall -g -c -o node.o node.c
  3. In file included from node.c:6:
  4. node.h:14: warning: useless storage class specifier in empty declaration
  5. gcc -Wall -g -o eread node.o

What exactly does this mean?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thanatosys is offline Offline
15 posts
since Oct 2008
Oct 26th, 2008
0

Re: Trie's in C

Do you know why do you use typedef?
It's syntax should be:
  1. typedef some huge type A_DEF;

And the code you wrote has no use from typedef since you haven't specified name that will replace that struct.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 26th, 2008
0

Re: Trie's in C

>What exactly does this mean?
typedef struct node { 
	char value;
	bool isroot;
	bool isend;
	struct node* sibling;
	struct node* child; 
} aliasname;
Where aliasname is any identifier you want to substitute struct node for.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Duplicates project :
Next Thread in C Forum Timeline: need some help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC