I want to add a node at ith position.

I added a head node and a node at position 4, 5 and 3 in sequence with value of
1,2,3,4 respectively.

I also have a print function to print the values of the nodes.

But my print function only prints the value of the head node (1) but not the values of rest of the nodes.

my desired output as follows

it will be something like this

add head node, data = 1
add node at position 4 = 2
add node at position 5 = 3
add node at position 3 = 4

and the output will be

1>4>2>3 = values of head node, node at 3 position, node at 4 position and node at 5 position

and if I add node at position 2 = 6

the output will become
1>6>4>2>3

this is what I have tried

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>


typedef struct node {
    int data;
    node *next;
}nodeType;

node *headNode()
{
    nodeType * head = NULL;
    nodeType * temp = new nodeType;
    temp->data= 1;

    if(head == NULL)
    {
        head = temp;
    }
    return head;
}

node *addNode(int pos, int value, node *head)
{
    nodeType *n = new nodeType;
    n->data = value;
    n->next = NULL;

    if(head != NULL)
    {
        int dist = 1;
        node *trans = head;
        while(trans != NULL)
        {
            if(dist == pos)
            {
                n->next = trans->next;
                trans->next= n;
                return head;
            }
            trans = trans->next;
            dist++;
        }
    }
    return n;
}

void printall(node *head){

    nodeType *temp =head;

    if(temp==NULL){
        std::cout << "\n\n\tError";
    } // if
    else{
        while(temp!=NULL){
            std::cout << temp->data << " ";
            temp=temp->next;
        }
    }
}

int main()
{
    node * head  = headNode();
    addNode(4, 2, head);
    addNode(5, 3, head);
    addNode(3, 4, head);
    printall(head);

}

I think you are forgetting you are coding in C++ ?

You may wish to try something like this ...

// addNodesIthPosition.cpp //  // 2015-05-11 //


#include <iostream>

typedef int MyType;


struct Node
{
    MyType data;
    Node* next;

    // default ctor...
    Node() : data(MyType()), next(0) {}
    // ctor's
    Node( const MyType& mt ) : data(mt), next(0) {}
    Node( const MyType& mt, Node* nxt ) : data(mt), next(nxt) {}
} ;

struct List
{
    Node* head;
    Node* tail;
    size_t len;

    // default ctor...
    List() : head(0), tail(0), len(0) {}
    ~List() { clear(); }

    void clear()
    {
        for( ; head != 0 ;  )
        {
            Node* cur = head;
            head = head->next;
            delete cur;
        }
        tail = 0;
        len = 0;
    }

    // copy ctor ...
    List( const List& lst )
    {
        head = tail = 0;
        len = 0;
        for( Node* cur = lst.head; cur != 0; cur = cur->next )
            push_back( cur->data );
    }
    // overloaded assignment ...
    List& operator = ( const List& lst )
    {
        if( this != &lst )
        {
            clear();
            for( Node* cur = lst.head; cur != 0; cur = cur->next )
                push_back( cur->data );
        }
        return *this;
    }

    void push_front( const MyType& mt )
    {
        if( len )
            head = new Node( mt, head );
        else
            tail = head = new Node( mt );
        ++len;
    }
    void push_back( const MyType& mt )
    {
        if( len )
        {
            tail->next = new Node( mt );
            tail = tail->next;
        }
        else
            tail = head = new Node( mt );
        ++len;
    }
    void insertAt( const MyType& mt, size_t i )
    {
        if( i <= len )
        {
            if( i == 0 ) push_front(mt);
            else if( i == len ) push_back(mt);
            else // at least 3 Nodes exit already ... //
            {
                Node* prev = head;
                for( size_t j = 1; j < i; ++ j )
                     prev = prev->next;
                prev->next = new Node(mt, prev->next->next);
            }
        }
        else
            std::cerr << "\nERROR! Index out of bounds, so NOT inserted ...\n";
    }

    void print( char endChr = '\n' ) const
    {
        for( Node* cur = head; cur != 0; cur = cur->next )
            std::cout << cur->data << endChr;
    }

} ;



int main()
{
    List myLst; // call ctor to construct an empty list //

    myLst.push_front(1); // pos 1, index 0
    myLst.insertAt(6, 1); // pos 2, index 1
    myLst.insertAt(4, 2); // pos 3, index 2
    myLst.insertAt(2, 3); // pos 4, index 3
    myLst.insertAt(3, 4); // pos 5, index 4

    myLst.print();

}

Note: this quickly coded example has not been fully tested, thus there may be bugs you need to fix?

Edit: (fixed comment)

void insertAt( const MyType& mt, size_t i )
{
    if( i <= len )
    {
        if( i == 0 ) push_front(mt);
        else if( i == len ) push_back(mt);
        else // at least 2 Nodes, head & tail exist, this goes in-between //
        {
            Node* prev = head;
            for( size_t j = 1; j < i; ++ j )
                 prev = prev->next;
            prev->next = new Node(mt, prev->next->next);
        }
    }
    else
        std::cerr << "\nERROR! Index out of bounds, so NOT inserted ...\n";
}
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.