473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Middle of a link list

Can any body help in finding the middle element of a link list in an
effective manner

i know the simple solution of using two pointers and increment one
pointer by one and second by two.

but i want a effective algo both in terms of memory and time

Jul 21 '07 #1
4 2028
On 21 Jul, 07:23, ravi <dceravigu...@gmail.comwrote:
Can any body help in finding the middle element of a link list in an
effective manner

i know the simple solution of using two pointers and increment one
pointer by one and second by two.

but i want a effective algo both in terms of memory and time
If the structure is simply a linked list, there is no quicker way to
locate the middle element than by starting at one end and moving along
one element at a time. If the structure is a doubly linked list you
could improve on the method you have suggested slightly by starting
one pointer at the front, one at the end, and seeing where they meet -
that way you only traverse the list once instead of one and a half
times.

Hope that helps.
Paul.

Jul 22 '07 #2
On Jul 22, 4:34 pm, gw7...@aol.com wrote:
On 21 Jul, 07:23, ravi <dceravigu...@gmail.comwrote:
Can any body help in finding the middle element of a link list in an
effective manner
i know the simple solution of using two pointers and increment one
pointer by one and second by two.
but i want a effective algo both in terms of memory and time

If the structure is simply a linked list, there is no quicker way to
locate the middle element than by starting at one end and moving along
one element at a time. If the structure is a doubly linked list you
could improve on the method you have suggested slightly by starting
one pointer at the front, one at the end, and seeing where they meet -
that way you only traverse the list once instead of one and a half
times.

Hope that helps.
Paul.
But how i get address of the last element of the doubly link list

for eg. i am using

struct node
{
int data;
node* next;
node *previous;
};

Jul 22 '07 #3
ravi wrote:
>
On Jul 22, 4:34 pm, gw7...@aol.com wrote:
On 21 Jul, 07:23, ravi <dceravigu...@gmail.comwrote:
If the structure is a doubly linked list you
could improve on the method you have suggested slightly by starting
one pointer at the front, one at the end,
and seeing where they meet -
that way you only traverse the list once instead of one and a half
times.
But how i get address of the last element of the doubly link list
For that matter, how do you get address of the first element?

--
pete
Jul 22 '07 #4

"ravi" <dc**********@gmail.comwrote in message
news:11**********************@g12g2000prg.googlegr oups.com...
On Jul 22, 4:34 pm, gw7...@aol.com wrote:
>On 21 Jul, 07:23, ravi <dceravigu...@gmail.comwrote:
Can any body help in finding the middle element of a link list in an
effective manner
i know the simple solution of using two pointers and increment one
pointer by one and second by two.
but i want a effective algo both in terms of memory and time

If the structure is simply a linked list, there is no quicker way to
locate the middle element than by starting at one end and moving along
one element at a time. If the structure is a doubly linked list you
could improve on the method you have suggested slightly by starting
one pointer at the front, one at the end, and seeing where they meet -
that way you only traverse the list once instead of one and a half
times.

Hope that helps.
Paul.

But how i get address of the last element of the doubly link list

for eg. i am using

struct node
{
int data;
node* next;
node *previous;
};
The same way you get the pointer to the head of the doubly
linked list.
Jul 22 '07 #5

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

Similar topics

2
by: lawrence | last post by:
Right now, over at www.monkeyclaus.org, the following script is getting to the 9th run through and dying after the line: echo "..."; You'll admit that is a strange place to die. I've hit...
3
by: DS | last post by:
I would like to construct a frame in the middle of a page, similar to a text box with scrollers, but I want the part in the box to be AN html page .... What I want to do is take a LARGE amount...
4
by: lindsey.crocker | last post by:
I have this links list with background images set on them which changes when they roll over. The <td> is set valign="middle" however as soon as you apply the rollover to the link, the text jumps...
15
by: Hi5 | last post by:
Hi, I am designing a database for a client in which It has a client table including the followings: 1-Table Client 2-Table lookupcategory 3-Table Ctegory
6
by: Ian Williamson | last post by:
Greetings, My company has an ASP.NET based enterprise product that is undergoing some changes and I need some community input to help solve a problem. In the current implementation, any given...
2
by: Daniel | last post by:
Is is possible to remove elements from the middle of a System.Collections.Queue? For the most part i just use endqueue and dequeue but there are points where i know that i no longer need certain...
2
by: wpollans | last post by:
Hello, I need to able to write JS that will click on a link with the middle mouse button - so that the link target will open in a new window or tab - using firefox. Or is there a better (more...
23
by: Himanshu Chauhan | last post by:
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
4
by: Ben | last post by:
Hello All: I am new to Python, and I love it!! I am running 2.6 on Windows. I have a flat text file here is an example of 3 lines with numbers changed for security: ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.