-2

I want an explanation for how I can get to the Nth node in the linked list as to insert a node after that Nth node I have this piece of code and the function for inserting a node after the nth node is given as

InsertAfter
I have problem as how to call the function InsertAfter in the driver main function i.e. if i have to go to the node next to the head I may write as head -> next but, is this necessary to write next -> next for the elements to go beyond the second node .
Is there any simple way which help me not to write next for every node to jump to the next node.
For examples --> If I have to insert node after 7, I have to write head-> next-> next but is there any easy way in which there is no need to next again and again.

#include<bits/stdc++.h>
using namespace std;
struct Node{
  int data;
  Node* next;
};
Node* head = NULL;
void insert(int x)
{
  Node* temp = new Node();
  temp -> data = x;
  temp -> next = head;
  head = temp;
}
void display(){
  Node* temp;
  temp = head;
  while(temp != NULL)
  {
    cout<< temp -> data<< " ";
    temp = temp-> next;
  }
}
void insertAtLast(int x)
{
  Node* temp = new Node();
  Node* last = head;
  temp -> data = x;
  temp -> next = NULL;
  if(head == NULL)
  {
    head = temp;
    return;
  }
  while (last -> next != NULL)
  {
    last = last-> next;
  }
  last -> next = temp;
  return;

}
void InsertAfter(Node* prev, int data)
{
  if(prev == NULL)
  {
    cout<< "The previous node cannot be NULL" << endl;
    return;
  }
  Node* temp = new Node();
  temp -> data = data;
  temp -> next = prev -> next;
  prev->next = temp;
}

int main() {
   insert(3);
   insert(1);
   insert(7);
   insert(2);
   insert(9);
   cout<<"The linked list is: \n";
   display();
   cout<< endl;
   insertAtLast(6);
   display();
   std::cout  << '\n';
   InsertAfter(head->,3);
   display();
   return 0;
}
dukeforever
  • 298
  • 1
  • 7
  • 7
    `#include using namespace std;` <-- Don't *EVER* do that. – Jesper Juhl Dec 27 '19 at 13:14
  • I know this.You are requested to answer only what is asked. – dukeforever Dec 27 '19 at 13:16
  • 7
    @dukeforever Then if you do know that, why do you do it? – Storm Dec 27 '19 at 13:20
  • 7
    "You are requested to answer only what is asked." - 1) it wasn't an answer, it was a comment. 2) you don't get to control what comments I make. – Jesper Juhl Dec 27 '19 at 13:34
  • 2
    `Node* head = NULL;` --> `Node* head = nullptr;` is better. Don't use `NULL` in new code. You `new` stuff, but you never `delete` it --> memory leak. And why do you use manual memory management in the first place? Use containers and/or smart pointers instead in modern code. – Jesper Juhl Dec 27 '19 at 13:58

2 Answers2

2

To get to the Nth node, you follow the links, decrementing the counter, until the counter is zero or the end of the list is reached:

Node * at(unsigned int index)
{
  Node * p = head;
  while (p && (index != 0))
  {
    p = p->next;
    --index;
  }
  return p;
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

"how I can get to the Nth node in the linked list as to insert a node after that Nth node" - Traverse the list until you get to the Nth node. Then insert.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70