Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have some study to do for an upcoming C++ assessment and I have an issue with my deleteNode function. I know the problem lies somewhere within the function as I copied a previous deleteNode function and it worked fine.All of my other pushback/pushfront functions work fine, I'd rather know the problem than just ignore it but I can't spot it for the life of me.
So the linked list is storing Employees, with a name (string) and a salary (double). My deleteNode matches the name of the employee and the string parameter passed in. If anyone can spot my mistake it'll be a great help to my learning!
bool EmployeeList::deleteNode(std::string n){
EmployeeNode *leadptr = head, *trailptr = nullptr;
if (head != nullptr){
if (head->emp.name == n){
head = head->next;
delete leadptr;
return true;
}
else{
while (leadptr != nullptr && leadptr->emp.name != n){
trailptr = leadptr;
leadptr = leadptr->next;
}
if (leadptr = nullptr){
return false;
}
else{
trailptr->next = leadptr->next; //access violation here, leadptr may be null
delete leadptr;
return true;
}
}
}
}
This is an assignment
if (leadptr = nullptr){
Correction would be
if (leadptr == nullptr){
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
I need help please It keeps giving me the same number and not a different one.
Bid LinkedList::Search(string bidId) {
// FIXME (6): Implement search logic
// special case if matching node is the head
// make head point to the next node in the list
//decrease size count
//return
// start at the head of the list
// keep searching until end reached with while loop (next != nullptr
// if the current node matches, return it
// else current node is equal to next node
//return bid
Node* temp = head;
Node* holder = new Node;
holder->bid.bidId = "";
while (temp != nullptr) {
cout << temp->bid.bidId << endl;
if (temp->bid.bidId == bidId) {
return temp->bid;
}
temp = temp->next;
return holder->bid;
}
}
Just remove everything with a holder. And at the end throw an exception when nothing was found. Alternatively return std::optional<Bid>.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have looked through previous answers, but none of them have explained why I am getting this error.
Here is my code with the error. It occurs on "if(pathID==2...)" and every if statement after that.
void add_path(int a,int b, int current_step,int pathID){
if(pathID == 0){
path[current_step] = new step(a,b,"Filled A",path[current_step]);
}
if(pathID == 1)
path[current_step] = new step(a,b,"Filled B",path[current_step]);
}
if(pathID == 2){
path[current_step] = new step(a,b,"Empty A",path[current_step]);
}
if(pathID == 3){
path[current_step] = new step(a,b,"Empty B",path[current_step]);
}
if(pathID == 4){
path[current_step] = new step(a,b,"Pour B to A",path[current_step]);
}
if(pathID == 5){
path[current_step] = new step(a,b,"Pour A to B",path[current_step]);
}
}
All this code is meant to do is add to the linked list at a given position in an array. The pathID is passed in and tells it what action was performed, so we know we know what to add to the linked list.
Later in the program I use that linked list to determine what actions were taken. I still need to make it a doubly linked list so it does not print in reverse, but that's another problem.
You forgot a curly brace after
if(pathID == 1)
Add it and it'll work fine.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
What I'm trying to do is to insert a node with a value to the ith location of a linked list. It has to return true if the insertion was successful, false otherwise. What I'm not able to understand is how to add to the start of the list. What I tries did not insert it at all. Where is the problem?
bool insertat(struct Node*head, int value,int loc){
Node *q = new Node;
q->data=value;
if(loc>list_length(head)){
insertlast(head,value);
return true;
}
else if(loc<=0){
cout<<"invalid location";
return false;
}
else if(loc==1){
q->next=head;
head=q;
return true;
}
else{
int i=1;
Node*p =head;
while( i<loc-1){
p=p->next;
i++;
}
q->next=p->next;
p->next=q;
return true;
}
}
This is because you are passing pointer by value. So, changes done to head in this function are lost.
Rather change your function to:-
bool insertat(struct Node**head, int value,int loc)
and pass the address of head.
OR
bool insertat(struct Node*&head, int value,int loc)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
this is my height function in bst. cpp
int IntBinaryTree::getHeight(TreeNode * nodePtr)
{
if(nodePtr = NULL)
return 0;
else
return (max(1+getHeight(nodePtr->left), 1+getHeight(nodePtr->right)));
}
When I call it in main(). I got an error.
this is my main()
int main {
IntBinaryTree tree;
....
tree. getHeight();
return 0;
}
You didn't say what error, but looks like changing:
if(nodePtr = NULL)
to
if(nodePtr == NULL)
^^
is what you need.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
All, I'm creating a function that would remove the tail from a linked list. And My function works fine for just one iteration but it doesn't for subsequent ones.
Could anybody shed some light?
Thanks
int List::removeAtTail(){
if(head == NULL)
{
cout << "Node cannot be deleted from an empty linkedList" << endl;
}
if(curr->next= NULL)
{
curr->next=curr;
}
return 0;
}
And also, if I would like to return the element that I deleted, how would I go around at doing that?
There are many ways this can be done, following is one:
int List::removeAtTail(){
while(curr != NULL) {
if(curr->next == NULL) { // depending on your implementation you might use Tail
int temp = *curr;
delete curr;
return temp;
}
curr = curr->next;
}
return 0;
}
Notice how we iterate through the list until we find the last item. At that point we store it in a temporary variable before freeing the memory. Lastly we return the value stored in the temporary variable.