Solved

Help with pointers

Posted on 2003-10-22
4
208 Views
Last Modified: 2010-04-02
Hi,

If there is a Dummy head in the list. I need to insert a new node pointed by NewPtr at the beginning of the list.

From what I read, I understood that dummy head is used to get exclude the special cases.
So the solution:

NewPtr->next=head;
Head=NewPtr;

is not applicable, coz that is what is being used for the special case.
What would be a similar code segment for the list that uses dummy head.

I have a solution, but i am not sure if it is right:

Head->Next=NewPtr;
NewPtr->Next=Head->Next;


Can you please advise, and if possible explain.
Thanks.

0
Comment
Question by:Gipsy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9600701
Just a minor change

NewPtr->Next=Head->Next;
Head->Next=NewPtr;

With your code

Head->Next=NewPtr;  // Here you lose the old head->next
NewPtr->Next=Head->Next;  // So you cannot assign it here
0
 
LVL 9

Accepted Solution

by:
tinchos earned 30 total points
ID: 9600825
Just a little more clarification

The idea of your code I believe it was right

The only problem is that the old value of head->next was  being lost.
This means, that after the first line of your code, the pointer to the head of the list was being overwritten, and when you tried to set the next element of the new head of the list, you were assigning it to itself.......

After running this

Head->Next=NewPtr;
NewPtr->Next=Head->Next;

you find NewPtr->Next = NewPtr and I believe that this is not the idea

I believe that you just have to swap the order of the instructions as I previously mentioned
0
 

Author Comment

by:Gipsy
ID: 9600883
Thanks, can you briefly explain what your code does, I am new to pointers and trying to understand how it works. Thanks
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9600937
Well, I believe that this is worth more than 30 points, but that's up to you............ here's my explanation

suppose you have a list with 5 elements

( 1 2 3 4 5 )

and you want to add a new element in the beggining containing 0.

then

before the insertion you have

head->next is pointing to the node containing the 1 (lets call it node1)
node1->next is pointing to the node containing the 2 (lets call it node2)
node2->next is pointing to the node containing the 3 (lets call it node3)

so, in some pseudocode

head->next = node1          // The first element is node1
node1->next = node2
node2->next = node3
and so on

if you want to add a new node containing 0 (lets call it node0 )

then, you want to do

node0->next = node1           // As you want the 0 to be before the 1

and the first element is no longer node1, it is node0, so

head->next=node0

so in the end you have

head->next=node0
node0->next = node1
node1->next = node2
node2->next = node3
and so on

and in this way you represent

( 0 1 2 3 )

I hope its clear enough

any doubts feel free to ask them
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question