Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 11-18-2005, 07:27 AM   #1 (permalink)
sham1888
Registered User
 
Join Date: Nov 2005
Posts: 1
sham1888 is on a distinguished road
Hi needing help

I have been given a task of reading in a text file seperating the words in to a linked list then sorting them in to frequency of occurence.

I have got the reading of the file, creating the tokens and inserting them in to a linked list but I dont know where to start with regards to sorting them in to frequence of occurence.

any help would be very much appreciated
__________________
sham1888 is offline   Reply With Quote
Old 11-18-2005, 09:43 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
I know this answer isn't in strict Java, it's in pseudo C++, but that dosnt matter, its the theory in it that counts.

I would've made the linked list of some class of some sort ie:
Code:
class my_item{ int count; string name; my_item * next; my_item *prev; public: my_item(string n){name=n; count=1;}; public: void increment(){count++;}; };
Then when I read a word from the input, see if it's in the list, if it is increment the counter at the appropriate point, else add it to the list.
Once done reading, just sort the linked list according to counter size.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 11-19-2005, 02:03 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,114
Belisarius is on a distinguished road
What you can do is keep Hashtable using words as keys and the value as an Integer, incrementing it each time it's added. Then simply return the values as an array and call a sort using the Array object. Loop through the array and insert the items, in order, into a LinkedList.

A much more cool Java-esque way, however, would be to create a Comparable that contains the String an a count object, overriding equals(), and inserting it into a custom Set that will increment a counter in the Object that's not taken into account in the equals() method. Everytime a String is added to the Set, check to see if it exists, remove it from the Set it if does, increment the counter, and re-add it. The compareTo() would order first on count, then on the String. You could then simply dump it to a LinkedList.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
question needing an answer Androto Standard C, C++ 14 10-30-2004 12:33 PM
text prediction C toblerone Standard C, C++ 5 08-03-2004 03:49 AM


All times are GMT -8. The time now is 06:44 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle