Quick question, I need this program to remove a number and print the remaining numbers from the linked list I run the program and it works fine until I enter a number I want to remove from the list and it re-prints the list all wrong:
before the removal:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ,
after the removal:
0 , 1 , 1629738484 , 1629738476 , 1629738468 , 0 ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
void print()
// prints salary of linked list
{
nodeType *curr;
tmp = first;
curr = tmp;
while (curr != NULL)
{
cout <<curr->num<<" , ";
curr = curr->link;
}
cout << endl;
}
nodeType* findNode(int Item)
// Given value, function searches through linked list information to
// determine if it is present in the list. If present, returns
// pointer to the item.
{
bool found = false;
nodeType *current;
current = first;
while ((current != NULL) && (!found))
{
if (current->num == Item)
found = true;
else
current = current->link;
}
return current;
}
void remove(const int item)
{
nodeType *current;
nodeType *next;
current = findNode(item);
next = current->link;
delete current;
current = next;
cout<<current->num;
return;
}
// client code
int main()
{
int a;
nodeType *temp;
ReadIntoLinkedList();
cout << "Given the Linked List:" << endl;
cout << " ";
print();
cout << "Enter a number to remove: "<< endl;
cin>>a;
remove(a);
cout << "printing now" << endl;
print();
return 0;
}
|
Not sure what is wrong, my thought is it my be in my function remove that is causing the problem but can't figure it out. Any help would be greatly appreciated.