#include <iostream>
#include <cstdlib>
#include <cassert>

using namespace std;

struct node
{
   int info;
   node *link;
};

void insert(node *first, node *here, int x) 
{
   node *temp = new node;
   temp->info = x;
   if (first == NULL)
   {
      first = temp;
   }
   else
   {
      here->link = temp;
   }
}

i want to insert a node containing x to the node after the one pointed by 'here'. I cant seem to get it right though?

I guess this initialization is not enough:

node *temp = new node;
temp->info = x;

you should add "temp->link=NULL;"

oh ok sweet thanks
im still missing something im not sure what tho
linked lists arent my strong point

any ideas anyone?

Here is a good tutorial on linked lists amongst other C++ data structures. Have a look at their implementation of a singly linked list, and try to emulate it (Emulation helps you remember how to do things rather than blindly doing it)...

Hope this helped!

void insert(node *first, node *here, int x) 
{
   node *temp = new node;
   
   temp -> info = x;
   temp -> link = NULL;
   
   if (here -> link == NULL)
   {
      first -> link = temp;
   }
   else
   {
      here -> link = temp;
   }
}

is this right?

Do the following:
1.Make a new node temp and store x in temp->info.
2.Set temp->link = here->link
3.Set here->link = temp
Thats it
As you might have suspected, you don't require first as an argument, hence the definition of insert should loop like: void insert(node *here, int x)

This article has been dead for over six months. Start a new discussion instead.