can I have two typedef with the same name but different things inside of it?
can I have two typedef with the same name but different things inside of it?
Sure, as long as they aren't in the same file and never get #included together.
Why would you want to? That would be very confusing.
hmm.. can I cast a typedef? it's basically just a pointer right?
so I have this:
when I call LinkedListConstruct it returns Words*, when I call HashTableConstruct I want it to return a similar thing too.. Words*, but my hashtable function returns a HashTable* which is a different kind of struct from Words*Code:struct node_t{ char* word; bla.. bla.. bla } typedef struct node_t Words;
I can't because I have a file that I can't change and that file does:
Words* f = DictionaryConstruct(some argument here); <== I can't change this!
and inside dictionary construct I have two types of construct, one is a hashtable construct and a bst construct.. which both returns a different type of struct.. and on the other hand I need to return a pointer to a struct called *Words
I mean when constructing a linked list and a bst, you got two different things going on at the same time:
in a struct of a linked list I have the next node (which is basically another struct inside a struct) I have the data itself. in a bst in the struct I have a left and right tree which is another struct inside a struct and the data
So what you're really asking is "can I make a function return two different things, depending on the phase of the moon and other unrelated things?" The answer is no -- DictionaryConstruct must always return the same type; it should never return sometimes a hashtable and sometimes a search tree. Pick one.
hmm.. so I can only return one? okay then, I'll have to figure out another way through this then.. by the way if I am implementing a hashtable, how can I get all the elements in that hash table if I don't know the key? is there a way to traverse a hash table?
hmm..let me explain my situation here more clearly:
so I have a code optimization assignment to be done. The program basically just takes two files,
first it stores the list of words in a data structure unsortedly in a hash table. Second it takes the second file, which is also a list of words. Before storing the second list of words into a binary tree I want to check first if that word exists in the first file, by checking the hash table. If it does exist then I put that word in a binary tree. In other words, two similar words that exists on both files are put inside a binary tree.
The problem is I have this code that I can't change or do anything to it:
Words* fd = Construct(..)
Words is just a pointer to a struct evertime it constructs that list of words file into a linked list (the current implementation is a linked list). But my HashTable returns a pointer to a struct which supports a hash table, a struct with key and values inside and my BinaryTree returns a pointer to a struct which is a binary tree struct/node that has a left and right subtree.
On the assumption your hash table is an array of linked lists, you can walk each linked list in order.
yes, my hash table is an array of linked list.. so I have to check each array slot it there's a data in that linked list? I guess this wouldn't be so efficient right
is there a way to iterate over a hash table and get the data one by one in a sorted way?
Unless your array of lists is somehow exponentially larger than it needs to be, traversal of a hash table is just as efficient of traversal of anything else; most every traversal is O(n) since you have to visit every item once.
Unless your hashing mechanism and list-inserting mechanism are very carefully designed, traversal of a hash table will not be in sorted order.