Solved

setting value to a pointer of a linklist

Posted on 2004-10-03
3
270 Views
Last Modified: 2010-04-01
I have made the following linklist. How can I create a loop

say for example if I want to set first->next->next=something;

then I want to set first->next->next->next=something;

I hope I am clear enough, what I need to do is replace ->next .. by a loop so if need 3 ->next I just run a for loop 3 times. I am doing it because I need to add a node at the nth position of my list; If the list already has some elements. For example the function addnth which I have tried to implement. The function is generic to add a node at the 2nd position from head. But I need it to be versatile enough to add at any location. However I am unable to do that because of difficulty in moving to first->next.....I even tried using pointer to pointer but was unable to do that.

_______________________________________
struct node
{
   int x;
   node *next;

};


class linklist
{

      node *first;
      int length;
public:
      linklist()
      {
            first= NULL;
            length=0;
      }

                void addlink(int d);
      void display();
      void addnth(int d,int n);

};

void linklist::addnth(int d,int n)

{

      node *current=first;
      node *temp=0;      
      node **temp1=0;
      node *m=0;



      for (int i=1;i<3;i++)
      {
      
            current=current->next;
      }

      temp=current;

                node *newlink=new node;

      newlink->x=d;

      first->next->next=newlink;
      
      /*
      m=first->next->next;
      temp1=&m;
               *temp1=newlink;
               */

    newlink->next=temp;


}
0
Comment
Question by:anshuma
[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
  • 2
3 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 350 total points
ID: 12212895
I think will be no errors. Please advice.

void linklist::addnth(int d,int n)
{
    node *current=first;
    node *newNode= new node;
    newNode->x = d;
 
    if (n==1) {    // special case, first
        newNode->next = first;
       first = newNode;
       return;          
    }

    for (int i=1;i<(n-1);i++) {
       if (current->next)    // this statement prevent outbound
          current=current->next;
    }

    newNode->next = current->next;
    current->next = newNode;            
}
0
 

Author Comment

by:anshuma
ID: 12212923
Great it worked but I was trying to do the same in my code. Could you point me to my folly. How can I solve this with a pointer to pointer approach.

-regards
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12212935
Sorry, I can not undestand why do you want to use pointer to pointer.
I doesn't make much sense to me.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

749 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