views:

654

answers:

4

Well I'm making a small phone book application and I've decided that using maps would be the best data structure to use but I don't know where to start. (Gotta implement the data structure from scratch - school work)

+1  A: 

A simple approach to get you started would be to create a map class that uses two vectors - one for the key and one for the value. To add an item, you insert a key in one and a value in another. To find a value, you just loop over all the keys. Once you have this working, you can think about using a more complex data structure.

anon
Two vectors? A single vector where each item is a struct containing together key and value would be simpler..
frunsi
We will have to disagree on that.
anon
+1 this is a great way to start
markh44
+4  A: 

Tries are quite efficient for implementing maps where the keys are short strings. The wikipedia article explains it pretty well.

To deal with duplicates, just make each node of the tree store a linked list of duplicate matches

Here's a basic structure for a trie

struct Trie {
   struct Trie* letter;
   struct List *matches;
};

malloc(26*sizeof(struct Trie)) for letter and you have an array. if you want to support punctuations, add them at the end of the letter array.

matches can be a linked list of matches, implemented however you like, I won't define struct List for you.

Charles Ma
ah my bad, that should have been a pointer
Charles Ma
Sorry but from my understanding, would it be more intuitive to name it node instead of Trie?`struct Node { struct Node* letter; struct List* matches;};`Thanks...Edit: Sorry, it seems that we can't put comments in "code" format.
Dois
Use `new`, not `malloc`.
GMan
+1  A: 

Simplest solution: use a vector which contains your address entries and loop over the vector to search.

A map is usually implemented either as a binary tree (look for red/black trees for balancing) or as a hash map. Both of them are not trivial: Trees have some overhead for organisation, memory management and balancing, hash maps need good hash functions, which are also not trivial. But both structures are fun and you'll get a lot of insight understanding by implementing one of them (or better, both :-)).

Also consider to keep the data in the vector list and let the map contain indices to the vector (or pointers to the entries): then you can easily have multiple indices, say one for the name and one for the phone number, so you can look up entries by both.

That said I just want to strongly recommend using the data structures provided by the standard library for real-world-tasks :-)

MartinStettner
A: 

Wow thanks for the quick help guys... Umm yeah about that - maps not handling collisions... how different is a binary tree from a map? I looked at Hash Tables and maps because it seems to suit the way a phone book is used well.

I'm also not too clear on the difference between hash tables and maps and all that.

I was thinking of a Hash Table but the impression I was getting is that it's too complex for me to learn because of all the efficiency considerations taken into account like load factor and all whatnot... along with the limited notes I could find on implementing the data structure...

Edit: Oh and also... in this course we've been taught Arrays, Linked Lists, Queues, Stacks and Trees so I'm somewhat familiar with those, now we're supposed to implement one of those or any other data structure in some simple application. We're also not allowed to use the STL so that's one limitation I have.

Dois
As I said in my answer, tries are the way to go. they don't require any balancing algorithms since they never grow very deep, and they're reasonably easy to implement. for the record, a trie is not a misspelling of a tree. imagine an array with 26 nodes, each node represents a letter, and each node has another 26 node array representing the next letter. That's all a trie is, just an array/linked list fusion.
Charles Ma
Oh... I see. Are tries the thing they use for when you're keying in a phone number in your handphone and they constantly bring up names of those related to the number as you type it in and it slowly narrows down to the one contact?
Dois
I don't know how they're implemented, but I wouldn't be surprised if some of them were implemented with tries.They can use tries with added heuristics (in the form of weights) that tell if a word is more likely to occur, but yes the underlying data structure for it is most intuitively a trie
Charles Ma