473,503 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linked list

1 New Member
how can i add and delete nodes at the middle of the linked list??
Aug 27 '07 #1
6 2079
r035198x
13,262 MVP
how can i add and delete nodes at the middle of the linked list??
What do you so far?
Do you have the LinkedList and the Nodes already?
Aug 27 '07 #2
JosAH
11,448 Recognized Expert MVP
how can i add and delete nodes at the middle of the linked list??
Think a bit: suppose you know how to reach the previous node and the current
node; you can reach the next node then. There are two possibilities: there is
no previous node or there is. In the first case the current node is the first node
in the list, otherwise it isn't. You're not really interested whether or not the next
node exists.

kind regards,

Jos
Aug 27 '07 #3
Nepomuk
3,112 Recognized Expert Specialist
how can i add and delete nodes at the middle of the linked list??
If you want to add/delete a node at the middle position, you have an annoying task infront of you:

You first have to count, how many elements are in the list (if no such function is preimplemented), then you have to find the middle position (Spoiler: number of elements divided by 2) and add/delete the element there.

If you have the choice, use a different type of list (e.g. ArrayList), as this makes life much easier.
Aug 28 '07 #4
JosAH
11,448 Recognized Expert MVP
If you want to add/delete a node at the middle position, you have an annoying task infront of you:

You first have to count, how many elements are in the list (if no such function is preimplemented), then you have to find the middle position (Spoiler: number of elements divided by 2) and add/delete the element there.

If you have the choice, use a different type of list (e.g. ArrayList), as this makes life much easier.
I didn't understand 'in the middle' that literally ;-)
But even if you do take it that way you don't have to count: take two pointers;
make them point at the start of the list. Then let them run: the first pointer runs
twice as fast as the second pointer so when the first pointer runs off the list,
the second pointer points to the middle element of the list (if present).

kind regards,

Jos
Aug 28 '07 #5
Nepomuk
3,112 Recognized Expert Specialist
take two pointers;
make them point at the start of the list. Then let them run: the first pointer runs
twice as fast as the second pointer so when the first pointer runs off the list,
the second pointer points to the middle element of the list (if present).
Hm, nice trick! I'll have to remember that one! ^^
Aug 28 '07 #6
JosAH
11,448 Recognized Expert MVP
Hm, nice trick! I'll have to remember that one! ^^
I'm just an old bag full of (old) tricks ;-) You have to take care of the details if
you want to make use of this trick, e.g. if you let the 'fast' pointer run first,
don't run the second pointer if the first one falls of the list in one of its two jumps.
That way you either end up at the middle element (with the slow pointer) when
there are an odd number of elements in the list, or you end up at element n/2
(integer division) when there are an even number n elements in the list.

kind regards,

Jos
Aug 28 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
859
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
15099
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
4582
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
0
8611
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
5756
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
7203
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
7281
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
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6993
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...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4675
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...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1514
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 ...
0
383
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...

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.