[algogeeks] Re: Given a file (can be a huge file) ....

2006-08-10 Thread ridvansg
1. Go to the end of the file. 2. Make a stack. 3. Read a block from the end - put them into a chararray[]. 4. Split the charray by "\n" into strings[] 5. going through the strings[] backwards - put each string into the stack. 6. repeat 3-5 until stack.size=n 7. empty the stack --~--~-~

[algogeeks] Re: Given a file (can be a huge file) ....

2006-08-10 Thread adak
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

[algogeeks] Re: Given a file (can be a huge file) ....

2006-08-09 Thread Lego Haryanto
I would second arunachalam's idea (read block-by-block backwards starting from end-of-file).  If there's a source code to "tail" tool, then you might see something useful there as well.  On 8/9/06, Dont Know <[EMAIL PROTECTED]> wrote: The solution I know, needs O(n) space.  Take a circular queue of

[algogeeks] Re: Given a file (can be a huge file) ....

2006-08-09 Thread Dont Know
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

[algogeeks] Re: Given a file (can be a huge file) ....

2006-08-09 Thread Arunachalam
My idea would be go to the last block ( say each block is made of 4K pages ) and check whether it contains all the n lines. You can repeatedly read the blocks from the end of the file and stop at a point where all the n blocks are read.   This is just my vague thought.   You are free to ignore thi