473,608 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Middle of a singly linked list of unknown length

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
Jun 27 '08 #1
23 4357
In article <g2**********@a ioe.org>,
Himanshu Chauhan <ch*********@gm ail.comwrote:
>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)
Jun 27 '08 #2
Dan
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.
Jun 27 '08 #3
On Jun 6, 1:48 pm, Himanshu Chauhan <chauhan....@gm ail.comwrote:
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.
Jun 27 '08 #4
Himanshu Chauhan <ch*********@gm ail.comwrote:
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.programmin g.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jun 27 '08 #5
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #6
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: <mailto:Th***** ********@gmail. com>

Jun 27 '08 #7

"Kenneth Brody" <ke******@spamc op.netwrote in message
news:48******** *******@spamcop .net...
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
Jun 27 '08 #8
In article <sl************ *******@nospam. invalid>,
Antoninus Twink <no****@nospam. invalidwrote:
>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)
Jun 27 '08 #9
Antoninus Twink wrote:
On 6 Jun 2008 at 16:36, Keith Thompson wrote:
>CBFalconer <cb********@yah oo.comwrites:
[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),
I think those so-called solutions are ruled out by the
O.P.'s requirement to get the answer "in the first parse"
over the list, which I read as a garbled form of "in the
first pass" over the list. Using two pointers means using
one-and-a-half passes.
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.programmin g.)
Right, that was a mistake on his part. Chuck, I hereby
chastise you for answering an off-topic question instead of
sending the questioner to comp.programmin g where he belongs
and where he'll get better answers.

--
Er*********@sun .com
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
13562
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e., head and tail are not connected. Of course, recursive function aproach to traverse the list is one way. But, depending upon the list size, it could overrun the stack pretty fast.
7
1969
by: Shwetabh | last post by:
Hi, can some one tell me: -> how to remove a loop from a singly linked list with a loop. -> how to count the number of nodes in a looped singly link list. Thanks
3
3315
by: Little | last post by:
Could someone help me get started on this program or where to look to get information, I am not sure how to put things together. 1. Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like identifiers of less than 256 characters long identified in the input file F. Each identifier will be represented by a triple (I, length, string) where I is used to identify the type of
1
4149
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like identifiers of less than 256 characters long identified in the input file F. Each identifier
3
472
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for your assitance in helping me solve this problem. Information: Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like
13
6551
by: Raj | last post by:
Is there any way to delete a particular node from a singly linked list where the header of the list is unknown.Only pointer available is the one which points to the node to be deleted
7
5730
by: davidson1 | last post by:
Hello friends, I want ur help regarding singly linked list in C,I tried in net,But it is confusing and difficult.I want singly linked list in a simple way of Understanding.Please Help Me I want single list to create,delete,Display Can u give me Program and Explanation,to create,display,Delete in a Simple and Easy Way.Really it will be Helpful for Me. I have given some partial code,I want Program to create,display,Delete a Singly...
0
8059
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8000
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1328
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.