By adding node at i-th position

I want to create a list of links with size 5 and add some nodes at the i-th position.

I will add nodes at a random location, say (0, 5 and 2) of the link list.

this is what it looks like adding a node at position 0.

      0
 +---------+
 |    1    |
 +---------+ --> NULL
 |  next   | 
 +---------+ 

      

this is what it looks like adding a node at position 5.

      0             1             2             3             4
 +---------+   +---------+   +---------+   +---------+   +---------+
 |    1    |   |  Empty  |   |  Empty  |   |  Empty  |   |    2    |
 +---------+-------------------------------------------->+---------+-->NULL
 |  next   |   |  node   |   |  node   |   |  node   |   |  next   |
 +---------+   +---------+   +---------+   +---------+   +---------+

      

so node 1,2,3 is empty and 0 is associated with 4.

this is what it looks like adding a node at position 1.

      0             1             2             3             4 
 +---------+   +---------+   +---------+   +---------+   +---------+
 |    1    |   |    2    |   |  Empty  |   |  Empty  |   |    2    |
 +---------+-->+---------+------------------------------>+---------+-->NULL
 |  next   |   |  next   |   |  node   |   |  node   |   |  next   |
 +---------+   +---------+   +---------+   +---------+   +---------+

      

so node 2,3 is empty and 0 is associated with 1 and 1 is associated with 4.

I tried to implement it but didn't print anything. Please advise. Thank.

#include <iostream>

struct node
{
    int x;
    node *next;
};

node * head = NULL;
node * newNode;
node * temp;

void addNode(int pos, int size)
{
    /*if head is null, initialize a new node
      set data = 1 for head;
    */
    if(head == NULL && pos == 0)
    {
        newNode = new node;
        head = newNode;
        head->x = 1;
        temp = head;
        temp->next=NULL;
    }
    else
    {
        /*
         Adding a node at ith position.
         1. check if the the position is less than the size of the link list.
         2. set the temp position to be 0(head)
         3. use the temp pointer and go to the ith postion.
         4. create new node at ith position.
         5. set data = 2 for the node at ith position.
     */
        if (pos < size)
        {
            for(int i=0; i < size; i++)
            {
                temp = head;
                temp = temp->next;

                if (pos == i)
                {
                    newNode = new node;
                    temp = newNode;
                    temp->x = 2;
                    temp->next = NULL;
                }
            }
        }
    }
}

void Print() {
    while(head->next != NULL)
    {
        std::cout<< head->x << std::endl;
        head=head->next;
    }
}

int main()
{
    int input = 0;
    while (true) {
        std::cout << "1. Add Node and Print " << std::endl;

        std::cin >> input;
        switch ( input ) {
            case 1:
                addNode(0, 5);
                addNode(5, 5);
                addNode(1, 5);
                Print();
                break;
            default:
                std::cout<<"Bad Input";
                break;
        }
        std::cin.get();
    }
    return 0;
}

      

+3


source to share


1 answer


Since I'm rejecting your question, you want to write a function that will set the value of a node to a specified index, and if the index is larger than the current size of the list, it will automatically expand your list. (Correct me if I'm wrong)

It's simple enough. You need to iterate over your list while the counter i

(see code below) is less than the specified position. If the list is smaller than you create node and mark their value as some specific value EMPTY

. When counter is equal to position, set the node value to value

.

#include <iostream>

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

#define EMPTY -1

node * head = NULL;

void setNode(int pos, int value) {
    if(head == NULL) {
        head = new node;
        head->data = EMPTY;
        head->next = NULL;
    }
    node* p = head;
    for(int i = 0; i < pos; i++) {
        if(p->next == NULL) {
            p->next = new node;
            p->next->data = EMPTY;
            p->next->next = NULL;
        }
        p = p->next;
    }
    p->data = value;
}

void print() {
    node* p = head;
    while(p != NULL) {
        std::cout << p->data << " ";
        p = p->next;
    }
    std::cout << std::endl;
}

int main() {
    setNode(0, 1);
    setNode(5, 2);
    setNode(1, 3);
    print();
    return 0;
}

      



Also your code had errors in the print function:

  • You have changed the variable head

    .
  • And the last value of the list was not printed due to a condition in the loop while

    .
0


source







All Articles