I am writing a linked list using a vector (and I know that I should not use vector) I am trying to apply a task to insert a node to x
and position all the elements after x
, but some For reasons it basically takes the element present on x
and all subsequent elements with this value The breakfast.
This is the function with which I am having problems:
// X index Enter element on zero linkedlist: insertAt (int x, int data) {node * TempNode = new node (); Node * moves = evolution [x]; Node * ignore = evolution [x + 1]; Node * previous node = vikilist [x - 1]; If (x == count) {push_back (tempNode, data); Return; } And {count ++; (Int i = 0; i Previous = previous node; TempNode-> Next = next node; TempNode-> Id = x + 1; VecList [x] = tempNode; VecList [x - 1] - & gt; Next = tempNode; // In the node, the node of the previous node adds the /} node, but instead of the organ node,
this position holds the value passed in x
I think my problem is with the transfer of elements after the x
.
When I linked list .inertAt (2, 50); Calling on:
10, 20, 50, 30, 30
, but expected: 10, 20, 50, 30, 40
.
Definition of node
Structure node {node * Previous; Node * next; Int id; Int data; };
The problem is your loop:
for (Int i = 0; i & lt; getSize (); i ++) {vecList [x + 1] - & gt; Next = Evaluate [x] - & gt; next; // 'Temp- & gt; Next 'K' Temp 'vecList [x + 1] - & gt; Data = vecList [x] - & gt; Tranfer the data address; If (vecList [x] == NULL) {break;}}
you are running again on i
, but anything in the loop actually < Code> i . So you're just doing the same operation getSize ()
times I think you want vecList [i + 1]
to veclist [i]
Wanted to Also the lower bound of the loop should not be 0
, it should be x
. For more clarity, the name of that variable should probably be pos
or something like that.
Whenever you are referencing vecList [x + 1]
or vecList [x - 1]
. What if x
is 0 or vecList.size () - 1
? You will point to an undefined object.
Comments
Post a Comment