# 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 * newNode;
node * temp;

{
/*if head is null, initialize a new node
set data = 1 for head;
*/
if(head == NULL && pos == 0)
{
newNode = new node;
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 = temp->next;

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

void Print() {
{
}
}

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

std::cin >> input;
switch ( input ) {
case 1:
Print();
break;
default:
break;
}
std::cin.get();
}
return 0;
}
```

```
+3

source to share

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

void setNode(int pos, int value) {
}
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() {
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;
}
```

```

• 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