B
B
booogabooo2015-06-14 23:26:44
Java
booogabooo, 2015-06-14 23:26:44

How does this algorithm work?

There is such a code for a doubly linked list. We feed the list itself as input, and the element that needs to be waved with the next one.
But on what principle this "wave" happens - I don’t understand

public static Node reverseMyList (Node c,int n) { 
          String temp = "";
          current = new Node("Новый список:");
          count = 0;
          
         while( c != null ) { 
              temp += c.inf+ " "; 
              c = c.next;
          }; 
          
          String[]tt = temp.split(" ");
          for(int i = 0; i < tt.length / 2; i++){
             String y = tt[i];
             tt[i] = tt[tt.length - i - 1];
             tt[tt.length - i - 1] = y;
          }
          n = tt.length - 3 - n;
          while(count != tt.length - 2){
              if(n != count){
                  Node e = new Node (tt[count]); 
                  Node old = current.next; 
                  e.next = old; // добавить элемент
                  current.next = e; 
              }
              if(n == count){
                  Node e = new Node (tt[count + 1]); 
                  Node old = current.next; 
                  e.next = old; 
                  current.next = e; 
                  addNode(current,tt[count]);            
              }
              count++; 
              int tn = 0;
          }
          return current;
  }

the code is shitty - but that's not the point...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Polycarp the Boreman, 2015-06-15
@booogabooo

The above code requires complete, absolute, total, comprehensive and exhaustive refactoring... If you need to "wave" an element with the following, then it is enough:
1. Go through the list to the current element
2. Create a buffer for the data in the current list
3. Copy the data from current element to buffer
4. Copy data from next element to previous
5. Copy data from buffer to next element 6.
(optional) Delete buffer
I consider it redundant.

J
jcmvbkbc, 2015-06-15
@jcmvbkbc

There is such a code for a doubly linked list.

The error is here: this code is for a singly linked list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question