It is necessary to change the 2nd and penultimate element of a doubly linked list.

Closed due to the fact that off-topic participants 0xdb , Fat-Zer , Harry , nörbörnën , Kromster 25 May '18 at 12:46 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve a problem "- Harry, nörbörnën
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Find and read the info how to work with doubly linked lists. - nick_n_a
  • And what you use for work with the double-linked list? - nick_n_a
  • What have you read on this issue? Here is sql.ru/forum/727050/... from the top 10 of Google. - nick_n_a
  • It is necessary to draw the elements of the list along with the neighbors on paper and think about how to break, save the links back and forth and then restore them for new items - MBo
  • Here is a simple example of working with a doubly linked list: pastebin.com/mScMkkdy - 0andriy

1 answer 1

the easiest way is to change the data in the elements of the list, if you still need to rearrange the elements, then the task is more complicated ... we need 4 variables where we will store the intermediate pointer data. If the elements are next to each other, then the order of replacing the pointers will be like this (written in Java, but the idea should be clear)

tempMAX.next = tempE.next; tempE.prev = tempMAX.prev; tempMAX.prev = tempE ; tempE.next = tempMAX ; tempMAX.next.prev = tempMAX ; tempE.prev.next = tempE ; tempMAX = tempMAX.prev; tempE = tempMAX.next ; 

if the elements are not adjacent, then the order of changing the pointers differs by the last two lines // copy references into temporary variables

  tempNext = tempE.next ; tempPrev = tempE.prev ; MAXnext = tempMAX.next ; MAXprev = tempMAX.prev ; // присваиваем новые ссылки ячейкам (элементам) tempE.next = MAXnext ; tempE.prev = MAXprev ; tempMAX.next = tempNext ; tempMAX.prev = tempPrev ; // меняем ссылки у соседей tempMAX.next.prev = tempMAX ; tempE.prev.next = tempE ; tempMAX.prev.next = tempMAX ; tempE.next.prev = tempE ; 
  • there is a headline in my list that points to the first element and the last one, that is, the list is looped back, if you have a beginning and end of the list, then you need to provide 2 more options if the elements are at the beginning or at the end. - Alexander Blazeyko