-2

I have a data structure (Circular Doubly Linked List) of N integers. I have to modify it by minimising some quantity. In some cases of arrangements of numbers, I will have to check whether the quantity is minimised by checking the modified data structure in two or more cases. I would like to know if , my attempt is good enough and if any better algorithm is available for those cases. I am not explicitly stating the problem as I would like to solve it myself but for the sake of debugging, we have to take two adjacent elements of the list which have minimum sum and replace them with the sum. We have to calculate the sum of the sums in each stage. (N to 1)

I have created two functions 2 functions, : mod() and demod(), to modify and demodify the list. In those cases, I will mod() the structure for case 1 and store quantity1, demod() it, then mod() it for case 2, and store quantity2. Then, I will select the mod() for that case which has least quantity. index and temp are two pointers where I have to evaluate the quantities. The edited code is given below.

    struct Node {
      long long int data;
      struct Node * prev;
      struct Node * next;
    };
    void mod(struct Node * index, long long int val) {
      struct Node * temp1 = index -> next;
      index -> data = val;
      index -> next = (index -> next) -> next;
      ((index -> next) -> next) -> prev = index;
      //free(temp1);
    }

    void demod(struct Node * index ,long long int a){
        long long int b = index->data - a;
        index->data = a;
        struct Node * new_Node = new Node;
         new_Node->data = b;
        new_Node -> next = index -> next;
        index->next = new_Node;
        new_Node->prev = index;
        (new_Node->next)->prev = new_Node;
    }
long long int value(struct Node * start, int length) {
  long long int val; 
    struct Node * index = start->prev;
    val = f(start -> prev);
    struct Node * temp = start;
    while (temp -> next != start) {

      if (val > f(temp)) {
        index = temp;
        val = f(temp);
      }
      temp = temp -> next;
    }
    cout << "val : " << val << "--";
    temp = start;
    while (temp -> next != start) {
      if (f(temp)==val && (index -> next == temp)) {
        std::cout << "*##";
        long long int init1 = index -> data;
        //displayList(start);
       // cout << init1 << " ";
        mod(index, val, start);
        cout << "start case 1\n ";
        long long int temp1 = solve(start, length - 1);
        cout << "end case 1\n ";
        demod(index, init1);
        displayList(start);
        mod(temp, val, start);
        displayList(start);
        cout << "start case 2 \n";
        long long int temp2 = solve(start, length - 1);
        cout << "end case 2\n";
        if (temp1 > temp2) {
          mod(temp, val, start);
          return val;
        } else {
          mod(index, val, start);
          return val;
        }
        /*if ((index -> prev) -> data > ((temp -> next) -> next) -> data) {
            std::cout << "*";
          index = index -> next;
        }*/
      }
      temp = temp -> next;
    }
   // cout << "index data " << index -> data << "\n";
    mod(index, val, start);

    return val;

}

... Full Code (162 lines) https://drive.google.com/open?id=1oD1pEr3_seX6kIzErpRG-Xu3s_95UVBj The code now has some corner cases that I have to correct and I have the basic problem of using the data structure again and again.

Sarthak Rout
  • 137
  • 2
  • 7
  • 3
    Welcome to Stack Overflow! From your question I cannot determine which problem you are trying to solve, and all the list manipulation in your code is drowning out the actual code that attempts to solve it. It would help if you 1) explained the problem better, 2) provided a small input/output pair that demonstrates what this code should do (and how), 3) formatted your code – Botje Jul 11 '19 at 11:18
  • 1
    Please provide a minimal example and try to stick to the "rules" of asking a good question: https://stackoverflow.com/help/how-to-ask – Ventu Jul 11 '19 at 11:20
  • Have you considered using STL containers and passing copies of your data structure to recursive invocations? Explicitly modifying and restoring changes seems like an unwanted complication. – Botje Jul 11 '19 at 11:33
  • Botje, I dont know how to copy one doubly linked circular list easily by using STL or something. Can you suggest some STL containers? – Sarthak Rout Jul 11 '19 at 11:40
  • 2
    Keep it simple and just use `std::vector` or `std::list`. You can always specialize and optimize after you get it to work. – Botje Jul 11 '19 at 11:46
  • Since the algorithm makes no sense I suspect this is some homework designed to stress test a Circular Doubly Linked List implementation. Using an STL container probably isn't an option. Also passing copies would be unwise for large lists. Modifying and restoring is usually the better (even though ugly) approach. – Goswin von Brederlow Jul 11 '19 at 11:57
  • If you are mixing `printf()` and `std::cout <<` as in the function `displayList()`, the `ios_base::sync_with_stdio(false)` is a really bad idea. – Armali Jul 11 '19 at 11:58
  • @SarthakRout: You say the result is 843 instead of 173. But then you give a step by step example why it should be 173. What does your code do? Add statements to print each step to see the first step where it diverges from your expected result. Given Johns answer I expect the first to give a different list than expected and then obviously it goes downhill from there. – Goswin von Brederlow Jul 11 '19 at 12:00
  • I think I got my mistake.. But, it is essentially in using mod() and demod(). Can anyone just tell me how to restore the modified structure as in I could store an copy of it somewhere else. I can use stl and I can copy node by node but it would be ugly. – Sarthak Rout Jul 11 '19 at 12:01
  • Armali, that was one bug I was getting. I will try to remove it. – Sarthak Rout Jul 11 '19 at 12:08
  • Goswin, if you see my full code, I generally try to debug that way. This time I thought I may be missing some STL container or some algorithm to do this better. And no, this is no homework... my friend gave me this as a problem. – Sarthak Rout Jul 11 '19 at 12:14

2 Answers2

0

So I'm somewhat struggling to follow your question, but mod is supposed to change the value of index and remove the next node after index from the list?

If so then this is bugged

index->next = index->next->next;
index->next->next->prev = index;

What you're forgetting is that you change that value of index->next->next in the first statement, but then your second statement tries to use the old value.

It should be this (my preference)

index->next = index->next->next;
index->next->prev = index;

or this (switching the order of the two statements)

index->next->next->prev = index;
index->next = index->next->next;

I've little doubt there are more bugs in the rest of the code. This kind of pointer heavy code is extremely tricky to get right.

You also have commented out the bugged code free(temp1);. If you allocate memory with new it must be deallocated with delete. If you allocate memory with new[] it must be deallocated with delete[]. If you allocate memory with malloc it must be deallocated with free. Don't mix these up, they are not equivalent.

john
  • 85,011
  • 4
  • 57
  • 81
  • There are still more mistakes. I will implement them myself somehow. You see, the solve() essentially modifies the structure many times and demod() can't undo it as it works for one step only. – Sarthak Rout Jul 11 '19 at 12:06
0

There are still more mistakes.

That's right. The next one (after the one john pointed out) is in the line

        demod(index, init1);displayList(start);  mod(temp, val); displayList(start);

since the node temp has been unlinked from the list (though not deleted) by mod() before, it is at this point no longer a correctly linked member of the list and cannot be used as such without being linked in again. One way to do so would be to pass temp to demod() and reinsert it into the list instead of allocating struct Node * new_Node = new Node; there.

Armali
  • 18,255
  • 14
  • 57
  • 171