•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 401,669 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,532 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1394 | Replies: 5
![]() |
•
•
Join Date: Apr 2004
Location: Dhaka, Bangladesh
Posts: 344
Reputation:
Rep Power: 5
Solved Threads: 3
This is probably a stupid question, but i dont get the answer. Plz help me understand.
Question:
How could a linked list and a hash table be combined to allow someone to run through the list from item to item while still maintaining the ability to access an individual element in O(1) time?
Solution:
Create a hash table where the elements of the array are pointers to linked list objects. You have your linked list as normal, but when you add an element to the linked list, "you also add a pointer to that element from the correct place in the hash table specified by the hash value of the data in the new linked list element".
I dont understand what you also add a pointer to that element from the correct place in the hash table specified by the hash value of the data in the new linked list element. means.
Question:
How could a linked list and a hash table be combined to allow someone to run through the list from item to item while still maintaining the ability to access an individual element in O(1) time?
Solution:
Create a hash table where the elements of the array are pointers to linked list objects. You have your linked list as normal, but when you add an element to the linked list, "you also add a pointer to that element from the correct place in the hash table specified by the hash value of the data in the new linked list element".
I dont understand what you also add a pointer to that element from the correct place in the hash table specified by the hash value of the data in the new linked list element. means.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
OK, I assume you know what a linked list is else it gets cumbersome 
A hashtable is a special type of map where the items are indexed on the hashcode of the key.
The hashcode is an integer calculated over the item being stored, in this case your data.
The hashtable contract specifies that items with identical value have identical hashcodes as well.
In other words if you have 2 objects a and b where a == b, then a.hashCode() == b.hashCode().
This facilitates fast lookup. Internally the items are indexed on hashcode, when you try to find an object by the key the hashcode of that key is calculated. That provides an index into the datastorage where a group of items stored with that hashcode are kept.
If the hashing algorithm is perfect there will always be only 1 item there. If not you will at least have a far smaller number of items to compare with your key than the entire collection.
What you're said to do is store a key into your stored data into a hashtable.
As the data you store a pointer to that data, the same pointer which also indexes that same data in your linked list.
So you get both a List (fast sequential lookup) and a Map (fast random lookup) of the same data for practically the price of one.
List Data Hashtable
1 *O1 O1.hashCode()
2 *O2 O2.hashCode()
3 *O3 O2.hashCode()
4 *O4 O2.hashCode()
5 *O5 O2.hashCode()
Now if you want to look up an item and you know the key (from which you can calculate the hashcode) you can do it through the hashtable.
If on the other hand you want to look up elements by index or iteration you can use the list.

A hashtable is a special type of map where the items are indexed on the hashcode of the key.
The hashcode is an integer calculated over the item being stored, in this case your data.
The hashtable contract specifies that items with identical value have identical hashcodes as well.
In other words if you have 2 objects a and b where a == b, then a.hashCode() == b.hashCode().
This facilitates fast lookup. Internally the items are indexed on hashcode, when you try to find an object by the key the hashcode of that key is calculated. That provides an index into the datastorage where a group of items stored with that hashcode are kept.
If the hashing algorithm is perfect there will always be only 1 item there. If not you will at least have a far smaller number of items to compare with your key than the entire collection.
What you're said to do is store a key into your stored data into a hashtable.
As the data you store a pointer to that data, the same pointer which also indexes that same data in your linked list.
So you get both a List (fast sequential lookup) and a Map (fast random lookup) of the same data for practically the price of one.
List Data Hashtable
1 *O1 O1.hashCode()
2 *O2 O2.hashCode()
3 *O3 O2.hashCode()
4 *O4 O2.hashCode()
5 *O5 O2.hashCode()
Now if you want to look up an item and you know the key (from which you can calculate the hashcode) you can do it through the hashtable.
If on the other hand you want to look up elements by index or iteration you can use the list.
•
•
Join Date: Apr 2004
Location: Dhaka, Bangladesh
Posts: 344
Reputation:
Rep Power: 5
Solved Threads: 3
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
•
•
Join Date: Oct 2005
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
I still do not understand. Could you please elucidate what you mean by:-
"What you're said to do is store a key into your stored data into a hashtable. As the data you store a pointer to that data, the same pointer which also indexes that same data in your linked list"
Assuming the hashtable is represented by the array Arr. If data items X, Y & Z hash to the same value (say I), then Arr[i] would contain a pointer to the linked list containing X,Y,Z. Now, suppose someone wants to search for Z in the hashtable. Z hashes to I. Then a sequential lookup of the linked list contained in Arr[i] is done, and Z is found. It takes O(3). How can we make it O(1)?
Thanks
Dio
"What you're said to do is store a key into your stored data into a hashtable. As the data you store a pointer to that data, the same pointer which also indexes that same data in your linked list"
Assuming the hashtable is represented by the array Arr. If data items X, Y & Z hash to the same value (say I), then Arr[i] would contain a pointer to the linked list containing X,Y,Z. Now, suppose someone wants to search for Z in the hashtable. Z hashes to I. Then a sequential lookup of the linked list contained in Arr[i] is done, and Z is found. It takes O(3). How can we make it O(1)?
Thanks
Dio
>Then a sequential lookup of the linked list contained in Arr[i] is done, and Z is found. It takes O(3).
That's still O(1) because it's a constant time. But if the hash function is good, the extra cost of a sequential search is negligible since the lists will all be very short. The only way to get guaranteed and exact O(1) performance as you're thinking of it is to use a perfect hash, which isn't always possible.
That's still O(1) because it's a constant time. But if the hash function is good, the extra cost of a sequential search is negligible since the lists will all be very short. The only way to get guaranteed and exact O(1) performance as you're thinking of it is to use a perfect hash, which isn't always possible.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C Forum
- Previous Thread: I want to get image from circle using graphics plz help
- Next Thread: returning by reference



Linear Mode