Dont Know wrote:
> The solution I know, needs O(n) space.  Take a circular queue of size
> n.  Read a line at a time from the file and insert it into the queue.
> If the queue is full remove the first line and insert the newly read
> line.  Repeat this process untill EOF is encountered.  When we reach
> end of the file the queue will contain the last n lines of the file.

The problem with this is that it could involve a LOT of work (and
time), that isn't necessary.

OP stated that the file could be a huge one.

When you open a file, you have a file pointer, already. Just move the
file pointer to the end of the file, (just one command), and begin
backing up from that point. When you've encountered n number of CR/LF,
char's, you're ready to swap directions with the file pointer, and just
read the data like you normally would, until you reach EOF.

If the OS and / or your language offers a simple way to do a task, it's
almost always best to use it.

Adak


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to