Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Middle of a singly linked list of unknown length

Reply
Thread Tools

Middle of a singly linked list of unknown length

 
 
Himanshu Chauhan
Guest
Posts: n/a
 
      06-06-2008
Hi!

I was wondering, In the first parse of a singly linked list of unknown
length, is it possible to know when we are at middle of the linked list?

Regards
--Himanshu
 
Reply With Quote
 
 
 
 
Dan
Guest
Posts: n/a
 
      06-06-2008

> I was wondering, In the first parse of a singly linked list of unknown
> length, is it possible to know when we are at middle of the linked list?


You could do it if you kept track of how many were in the list when you
built it.


 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      06-06-2008
In article <g2atn8$k9f$>,
Himanshu Chauhan <> wrote:

>I was wondering, In the first parse of a singly linked list of unknown
>length, is it possible to know when we are at middle of the linked list?


Obviously not, since nothing would be different if extra elements were
added to the end.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
 
Reply With Quote
 
rahul
Guest
Posts: n/a
 
      06-06-2008
On Jun 6, 1:48 pm, Himanshu Chauhan <chauhan....@gmail.com> wrote:
> Hi!
>
> I was wondering, In the first parse of a singly linked list of unknown
> length, is it possible to know when we are at middle of the linked list?
>
> Regards
> --Himanshu

You can keep the count of nodes inserted as a static or global
variable. By counting
the number of iterations, you can decide if you are in the middle of
the list.

Just out of curiosity, this question is meant for some real code or is
it just like that.
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      06-06-2008
Himanshu Chauhan <> wrote:
> Hi!


> I was wondering, In the first parse of a singly linked list of unknown
> length, is it possible to know when we are at middle of the linked list?


Are you looking for the old trick of using two pointers, the first
one always getting set to the next element, the other one being set
to the successor of the next element so, when the second pointer
reaches the end of the list, the first one points to the middle
element? But I don't know if that is allowed by what you call
"first parse".

BTW, this hasn't anything to do with C, so such a question is
much better suited for comp.programming.

Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      06-06-2008
Himanshu Chauhan wrote:
>
> Hi!
>
> I was wondering, In the first parse of a singly linked list of unknown
> length, is it possible to know when we are at middle of the linked list?


The me rephrase the problem, and see if you can find a solution:

Drive down this road and stop halfway to a destination which I
have not yet revealed.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <private.php?do=newpm&u=>

 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      06-06-2008

"Kenneth Brody" <> wrote in message
news:...
> Himanshu Chauhan wrote:
>>
>> Hi!
>>
>> I was wondering, In the first parse of a singly linked list of unknown
>> length, is it possible to know when we are at middle of the linked list?

>
> The me rephrase the problem, and see if you can find a solution:
>
> Drive down this road and stop halfway to a destination which I
> have not yet revealed.


That's not quite the same. In that case you would not know when you got to
the destination so it's unsolveable. A linked list however has a definite
end point, assuming it's not circular.

Better, 'stop halfway to the end of the road of unknown length'. Easily done
by traversing the entire length one and a half times.
--
Bartc


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-06-2008
CBFalconer <> writes:
> Himanshu Chauhan wrote:
>> I was wondering, In the first parse of a singly linked list of
>> unknown length, is it possible to know when we are at middle of
>> the linked list?

>
> Yes. Just build the list in two halves, and join them when
> building is done. The head of the second half will be the
> midpoint, withing an error of one.


Right, finding a solution is easy if you're allowed to redefine the
problem.

The problem statement assumed "a singly linked list of unknown
length", not two lists of equal length.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      06-06-2008
On 6 Jun 2008 at 16:36, Keith Thompson wrote:
> CBFalconer <> writes:

[snip nonsense]
>
> Right, finding a solution is easy if you're allowed to redefine the
> problem.


Vintage CBF. At least two people have already provided a correct
solution (i.e. run two pointers through the list, one travelling at
half the speed of the other), but several hours later in wades Chuck
with something completely wrong-headed. (Still, I suppose at least he
tried, albeit unsuccessfully, to answer the damn question for once,
instead of telling the OP to get lost and try comp.programming.)

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-06-2008
In article <>,
Antoninus Twink <> wrote:
>At least two people have already provided a correct
>solution (i.e. run two pointers through the list, one travelling at
>half the speed of the other)


That's not a solution to the problem as I interpreted it. It uses
one-and-a-half passes over the list, rather than stopping in the
middle during the first pass.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reverse search in a singly-linked list RAJASEKHAR KONDABALA C Programming 20 02-27-2011 12:53 PM
pruning a linear singly linked list Anando C Programming 59 04-28-2006 11:12 AM
About Ordered singly linked list.. HS-MOON C++ 4 09-24-2004 02:26 PM
Stack & Singly Linked List Data Structures Patrick McCourt Java 2 05-24-2004 10:55 PM
AlphaSort for singly linked list CR C Programming 1 12-15-2003 08:52 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57