ISBN13 9781449646752 C Plus Data Structures 5th Edition Chap
ISBN13: 9781449646752 C++ Plus Data Structures 5th Edition
Chapter 3 Question 6
1) Implement DeleteItem where the list is unchanged if the item to be deleted is not in the list using a linked implementation.
2) Implement DeleteItem where all the copies of the item to be deleted are removed if they exsist using a linked implentation.
Solution
Hi Frieds, We do not have The Book you have mentioned in question.
Please post all details of questions insteaad of giving reference.
I am providing you a methods that delete all nodes of given value. You ca take reference of my method.
Suppose we have given structure of Linked List:
class List{
struct Node
{
int data;
Node* next;
Node(int data): data(data), next(NULL){}
};
typedef struct Node* Nodeptr;
Nodeptr first;
public:
/* This is default constructor that initialize \'first\' and \'last\' with null and \'size\' with zero*/
List()
{
first = NULL;
}
void DeleteItem(int data){
// if list is not empty
if(first != NULL){
// list has only one node
if(first->next == NULL && first->data == data){
delete first;
first = NULL;
}
else{
// if first node has same value then forward first pointer
while(first != NULL && first->data == data){
first = first->next
}
// if all nodes have same value then delete all nodes and first point to NULL
if(first == NULL){
last = NULL;
return;
}
Nodeptr temp = first;
while(temp != NULL && temp->next!= NULL){
if(emp->next->data == data) // if node has same value then delete it
temp = temp->next->next;
else
temp = temp->next;
}
}
}
}
}

