Reputation: 3114
Hi I have a question that :
for example I have a linked list that has 4 elements. [1,4,2,7]
and their index will be 0,1,2,3.
when I remove the third element which is "2"
and it index is 2
,the index of fourth element will be 2
,I mean that the index of "7"
will be 2
? my problem is because of the code bellow.
with doubly linked list we can write such a this code:
(p1--> next) = p3;(p3-->prev)=p1;delete p2;p1 = (p1.prev);
how can i write it for linked list? thanks
Upvotes: 1
Views: 171
Reputation: 533510
in java you just have the following for a linked list. You don't need to/can't delete a node.
p1.next = p3;
Note: you would only do this if it is homework. In the real world, you should use the builtin, well understood and tested classes.
Upvotes: 1