Clean sequences from a LinkedList











up vote
-1
down vote

favorite












Edited
I have to remove from a LinkedList sequences which their sum equals to 5.



for example for this Linked list: {3,2, 1, 4, 10,3,3,2,1} I get {10, 3, 3, 2, 1}. I dont really know why it's like that, it should remove 3,2 also but it dosent.



void LinkedList::cleanFive(LinkedList& list)
{
head = list.GetHead();

Node* end = head;
Node* headd = head;
Node* flag = head;
Node* curr = head;

int sum = 0;


while (end) {
sum += end->data;

if (sum < 5) {
end = end->next;
}
if (sum == 5) {
end = end->next;
headd = end;
flag = headd;
head = flag;
sum = 0;
}
if (sum > 5) {
flag = head;
curr = end;
end = end->next;
sum = 0;
}
}


}










share|improve this question




















  • 1




    Why does this take a parameter and why does it take over the parameter's head node? Shouldn't it remove such a sequence from itself?
    – molbdnilo
    Nov 22 at 14:56










  • The best tools for fixing (and creating) pointer-related code is pen(cil) and paper. Draw your list and figure out what should happen. Then compare that to what does happen.
    – molbdnilo
    Nov 22 at 15:00










  • In your example, you try 3 + 4, it exceeds 5, whereupon you start counting again from 1 and miss 4 + 1 sequence. You need to advance curr by one element, not all the way to the end; and at the same time, subtract curr->data from sum to account for the sequence getting shorter.
    – Igor Tandetnik
    Nov 22 at 15:09








  • 1




    What is the expected output in case of {3, 4, 3, 2, 1, 2, 5}, {}?
    – Bob__
    Nov 22 at 15:16










  • @Bob__ in this particular case: { }
    – Benjamin Yakobi
    Nov 22 at 16:15















up vote
-1
down vote

favorite












Edited
I have to remove from a LinkedList sequences which their sum equals to 5.



for example for this Linked list: {3,2, 1, 4, 10,3,3,2,1} I get {10, 3, 3, 2, 1}. I dont really know why it's like that, it should remove 3,2 also but it dosent.



void LinkedList::cleanFive(LinkedList& list)
{
head = list.GetHead();

Node* end = head;
Node* headd = head;
Node* flag = head;
Node* curr = head;

int sum = 0;


while (end) {
sum += end->data;

if (sum < 5) {
end = end->next;
}
if (sum == 5) {
end = end->next;
headd = end;
flag = headd;
head = flag;
sum = 0;
}
if (sum > 5) {
flag = head;
curr = end;
end = end->next;
sum = 0;
}
}


}










share|improve this question




















  • 1




    Why does this take a parameter and why does it take over the parameter's head node? Shouldn't it remove such a sequence from itself?
    – molbdnilo
    Nov 22 at 14:56










  • The best tools for fixing (and creating) pointer-related code is pen(cil) and paper. Draw your list and figure out what should happen. Then compare that to what does happen.
    – molbdnilo
    Nov 22 at 15:00










  • In your example, you try 3 + 4, it exceeds 5, whereupon you start counting again from 1 and miss 4 + 1 sequence. You need to advance curr by one element, not all the way to the end; and at the same time, subtract curr->data from sum to account for the sequence getting shorter.
    – Igor Tandetnik
    Nov 22 at 15:09








  • 1




    What is the expected output in case of {3, 4, 3, 2, 1, 2, 5}, {}?
    – Bob__
    Nov 22 at 15:16










  • @Bob__ in this particular case: { }
    – Benjamin Yakobi
    Nov 22 at 16:15













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Edited
I have to remove from a LinkedList sequences which their sum equals to 5.



for example for this Linked list: {3,2, 1, 4, 10,3,3,2,1} I get {10, 3, 3, 2, 1}. I dont really know why it's like that, it should remove 3,2 also but it dosent.



void LinkedList::cleanFive(LinkedList& list)
{
head = list.GetHead();

Node* end = head;
Node* headd = head;
Node* flag = head;
Node* curr = head;

int sum = 0;


while (end) {
sum += end->data;

if (sum < 5) {
end = end->next;
}
if (sum == 5) {
end = end->next;
headd = end;
flag = headd;
head = flag;
sum = 0;
}
if (sum > 5) {
flag = head;
curr = end;
end = end->next;
sum = 0;
}
}


}










share|improve this question















Edited
I have to remove from a LinkedList sequences which their sum equals to 5.



for example for this Linked list: {3,2, 1, 4, 10,3,3,2,1} I get {10, 3, 3, 2, 1}. I dont really know why it's like that, it should remove 3,2 also but it dosent.



void LinkedList::cleanFive(LinkedList& list)
{
head = list.GetHead();

Node* end = head;
Node* headd = head;
Node* flag = head;
Node* curr = head;

int sum = 0;


while (end) {
sum += end->data;

if (sum < 5) {
end = end->next;
}
if (sum == 5) {
end = end->next;
headd = end;
flag = headd;
head = flag;
sum = 0;
}
if (sum > 5) {
flag = head;
curr = end;
end = end->next;
sum = 0;
}
}


}







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 17:10

























asked Nov 22 at 14:48









Benjamin Yakobi

525




525








  • 1




    Why does this take a parameter and why does it take over the parameter's head node? Shouldn't it remove such a sequence from itself?
    – molbdnilo
    Nov 22 at 14:56










  • The best tools for fixing (and creating) pointer-related code is pen(cil) and paper. Draw your list and figure out what should happen. Then compare that to what does happen.
    – molbdnilo
    Nov 22 at 15:00










  • In your example, you try 3 + 4, it exceeds 5, whereupon you start counting again from 1 and miss 4 + 1 sequence. You need to advance curr by one element, not all the way to the end; and at the same time, subtract curr->data from sum to account for the sequence getting shorter.
    – Igor Tandetnik
    Nov 22 at 15:09








  • 1




    What is the expected output in case of {3, 4, 3, 2, 1, 2, 5}, {}?
    – Bob__
    Nov 22 at 15:16










  • @Bob__ in this particular case: { }
    – Benjamin Yakobi
    Nov 22 at 16:15














  • 1




    Why does this take a parameter and why does it take over the parameter's head node? Shouldn't it remove such a sequence from itself?
    – molbdnilo
    Nov 22 at 14:56










  • The best tools for fixing (and creating) pointer-related code is pen(cil) and paper. Draw your list and figure out what should happen. Then compare that to what does happen.
    – molbdnilo
    Nov 22 at 15:00










  • In your example, you try 3 + 4, it exceeds 5, whereupon you start counting again from 1 and miss 4 + 1 sequence. You need to advance curr by one element, not all the way to the end; and at the same time, subtract curr->data from sum to account for the sequence getting shorter.
    – Igor Tandetnik
    Nov 22 at 15:09








  • 1




    What is the expected output in case of {3, 4, 3, 2, 1, 2, 5}, {}?
    – Bob__
    Nov 22 at 15:16










  • @Bob__ in this particular case: { }
    – Benjamin Yakobi
    Nov 22 at 16:15








1




1




Why does this take a parameter and why does it take over the parameter's head node? Shouldn't it remove such a sequence from itself?
– molbdnilo
Nov 22 at 14:56




Why does this take a parameter and why does it take over the parameter's head node? Shouldn't it remove such a sequence from itself?
– molbdnilo
Nov 22 at 14:56












The best tools for fixing (and creating) pointer-related code is pen(cil) and paper. Draw your list and figure out what should happen. Then compare that to what does happen.
– molbdnilo
Nov 22 at 15:00




The best tools for fixing (and creating) pointer-related code is pen(cil) and paper. Draw your list and figure out what should happen. Then compare that to what does happen.
– molbdnilo
Nov 22 at 15:00












In your example, you try 3 + 4, it exceeds 5, whereupon you start counting again from 1 and miss 4 + 1 sequence. You need to advance curr by one element, not all the way to the end; and at the same time, subtract curr->data from sum to account for the sequence getting shorter.
– Igor Tandetnik
Nov 22 at 15:09






In your example, you try 3 + 4, it exceeds 5, whereupon you start counting again from 1 and miss 4 + 1 sequence. You need to advance curr by one element, not all the way to the end; and at the same time, subtract curr->data from sum to account for the sequence getting shorter.
– Igor Tandetnik
Nov 22 at 15:09






1




1




What is the expected output in case of {3, 4, 3, 2, 1, 2, 5}, {}?
– Bob__
Nov 22 at 15:16




What is the expected output in case of {3, 4, 3, 2, 1, 2, 5}, {}?
– Bob__
Nov 22 at 15:16












@Bob__ in this particular case: { }
– Benjamin Yakobi
Nov 22 at 16:15




@Bob__ in this particular case: { }
– Benjamin Yakobi
Nov 22 at 16:15












1 Answer
1






active

oldest

votes

















up vote
1
down vote













curr = head;
end = head;
currprev = nullptr;

std::vector<Node*> flag;
while(end) {
sum += end->data;
if (sum < 5) {
flag.push_back(end);
end = end->next;
}

if (sum > 5) {
currprev = curr;
curr = curr->next;
end = curr;
sum = 0;
empty_flag()
}

if (sum == 5) {
curr = end->next;
end = curr;
sum = 0;
delete_nodes_from_flag_from_linked_list()
empty_flag()
}


}



Here is a possible solution. flag is in this case a vector of nodes which are saved until the sum is proven to be 5 or greater as 5. With flag there are two operations which need to be made: empy_flag() - deletes all entries from the vector and delete_nodes_from_flag_from_linked_list() - that deletes the elements from flag from the linked list.



Code for delete_nodes_from_flag_from_linked_list() should be as follows:



auto it = flag.end();
it--;
Node* last = *it;

if (currprev)
currprev->next = last->next;
else
head = last->next

for (auto f : flag)
delete f;


where currprev is the element just before curr. You have to keep track of this element from the beginning, unless your linked list elements keep pointers to previous elements. I am updating the code from above. head is the start of the list.






share|improve this answer























  • how do I implement this? delete_nodes_from_flag_from_linked_list()
    – Benjamin Yakobi
    Nov 22 at 16:27








  • 1




    Please see revised anwer.
    – Cristi
    Nov 22 at 16:53










  • Could you please mark as anwer if it is a correct answer ?
    – Cristi
    Nov 23 at 7:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433429%2fclean-sequences-from-a-linkedlist%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













curr = head;
end = head;
currprev = nullptr;

std::vector<Node*> flag;
while(end) {
sum += end->data;
if (sum < 5) {
flag.push_back(end);
end = end->next;
}

if (sum > 5) {
currprev = curr;
curr = curr->next;
end = curr;
sum = 0;
empty_flag()
}

if (sum == 5) {
curr = end->next;
end = curr;
sum = 0;
delete_nodes_from_flag_from_linked_list()
empty_flag()
}


}



Here is a possible solution. flag is in this case a vector of nodes which are saved until the sum is proven to be 5 or greater as 5. With flag there are two operations which need to be made: empy_flag() - deletes all entries from the vector and delete_nodes_from_flag_from_linked_list() - that deletes the elements from flag from the linked list.



Code for delete_nodes_from_flag_from_linked_list() should be as follows:



auto it = flag.end();
it--;
Node* last = *it;

if (currprev)
currprev->next = last->next;
else
head = last->next

for (auto f : flag)
delete f;


where currprev is the element just before curr. You have to keep track of this element from the beginning, unless your linked list elements keep pointers to previous elements. I am updating the code from above. head is the start of the list.






share|improve this answer























  • how do I implement this? delete_nodes_from_flag_from_linked_list()
    – Benjamin Yakobi
    Nov 22 at 16:27








  • 1




    Please see revised anwer.
    – Cristi
    Nov 22 at 16:53










  • Could you please mark as anwer if it is a correct answer ?
    – Cristi
    Nov 23 at 7:30















up vote
1
down vote













curr = head;
end = head;
currprev = nullptr;

std::vector<Node*> flag;
while(end) {
sum += end->data;
if (sum < 5) {
flag.push_back(end);
end = end->next;
}

if (sum > 5) {
currprev = curr;
curr = curr->next;
end = curr;
sum = 0;
empty_flag()
}

if (sum == 5) {
curr = end->next;
end = curr;
sum = 0;
delete_nodes_from_flag_from_linked_list()
empty_flag()
}


}



Here is a possible solution. flag is in this case a vector of nodes which are saved until the sum is proven to be 5 or greater as 5. With flag there are two operations which need to be made: empy_flag() - deletes all entries from the vector and delete_nodes_from_flag_from_linked_list() - that deletes the elements from flag from the linked list.



Code for delete_nodes_from_flag_from_linked_list() should be as follows:



auto it = flag.end();
it--;
Node* last = *it;

if (currprev)
currprev->next = last->next;
else
head = last->next

for (auto f : flag)
delete f;


where currprev is the element just before curr. You have to keep track of this element from the beginning, unless your linked list elements keep pointers to previous elements. I am updating the code from above. head is the start of the list.






share|improve this answer























  • how do I implement this? delete_nodes_from_flag_from_linked_list()
    – Benjamin Yakobi
    Nov 22 at 16:27








  • 1




    Please see revised anwer.
    – Cristi
    Nov 22 at 16:53










  • Could you please mark as anwer if it is a correct answer ?
    – Cristi
    Nov 23 at 7:30













up vote
1
down vote










up vote
1
down vote









curr = head;
end = head;
currprev = nullptr;

std::vector<Node*> flag;
while(end) {
sum += end->data;
if (sum < 5) {
flag.push_back(end);
end = end->next;
}

if (sum > 5) {
currprev = curr;
curr = curr->next;
end = curr;
sum = 0;
empty_flag()
}

if (sum == 5) {
curr = end->next;
end = curr;
sum = 0;
delete_nodes_from_flag_from_linked_list()
empty_flag()
}


}



Here is a possible solution. flag is in this case a vector of nodes which are saved until the sum is proven to be 5 or greater as 5. With flag there are two operations which need to be made: empy_flag() - deletes all entries from the vector and delete_nodes_from_flag_from_linked_list() - that deletes the elements from flag from the linked list.



Code for delete_nodes_from_flag_from_linked_list() should be as follows:



auto it = flag.end();
it--;
Node* last = *it;

if (currprev)
currprev->next = last->next;
else
head = last->next

for (auto f : flag)
delete f;


where currprev is the element just before curr. You have to keep track of this element from the beginning, unless your linked list elements keep pointers to previous elements. I am updating the code from above. head is the start of the list.






share|improve this answer














curr = head;
end = head;
currprev = nullptr;

std::vector<Node*> flag;
while(end) {
sum += end->data;
if (sum < 5) {
flag.push_back(end);
end = end->next;
}

if (sum > 5) {
currprev = curr;
curr = curr->next;
end = curr;
sum = 0;
empty_flag()
}

if (sum == 5) {
curr = end->next;
end = curr;
sum = 0;
delete_nodes_from_flag_from_linked_list()
empty_flag()
}


}



Here is a possible solution. flag is in this case a vector of nodes which are saved until the sum is proven to be 5 or greater as 5. With flag there are two operations which need to be made: empy_flag() - deletes all entries from the vector and delete_nodes_from_flag_from_linked_list() - that deletes the elements from flag from the linked list.



Code for delete_nodes_from_flag_from_linked_list() should be as follows:



auto it = flag.end();
it--;
Node* last = *it;

if (currprev)
currprev->next = last->next;
else
head = last->next

for (auto f : flag)
delete f;


where currprev is the element just before curr. You have to keep track of this element from the beginning, unless your linked list elements keep pointers to previous elements. I am updating the code from above. head is the start of the list.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 18:08

























answered Nov 22 at 15:12









Cristi

1901315




1901315












  • how do I implement this? delete_nodes_from_flag_from_linked_list()
    – Benjamin Yakobi
    Nov 22 at 16:27








  • 1




    Please see revised anwer.
    – Cristi
    Nov 22 at 16:53










  • Could you please mark as anwer if it is a correct answer ?
    – Cristi
    Nov 23 at 7:30


















  • how do I implement this? delete_nodes_from_flag_from_linked_list()
    – Benjamin Yakobi
    Nov 22 at 16:27








  • 1




    Please see revised anwer.
    – Cristi
    Nov 22 at 16:53










  • Could you please mark as anwer if it is a correct answer ?
    – Cristi
    Nov 23 at 7:30
















how do I implement this? delete_nodes_from_flag_from_linked_list()
– Benjamin Yakobi
Nov 22 at 16:27






how do I implement this? delete_nodes_from_flag_from_linked_list()
– Benjamin Yakobi
Nov 22 at 16:27






1




1




Please see revised anwer.
– Cristi
Nov 22 at 16:53




Please see revised anwer.
– Cristi
Nov 22 at 16:53












Could you please mark as anwer if it is a correct answer ?
– Cristi
Nov 23 at 7:30




Could you please mark as anwer if it is a correct answer ?
– Cristi
Nov 23 at 7:30


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433429%2fclean-sequences-from-a-linkedlist%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu