I have a function:
void initGraph(node_t *node, const int orderedInputNodes[]) {
int index;
for (int i = 0; i < S * 2; i++) {
if (orderedInputNodes[i] == node->number) {
index = i % 2 == 0 ? i + 1 : i - 1;
node_t newNode;
newNode.number = orderedInputNodes[index];
newNode.left = EMPTY;
newNode.right = EMPTY;
if (node->left == EMPTY) {
node->left = &newNode;
}
else if (node->right == EMPTY) {
node->right = &newNode;
}
}
}
.
.
.
}
Everytime I find a corresponding number I create newNode, assign its value to it and pass it either to the left or to the right, however, when the code comes to the part when the right child is being assigned, the left child gets overwritten. How come is that? I thought by stating node_t newNode; a completely new node will be automatically created.
I thought by stating node_t newNode; a completely new node will be automatically created.
Correct. The problem is that the new node can replace the old node. Consider:
for (int j = 0; j < 1000; ++j)
{
int i;
i = 3;
}
You don't think after this loop runs, there's a thousand instances of i still sitting around, do you? Every time this bit of code runs, a new i is created. But every time this bit of code runs, the i from the previous iteration doesn't exist anymore because it goes out of scope.
You have:
{
node_t newNode; // newNode is created here
newNode.number = orderedInputNodes[index];
newNode.left = EMPTY;
newNode.right = EMPTY;
if (node->left == EMPTY) {
node->left = &newNode; // you save a pointer to it here
}
else if (node->right == EMPTY) {
node->right = &newNode;
}
}
// but newNode doesn't exist anymore here
So you've saved a pointer to an object that no longer exists. It winds up pointing to whatever happens to be stored in that memory, likely the next newNode that is created on the next pass in the loop.
Don't store pointers to local objects.
Update: You say you're coding in C++. Why are you using raw pointers then? Do you really want to manually manage the lifetimes of objects? C++ has fantastic tools to make this easy like std::unique_ptr.
Related
I trying to code out the Linked List with priority queue and i encountered some problem.
I have about 7 priority from 1 the most to 7 the least important.
here's my current insert method.
void queue::addToQueueList(int newPriority, double newFare, int custID)
{
node* newnode= new node;
newnode->priority= newPriority;
newnode->fare = newFare;
newnode->cusID = custID;
newnode->next= NULL;
if (isempty())
{
front = back = newnode;
}
else
{
node* temp = front;
if(newnode->priority < temp->priority)
{
newnode->next = front;
front = newnode;
}
else
{
while(newnode->priority < temp->priority)
{
if(temp->next == NULL)
{
break;
temp = temp->next;
}
}
if(temp->next == NULL && newnode->priority < temp->priority)
{
back->next = newnode;
back = newnode;
}
else
{
newnode->next = temp->next;
temp->next = newnode;
}
}
}
}
Invoked as:
qList->addToQueueList(2, 488.88, A);
qList->addToQueueList(1, 388.88, B);
qList->addToQueueList(3, 488.88, C);
Expected result should be :
B, A, C
THe result shows :
B, C, A
Your making this considerably harder than it needs to be. Ultimately you need to walk the list, find the insertion point, remember how you arrived at that insertion point, and wire both your fore and aft pointers appropriately. Also a priority queue has no reason to keep a "back" pointer, so I'm not sure why you have one.
There are a number of ways to do this. First, to make the code cleaner to understand, providing a proper parameterized constructor for node is both trivial and helpful:
struct node
{
int priority;
double fare;
int cusID;
node *next;
node(int p, double f, int id, node *nxt = nullptr)
: priority(p), fare(f), cusID(id), next(nxt)
{
}
};
One you have that, you can go down the road you were apparently trying to navigate, using a pointer-value list walking approach. To do that you need to maintain a previous pointer:
void queue::addToQueueList(int newPriority, double newFare, int custID)
{
node* temp = front, *prev = NULL;
while (temp && temp->priority < newPriority)
{
prev = temp; // remember how we got here
temp = temp->next; // advance to next node
}
// create new node, linking to temp
node *newnode = new node(newPriority, newFair, custID, temp);
// link to previous node or assign as new head, whichever is needed
if (prev != nullptr)
prev->next = newnode;
else
head = newnode;
// though there is no need for a back pointer in a priority queue
// you had one none-the-less, so....
if (!temp)
back = newnode;
}
it is worth noting that this algorithm will insert new arrivals with similar priority at the head of that priority section of the list. I.e. the newest arrivals for a given priority are always at the forefront of that priority's position in the queue. If you want the oldest arrivals of a given priority to be "ahead" of their brethren, you simply need to change this:
while (temp && temp->priority < newPriority)
to this:
while (temp && temp->priority <= newPriority) // note < is now <=
Best of luck.
The comparison in your while loop is wrong. When inserting C newnode->priority == 3 and temp(B)->priority == 1. Thus the while loop is never entered.
Also, the temp = temp->next inside the while loop should be outside (after) the if statement. Otherwise this will be an infinite loop.
Assuming you are correcting these: you will always insert the new element after temp. Be aware of this in your fix of your comparisons. You are likely to add comparisons with temp->next->priority as well.
I agree with Joachim in the comments: step through the code with a debugger. Then you can see the values of the variables and which comparisons produce which results.
The following code is part of my implementation for a class member function that rents lockers and thus creates nodes in a linked list:
void SelfStorageList::rentLocker(Locker e) {
int count = 0;
LockerNode *p = head;
if (isEmpty()) {
head = new LockerNode(e, head);
tail = head;
}
LockerNode *prev = head;
LockerNode *curr = head->next;
for( ; curr != 0 && curr->objLocker.isVip; prev = prev->next, curr = curr->next) {
if(count == 1) {
if (e.isVip) {
if(p->objLocker.isVip) {
LockerNode *p = new LockerNode(e, p->next);
}
else {
LockerNode *p = new LockerNode(e,head);
}
}
//etc...
When I run it, I get two errors that my *p pointers may potentially be uninitialized. I don't think they would pose any sort of run time error issues, but the program will not compile as long as the errors persist. I was wondering what alternative I might have to the current implementation of the
LockerNode *p = new LockerNode(e, p->next);
and
LockerNode *p = new LockerNode(e,head);
lines. All I want them to do is to create new nodes with the passed in info element of e, either before the current listed locker or after, depending on the condition.
Thanks for any help provided! Let me know if it would be useful to post any other particular parts of the code, although it is quite lengthy, so I was hoping to avoid posting it all and isolating the problem into a manageable module.
In this if-else, you are declaring a new pointer p in each block:
if(p->objLocker.isVip) {
LockerNode *p = new LockerNode(e, p->next);
}
else {
LockerNode *p = new LockerNode(e,head);
}
Each one of these is local to the scope. It results in a memory leak, and has no effect on the p from the outer scope.
I've checked the boards and could not find any help with this. I find it easy to implement recursive functions given base and general cases, but this doesn't work the way I do it. I'm supposed to iterate down a list until I reach the tail of a linked list. If the next node is NULL, then I have to store the value at the last node, remove that node, and return the value. So it's similar to a dequeue method, except it's performed recursively. What am I doing wrong?
int LinkedList::removeTailRec(Node *n)
{
// check for the base case(s)
if(n->next == NULL)
{
Node *tmp = new Node();
tmp = n;
int val = n->value;
tmp = NULL;
return val;
}
else
return removeTailRec(n->next);
// else call the recursive method
}
First, I recommend you use nullptr instead of NULL.
Then, onto your code. You're actually not removing anything from your list.
if(n->next == NULL)
{
Node *tmp = new Node();
^^^^^^^^^^
//Useless, and dangerous. This memory is never free'd
tmp = n;
int val = n->value;
tmp = NULL;
^^^^^^^^^^
//You just set a local variable to NULL, you're not deleting anything
return val;
}
If you want to remove the node, you'll have to keep a reference to the previous node (e.g. having a doubly linked list, that is, having a pointer to the next element and a pointer to the previous element in each node, or working on the previous node directly).
Set this previous node's next to nullptr, store the node's value and then delete the Node pointer.
One way to do this is to work with the pointer to the next node :
int LinkedList::removeTailRec(Node *n)
{
//EDIT: Adding a check for n validity
if(!n){
//Here, you should have a way of detecting
//a call to your method with a null pointer
return 0;
}
Node* nextNode = n->next;
// check for the base case(s)
if(nextNode->next == nullptr)
{
//Get the next node value
int val = nextNode->value;
//Set the current node next member to nullptr
n->next = nullptr;
//Free the last node
delete nextNode;
return val;
}
else{
return removeTailRec(n->next);
}
// else call the recursive method
}
You are storing the result but not deleting it from linked list. You can return result in another variable (pointer : result).
Node* getTail(Node *n,int *result){
//u can even free the memory
if(!n->next)
{
result=n->value;
return NULL;
}
n->next=getTail(n->next,result);
}
or you can do it other way
int getTail(Node *n)
{
if(!n) return 0;
if(n->next)
{
if(!n->next->next)
{
Node *frnode=n->next;
int result=n->next->value;
n->next=NULL;
delete frnode;
return result;
}
getTail(n->next);
}
You are not removing last node in your code, and you leak another (temporary) node here.
To remove last node you have to zero the link in the previous node.
Your code should look like
...
if (n == NULL || n->next == NULL)
throw std::out_of_range("node");
if(n->next->next == NULL)
{
int val = n->next->value;
delete n->next;
n->next = NULL;
return val;
}
else ...
Be aware of the fact that c++ is not a functional language and has no optimizations for tail recursion, so in real application as your lists grow big enough you'll eventually have failure with stack overflow =) use Haskell or Erlang for this style of programming, in c++ use for or while.
You should set the Node n's previous Node's next field to NULL when n is the tail Node.
I'm trying to write a method for my LinkedList class that will sort a linked list of Person objects by their name. My method compiles fine but when I try to sort a list of people, the output is incorrect. It also never stops running. For example, this code
Person *p1 = new Person("K", "B");
Person *p2 = new Person("A", "A");
Person *p3 = new Person("S", "M");
Person *p4 = new Person("B", "M");
LinkedList ll;
ll.insertFront(*p1);
ll.insertFront(*p2);
ll.insertFront(*p3);
LinkedList newList = ll.insertionSort();
newList.print();
cout << endl;
Gives this output
B, K
A, A
Could anyone help me figure out where I went wrong with my algorithm? Thanks!
This is the method I use to sort names by both first and last:
int Person::compareName(Person p)
{
if (lName.compare(p.lName) > 0)
{
return 1;
}
else if (lName.compare(p.lName) == 0)
{
if (fName.compare(p.fName) > 0)
{
return 1;
}
else return -1;
}
else return -1;
}
Insertion Sort Method:
LinkedList LinkedList::insertionSort()
{
//create the new list
LinkedList newList;
newList.front = front;
Node *n;
Node *current = front;
Node *trail = NULL;
for(n=front->link; n!= NULL; n = n->link)//cycle through old chain
{
Node* newNode = n;
//cycle through new, sorted chain to find insertion point
for(current = newList.front; current != NULL; current = current->link)
{
//needs to go in the front
if(current->per.compareName(n->per) < 0)
{
break;
}
else
{
trail = current;
}
}
//if it needs to be added to the front of the chain
if(current == front)
{
newNode->link = newList.front;
newList.front = newNode;
}
//else goes in middle or at the end
else{
newNode->link = current;
trail->link = newNode;
}
return newList;
}
You have current->link in your inner for loop, and in the else to the inner for loop. I assume that you really have current = current->link in the for loop or it does nothing. If so, you'd be skipping every other element.
You also have a language thing- you aren't creating new nodes, you're altering the nodes on your original list. That measn you're changing the list as you walk it, which will corrupt the list as you sort it. Behavior is undefined and dependent on the order in which you add elements.
Even after you have fixed any linked list handling issues (which I haven't looked at), your compareName() function has a flaw - when comparing Person objects that have the same last name it may return from the function without providing a value (in the cases where Name.compare(p.fName) <= 0).
Getting an indeterminate result from the compare function will break pretty much any sort.
Since this is likely homework, I'll leave correcting the problem as an exercise.
I've created a Hash-table and I want to remove a node from the linked-list. The code works for removing the first node but not for removing others.
void intHashTable::remove(int num){
int location = ((unsigned)num) % size;
Node * runner = table[location];
int checker;
if(runner->next == NULL){
if(num == table[location]->num){
table[location] = NULL;
}
}else{
if(table[location]->num == num){
table[location] = table[location]->next;
}else{
//This part doesn't seem to be working.
Node *temp = runner->next;
while(temp != NULL){
if(temp->num == num){
runner->next = temp->next;
delete(temp);
break;
}
}
}
}
}
You haven't updated temp to point to the next item within the loop:
temp = temp->next;
You also appear to represent an empty row with a NULL pointer in your table, but you don't handle this case properly in your code - if runner is NULL then you'll crash when you try to access runner->next in the first check. Also, you're failing to delete the node in some cases.
To fix these issues, you can update your code to something like this:
void intHashTable::remove(int num)
{
int location = ((unsigned)num) % size;
Node * runner = table[location];
if (runner != NULL) {
if (runner->num == num) {
delete runner;
table[location] = NULL;
} else {
while (runner->next != NULL) {
if (runner->next->num == num) {
Node *temp = runner->next;
runner->next = runner->next->next;
delete temp;
break;
}
runner = runner->next;
}
}
}
}
Also note that I've removed the brackets from delete, which is a C++ keyword and not a function.
If you use doubly-linked lists (i.e. with a previous pointer as well as a next) then you can simplify this code a little, although for something like a hash table where you only tend to iterate through in one direction it's probably not worth the expense of the extra pointer (an extra 8 bytes per item on a 64-bit system).
You didn't updated temp and runner variables inside loop:
while(temp != NULL)
{
if(temp->num == num)
{
runner->next = temp->next;
delete temp;
break;
}
runner = temp; // Keep previous element to change its next pointer when num found
temp = temp->next; // Advance current pointer to next element
}