473,749 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert node in Linked list without temp pointer

Hi All,

I would like to know how it is possible to insert a node in a linked
list without using a temp_pointer. If the element is the first element
then there is no problem but if it is in between then what is the best
solution.

Jan 3 '08 #1
10 10503
On Jan 3, 7:17*am, Aditya <aditya.to...@g mail.comwrote:
Hi All,

I would like to know how it is possible to insert a node in a linked
list without using a temp_pointer. If the element is the first element
then there is no problem but if it is in between then what is the best
solution.
Why would a temp pointer ever be needed?

To insert node c between a and b:
a->next = c
c->prev = a
c->next = b
b->prev = c
--
Fred Kleinschmidt
Jan 3 '08 #2
On Jan 3, 4:35*pm, fred.l.kleinsch m...@boeing.com wrote:
On Jan 3, 7:17*am, Aditya <aditya.to...@g mail.comwrote:
Hi All,
I would like to know how it is possible to insert a node in a linked
list without using a temp_pointer. If the element is the first element
then there is no problem but if it is in between then what is the best
solution.

Why would a temp pointer ever be needed?

To insert node c between a and b:
a->next = c
c->prev = a
c->next = b
b->prev = c
--
Fred Kleinschmidt
Hi ,

It is a Single Link list not a double link list...!

Aditya
Jan 3 '08 #3
Aditya <ad**********@g mail.comwrites:
I would like to know how it is possible to insert a node in a linked
list without using a temp_pointer. If the element is the first element
then there is no problem but if it is in between then what is the best
solution.
Pointers can be confusing, so I would write the code to be as
clear as possible. If that involves using a temporary pointer
variable, so be it.
--
Ben Pfaff
http://benpfaff.org
Jan 3 '08 #4
On 3 Jan, 15:51, Aditya <aditya.to...@g mail.comwrote:
On Jan 3, 4:35*pm, fred.l.kleinsch m...@boeing.com wrote:


On Jan 3, 7:17*am, Aditya <aditya.to...@g mail.comwrote:
Hi All,
I would like to know how it is possible to insert a node in a linked
list without using a temp_pointer. If the element is the first element
then there is no problem but if it is in between then what is the best
solution.
Why would a temp pointer ever be needed?
To insert node c between a and b:
a->next = c
c->prev = a
c->next = b
b->prev = c
--
Fred Kleinschmidt

*Hi ,

It is a Single Link list not a double link list...!

Aditya- Hide quoted text -

- Show quoted text -
So what's wrong with
c->next = a->next
a->next = c

Jan 3 '08 #5
On Jan 3, 10:51 am, Aditya <aditya.to...@g mail.comwrote:
On Jan 3, 4:35 pm, fred.l.kleinsch m...@boeing.com wrote:
On Jan 3, 7:17 am, Aditya <aditya.to...@g mail.comwrote:
Hi All,
I would like to know how it is possible to insert a node in a linked
list without using a temp_pointer. If the element is the first element
then there is no problem but if it is in between then what is the best
solution.
Why would a temp pointer ever be needed?
To insert node c between a and b:
a->next = c
c->prev = a
c->next = b
b->prev = c
--
Fred Kleinschmidt

Hi ,

It is a Single Link list not a double link list...!

Aditya
Even simpler:
a->next = c
c->next = b
Jan 3 '08 #6

We have only start pointer which points to the start of link list say
there are currently 4 nodes in link list a,b,d,e. We need to insert a
node c between b and d .

1.) step would be to create the new node c and temp pointer will point
to the C.

Now tell me how will we insert it

c->next = a->next
a->next = c

above logic will work if we know the address of node a but as we have
only start point then how will be manage this ...?

Jan 3 '08 #7
On Jan 3, 10:41*am, Aditya <aditya.to...@g mail.comwrote:
We have only start pointer which points to the start of link list say
there are currently 4 nodes in link list a,b,d,e. We need to insert a
node c between b and d .

1.) step would be to create the new node c and temp pointer will point
to the C.

Now tell me how will we insert it

c->next = a->next
a->next = c

above logic will work if we know the address of node a but as we have
only start point then how will be manage this ...?
You have not yet made your question clear. To insert the c node
between b and d you must walk the list untul you get to the b node
then insert with:

c->next = b->next
b->next = c

Are you trying to not walk the list?

Steve

Jan 3 '08 #8
jxh
On Jan 3, 10:41*am, Aditya <aditya.to...@g mail.comwrote:
We have only start pointer which points to the start of
link list say there are currently 4 nodes in link list
a,b,d,e. We need to insert a node c between b and d .

1.) step would be to create the new node c and temp
pointer will point to the C.

Now tell me how will we insert it

c->next = a->next
a->next = c

above logic will work if we know the address of node a but
as we have only start point then how will be manage this
...?
Assuming this is an interview question, you should first assert
that in a real program you would do whatever it takes to make sure
the program is written correctly and clearly, which would probably
involve using a temporary.

Second, since a linked list is not a random access data structure,
positional insertion should not be required, since an O(n) search
will be required to retrive the item later anyway, it doesn't
really matter what position it is at. Efficient search retrieval
should be achieved using a data structure that is designed for that
purpose, such as a hash or a search tree.

Understanding that the limitations are being artificially imposed
for the purpose of the interview, you should attempt to get some
clarifications.

How is the point of insertion being communicated to the code
that is being written?

Are you writing a function that is performing an insertion?

Is the list circular?

Does the position of the head matter after the end of the
insertion?

Are multiple passes allowd?

In any case, one possible solution is this:

node *insert (node *head, void *data, unsigned pos)
{
node *n = new_node();
n->next = head;
head = n;
while (pos 0 && n->next) {
n->data = n->next->data;
n = n->next;
--pos;
}
n->data = data;
return head;
}

-- James
Jan 4 '08 #9
On Jan 4, 2:10*am, jxh <j...@despammed .comwrote:
On Jan 3, 10:41*am, Aditya <aditya.to...@g mail.comwrote:
We have only start pointer which points to the start of
link list say there are currently 4 nodes in link list
a,b,d,e. We need to insert a node c between b and d .
1.) step would be to create the new node c and temp
pointer will point to the C.
Now tell me how will we insert it
c->next = a->next
a->next = c
above logic will work if we know the address of node a but
as we have only start point then how will be manage this
...?

Assuming this is an interview question, you should first assert
that in a real program you would do whatever it takes to make sure
the program is written correctly and clearly, which would probably
involve using a temporary.

Second, since a linked list is not a random access data structure,
positional insertion should not be required, since an O(n) search
will be required to retrive the item later anyway, it doesn't
really matter what position it is at. *Efficient search retrieval
should be achieved using a data structure that is designed for that
purpose, such as a hash or a search tree.

Understanding that the limitations are being artificially imposed
for the purpose of the interview, you should attempt to get some
clarifications.

How is the point of insertion being communicated to the code
that is being written?

Are you writing a function that is performing an insertion?

Is the list circular?

Does the position of the head matter after the end of the
insertion?

Are multiple passes allowd?

In any case, one possible solution is this:

node *insert (node *head, void *data, unsigned pos)
{
* * node *n = new_node();
* * n->next = head;
* * head = n;
* * while (pos 0 && n->next) {
* * * * n->data = n->next->data;
* * * * n = n->next;
* * * * --pos;
* * }
* * n->data = data;
* * return head;

}

-- James
Hi James,

Yes it was an interview question where the things were not cleared to
add confusion.

The solution you had proposed it the best way to deal it.

The question was asked because in the current implementation we were
using an temp pointer to insert a node in the link list so the next
question in interview asked was if we don't allow you to use the temp
pointer then how will you insert the node in the link list.

Now the things are clear ...!

Thanks to all of you who had participated in the discussion.

Aditya
Jan 4 '08 #10

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

Similar topics

10
5493
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the following: struct Node { int integer_value;
8
1697
by: farhanb | last post by:
hello, I am writing a simple linked list implementation. When I use function insert1 I must allocate to head in my main to get it to work otherwise list stays empty but when I use function insert2 there is no need to allocate to head this is the correct way. why does this happen? why do we need to have a pointer to a pointer to head in the insert function for this to work?
3
2710
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h> #include<stdlib.h> int main(void) {
13
2192
by: XXXXXX.working.in.my.blood | last post by:
hi all, i need help with linked lists... the problem is this, "reverse the contents of a singly linked list without using a temporary node"... solution with code will be appreciated...
5
3371
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran the authors example out of the book and quess what, it crashed also, : 0. I ran them both on my...
6
944
by: Baltazar007 | last post by:
Does anyone know how to make quicksort for single linked list without using array? I know that mergesort is maybe better but I need quicksort!! thnx
10
6579
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
5
1638
by: WarmDismalAgony | last post by:
hey, can anyone figure out why this insert function is setting all nodes in the list to have the same item? void insert_list (list_ref list, list_item item) { listnode_ref new = malloc (sizeof (struct listnode)); assert (is_list (list)); assert (new != NULL); new->tag = listnode_tag; new->item = item; if (list->head == NULL) { list->head = new;
0
8631
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 anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
0
8997
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
9568
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...
0
9389
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8257
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
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
6079
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();...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
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
3
2218
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.