This will be in Python We have used the node structure and a linked-list structu
ID: 670348 • Letter: T
Question
This will be in Python
We have used the node structure and a linked-list structure as shown below:
Class Node:
def __init__(self,initdata):
self.data = initdata
self.next = none
Class UnorderedList:
def __init__(self):
self.head = None
Now, for an unordered linked-list, I need to write an algorithm called count which will count the number of occurrences of a specific piece of data. If you could add in notes that would be very helpful.
Thank you
Explanation / Answer
int SortedWordCount(ListItem *list, int *wordCount):
if list is not the head:
error out
for every item in list:
if item is NULL:
set word count to 0 for this item and continue
else:
for every newItem after this one:
if newItem is the same as item:
increment a counter
save the word count for this item
for every newItem after this one:
if newItem is the same as item:
save the negative of the word count for this newItem
****
node = list.head
count = 0
while node: # while we have a valid node
if node.data == target_data:
count++
node = node.next # assume this returns a "false" value if there is no next node
using hash:
map = new HashMap()
for (item : list)
{
if map.has_key(item)
++map[item]
else
map[item] = 0
}
map[item] contains the number of occurrences of item
count = 0
for (item : list)
{
if (item == counted_item)
{
++count}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.