Hello Im quite Confused right now. I am trying to insert a Node in between nodes and it is not being displayed.
Please Help.

#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;

struct Node {
      int DATA;
      Node * Next;

};
Node * head = NULL;
void Display();
void Createnode(int data);
void Removenode(int data);
void Display() 
{
     Node *tmp;
     tmp = head;
     if ( tmp == NULL ) {

          cout << "EMPTY" << endl;
          return ;

     }
     if ( tmp->Next == NULL ) {
          cout << tmp->DATA;
          cout << "->";
          cout << "NULL" << endl;


          }else {
          do {
              cout << tmp->DATA;
              cout << "Last Node";
              tmp = tmp->Next;

          } while ( tmp != NULL );
     cout << "NULL" << endl;
     }

}


void Createnode(int data)

{
    Node *front, *tail, *temp;
    tail = new Node;
    tail->DATA = data;
    tail->Next = NULL;
        if(head == NULL)
        {
        head = tail;
        //tail->Next=front->Next;
        }
    else
    {
        front = head;
        temp=head;
        while(front->Next!=NULL)
            front=tail;
        tail->Next=front->Next;
        tail->Next=front;
        front->Next=NULL;;

    }


}


void Removenode(int data)
{

    Node * curr, *prev;
    curr=head;
    prev=NULL;
    while ( curr != NULL) 
    {
        if (curr->DATA == data)
        {
            if (prev == NULL)
            {       
                curr->Next= prev;
                curr=curr->Next;
                prev=NULL;
                delete head;
            }
            else
            {
                prev->Next=curr->Next;
                prev->Next=NULL;
                //delete curr;  

                curr=prev->Next;
                curr=NULL;
            }
        }
        else
        {
            prev=curr;
            curr=curr->Next;


        }
    }
}



int main() {

    int n[100];
    char choice;

    for(int i=0; i<=50; i++)
    {   
start:  cout << "\n[i]-Insert head Node, \n[d]- Delete head Node" << endl;
    cout << "choice: ";
    cin >> choice;

    switch(choice){
    case 'i':
    case 'I':
              do
          {
            cout<<"Enter a number to be inserted:";
            cin>>n[i];

            if(!isdigit(n[i]))
            {
            Createnode(n[i]);
            cout << "\t\t\tList:";
            i++;
            Display();  
          }else{
            cout << "Enter a Number please: " << endl;}
            goto start;
          }while(isdigit(n[i]));
          break;
    case 'd':
    case 'D':

          for(int x=1; x<10; x++){


            Removenode(n[i]);
            cout << "\t\t\tList:";

        i--;
            Display();

          goto start;
          }
          break;
    default: 
        cout << " Invalid Choice" << endl;
        break;

    }
    }


    system("pause");
    return 0;
}

Here you go.

#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;

class Node {
public:
    int data_;
    Node * next_;
};
Node * head = NULL;
Node * last_node_ptr = NULL;

void Display();
void Createnode(int data_);
void Removenode(int data_);

void Display()
{
    Node *node_ptr = head;

    if (node_ptr == NULL){
        cout << "The list is empty!" << endl;
        return;
    }

    cout << endl;
    while (node_ptr != last_node_ptr){
        if (node_ptr != head) // Don't show head node data
            cout << "Node value is: " << node_ptr->data_ << endl;
        node_ptr = node_ptr->next_;
    };
    if (node_ptr != head) // Don't show head node data
        cout << "Node value is: " << last_node_ptr->data_ << endl;
}

void Createnode(int data_)
{
    Node *n = new Node();
    n->data_ = data_;
    n->next_ = NULL;

    if (head == NULL) {
        head = n;
        last_node_ptr = head;
    }
    else{
        last_node_ptr->next_ = n;
        last_node_ptr = n;
    }
}
void Removenode(int data_)
{
    Node *node_ptr = head;

    if (node_ptr == NULL){
        cout << "The list is empty!" << endl;
        return;
    }

    if (node_ptr == last_node_ptr){
        cout << "We can't remove head node!" << endl;
        return;
    }

    cout << endl;
    while (node_ptr->next_ != last_node_ptr){
        node_ptr = node_ptr->next_;
    };
    delete last_node_ptr;
    last_node_ptr = node_ptr;
}

int main() {
    int n[100];
    char choice;

    Createnode(-1); //<- Root/Head node

    for (int i = 0; i <= 50; i++){
    start: cout << "\n[i]-Insert new Node, \n[d]- Delete last added Node" << endl;
        cout << "choice: ";
        cin >> choice;
        switch (choice){
        case 'i':
        case 'I':
            do
            {
                cout << "Enter a data value to be stored:";
                cin >> n[i];
                if (!isdigit(n[i]))
                {
                    Createnode(n[i]);
                    cout << "List:";
                    i++;
                    Display();
                }
                else{
                    cout << "Enter a Number please: " << endl;
                }
                goto start;
            } while (isdigit(n[i]));
            break;
        case 'd':
        case 'D':
            for (int x = 1; x<10; x++){
                Removenode(n[i]);
                cout << "List:";
                i--;
                Display();
                goto start;
            }
            break;
        default:
            cout << " Invalid Choice" << endl;
            break;
        }
    }
    return 0;
}

I have edited code a little (had no time to check yours for errors)!

Hi, from your code I only see that you are inserting a new node at the end of the list

On your remove node, you are not checking to see if the list is null, which Kristian_2 code shows how. Now if you have to any node your code is close, I would suggest doing the following plus adding the check for when the list is empty

    1.     while ( curr != NULL)   
    2.         {
    3.             if (curr->DATA == data)
    4.             {
    5.              if (prev == NULL) //we are removing the first node of the list
    6.                 {       
    7.                  // get a pointer to the next element of the one to be deleted
    8.                     prev = curr->Next;
    9.                  
    10.                     // delete element
    11.                     curr->Next = NULL;
    12.                     delete curr;
    13.                     
    14.                     // update where the head pointer points to and set prev to NULL
    15.                     head = prev;
    16.                     prev = NULL'
    17.                 }
    18.                 else
    19.                 {
    20.                     prev->Next = curr->Next;
    21.                     //prev->Next=NULL;  --> doing this will lose the link to the next element in the list
    22.                     //delete curr;  
    23.                     curr=prev->Next;
    24.                     //curr=NULL;  --> doing this will set the pointer to null but not free memory
    25.                     delete curr; // here we free memory  
    26.                     curr = NULL;
    27.                 }
    28.             }
    29.             else
    30.             {
    31.                 prev=curr;
    32.                 curr=curr->Next;
    33.             }
    34.         }

In order to insert in between nodes, you follow the same logic as when you remove one with the exception that you already have a node pointer with the data to insert. When you find the position where you want to insert, you set the new node next to the current node and the previous node next to the new node

1.     // adding at index idx with i as a counter
2.     if (i == idx)
3.     {
4.      newNode->Next = currentNode;
5.      previousNode->Next = newNode;
6.      
7.      break;  //exit loop
8.     }

Of course you will have to check in case the user wants to add a new node on a position outside the list

thank you for the corrections!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.