473,386 Members | 1,860 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,386 software developers and data experts.

inserting an item after a specific position in linked List

43
hi ,
i wanna ask about how to insert an item in a linked list..
this item should be inserted after a specific position..

any ideas??
Oct 31 '07 #1
4 3481
mac11
256 100+
hi ,
i wanna ask about how to insert an item in a linked list..
this item should be inserted after a specific position..

any ideas??
In general you break the link at the place where you want to insert your new node and form new links from your new node to the places where you broke the link.

so if you have A -> B -> C -> E and you want to insert D you break the link from C-> E and make C -> D -> E

The details of how it's actually accomplished will vary depending upon the implementation of the list.
Oct 31 '07 #2
maloov
43
thanx mac..

but ma problem actually in breaking the link..
how to break it..
we should assign something to another..
here where i'm kinda lost..
Oct 31 '07 #3
Ganon11
3,652 Expert 2GB
Let's take the above example of the following list:

A->B->C->E->(rest of the list)

You want to insert D.

First you have to get a node* to point to C by going first to A, then following the node* next pointers until you get to C. So let's look at the situation:

Before the insert:

Expand|Select|Wrap|Line Numbers
  1. C:
  2. Has some data (C)
  3. next points to E
  4.  
  5. E:
  6. Has some data (E)
  7. next points somewhere (it doesn't matter where)
  8.  
  9. D (node to be inserted):
  10. Has some data (D)
  11. next points nowhere (null)
And after the insert:


Expand|Select|Wrap|Line Numbers
  1. C:
  2. Has some data (C)
  3. next points to D
  4.  
  5. E:
  6. Has some data (E)
  7. next points somewhere (it doesn't matter where)
  8.  
  9. D (node to be inserted):
  10. Has some data (D)
  11. next points to E
Hmm...only two things changed: C's next pointer, and D's next pointer. The data didn't change anywhere, and E wasn't changed.

Can you figure it out from here? It's just a few simple assignment statements.
Oct 31 '07 #4
maloov
43
aha ..

ok i've got it now..
thanx buddy..
Oct 31 '07 #5

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

Similar topics

0
by: Jonathan Moss | last post by:
I have (almost) succesfully created a linked list data structure class. It will quite happily add items to to the end of the list (once I found that objects are not passed by reference :). What I...
1
by: Kay | last post by:
There are two different function. The first one loads data from a txt file and adds the name and num to the linked list. It can display the context of the linked list. I'm designing to use second...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
1
by: Udi | last post by:
Hi, I have a control "A" that displays a list of control "B". How do I add the ability to insert a new "B" to the list through a context menu? I mean, What do I need to do in the event handler...
2
by: Joe Fallon | last post by:
I can add items to my dropdown list using javascript like this: cbo.length = j+2; cbo.options.text = "Some Text"; cbo.options.value = "MyValue"; cbo.options.text = "Some Other Text";
5
by: Joe Fallon | last post by:
I have a list box with 7 text values in it. I have a pair of buttons to Move Up or Move Down the selected item one position. What is the simplest way to code the buttons so the item moves one...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
6
by: DWrek | last post by:
Here is my problem. I have an XML document that is returned to me by a third party service. The XML document contains results for a search but only lists a maximum of 10 results. If there are any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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...

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.