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;
}
}
}
c++
|
show 2 more comments
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;
}
}
}
c++
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 try3 + 4
, it exceeds 5, whereupon you start counting again from 1 and miss4 + 1
sequence. You need to advancecurr
by one element, not all the way to the end; and at the same time, subtractcurr->data
fromsum
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
|
show 2 more comments
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;
}
}
}
c++
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++
c++
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 try3 + 4
, it exceeds 5, whereupon you start counting again from 1 and miss4 + 1
sequence. You need to advancecurr
by one element, not all the way to the end; and at the same time, subtractcurr->data
fromsum
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
|
show 2 more comments
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 try3 + 4
, it exceeds 5, whereupon you start counting again from 1 and miss4 + 1
sequence. You need to advancecurr
by one element, not all the way to the end; and at the same time, subtractcurr->data
fromsum
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
|
show 2 more comments
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 miss4 + 1
sequence. You need to advancecurr
by one element, not all the way to the end; and at the same time, subtractcurr->data
fromsum
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