Of course this isn't the whole program....
but it is a rather large slice of code.
Yes I am in college, I am not asking for someone to do this for me.. I think that is rather stupid... you don't learn anything and if you get a job and cannot do it because someone else did the work for you.. that is your stupidity :)
I understand most of this.
This uses templates, that is what the <T> is...
This compiles, and I can remove the head and tail nodes fine..
and in some strange cases I can remove the middle node IF it is directly to the right or left of the head or tail nodes.
if not.. it crashes on the line #59 that says "if(value == walk->data)"
any questions or comments are welcome and encouraged.
any help or pointers is appreciated!
[CODE=CPP]
// This will delete a value in the list specified by the user
void remove(T value)
{
// if the value is equal to the head
if(value == mHead->data)
{
// make a temp node to hold the value of the head
SNode<T> *temp = mHead;
// advance the the next value past the head and
// assign it to the head
mHead = mHead->next;
// delete the old head
delete temp;
}
// if the value is equal to the tail
else if(value == mTail->data)
{
// create two temp nodes for swapping values and
// assign temp the value of head
SNode<T> *temp = mHead, *temp2;
// if the head is the only value delete it and
// set it to NULL
if(temp->next == NULL)
{
delete temp;
mHead = NULL;
}
else
{
// while temp->next is not the end of list
while(temp->next != NULL)
{
// assign temp to temp2
temp2 = temp;
// temp is assigned to the temp->next
temp = temp->next;
}
// and delete temp
delete temp;
// set temp2-next equal to NULL
temp2->next = NULL;
// and assign the tail to what temp2 holds
mTail = temp2;
}
}
else
{
// Create 2 nodes, walk and temp
SNode<T> *walk = mHead, *temp;
// advance past head node
walk = walk->next;
// assign the head to walk->prev
walk->prev = mHead;
// while walk is not NULL
while(walk != NULL)
{
// if the user input matches the value in the current position
// CRASHES HERE!!!
if(value == walk->data)
{
// assign walk to temp
temp = walk;
// assign walk->prev to walk
walk = walk->prev;
// temp->next is assigned to walk->next
walk->next = temp->next;
//delete the temp node
delete temp, temp2;
}
walk = walk->next;
}
}
}
[/CODE]
but it is a rather large slice of code.
Yes I am in college, I am not asking for someone to do this for me.. I think that is rather stupid... you don't learn anything and if you get a job and cannot do it because someone else did the work for you.. that is your stupidity :)
I understand most of this.
This uses templates, that is what the <T> is...
This compiles, and I can remove the head and tail nodes fine..
and in some strange cases I can remove the middle node IF it is directly to the right or left of the head or tail nodes.
if not.. it crashes on the line #59 that says "if(value == walk->data)"
any questions or comments are welcome and encouraged.
any help or pointers is appreciated!
[CODE=CPP]
// This will delete a value in the list specified by the user
void remove(T value)
{
// if the value is equal to the head
if(value == mHead->data)
{
// make a temp node to hold the value of the head
SNode<T> *temp = mHead;
// advance the the next value past the head and
// assign it to the head
mHead = mHead->next;
// delete the old head
delete temp;
}
// if the value is equal to the tail
else if(value == mTail->data)
{
// create two temp nodes for swapping values and
// assign temp the value of head
SNode<T> *temp = mHead, *temp2;
// if the head is the only value delete it and
// set it to NULL
if(temp->next == NULL)
{
delete temp;
mHead = NULL;
}
else
{
// while temp->next is not the end of list
while(temp->next != NULL)
{
// assign temp to temp2
temp2 = temp;
// temp is assigned to the temp->next
temp = temp->next;
}
// and delete temp
delete temp;
// set temp2-next equal to NULL
temp2->next = NULL;
// and assign the tail to what temp2 holds
mTail = temp2;
}
}
else
{
// Create 2 nodes, walk and temp
SNode<T> *walk = mHead, *temp;
// advance past head node
walk = walk->next;
// assign the head to walk->prev
walk->prev = mHead;
// while walk is not NULL
while(walk != NULL)
{
// if the user input matches the value in the current position
// CRASHES HERE!!!
if(value == walk->data)
{
// assign walk to temp
temp = walk;
// assign walk->prev to walk
walk = walk->prev;
// temp->next is assigned to walk->next
walk->next = temp->next;
//delete the temp node
delete temp, temp2;
}
walk = walk->next;
}
}
}
[/CODE]
Comment