Question #12244

Write a program to copy the content of the following array to a dynamic single linked list, and write a function to delete every node has an even value in its data ?

Expert's answer

struct node{

int value;

struct node* next;

};

node* root = new node;

void toList(int* array, int size){

root->value = array[0];

root->next = NULL;

node* temp = root;

for(int i = 1; i < size; i++){

node* n = new node;

n->next = NULL;

n->value = array[i];

temp->next = n;

temp = n;

}

}

void remove(){

node* temp = root;

while(temp->next != NULL){

if(temp->next->value % 2 == 0){

temp->next= temp->next->next;

}

else

temp = temp->next;

}

if(root->value % 2 == 0)

root = root->next;

}

int value;

struct node* next;

};

node* root = new node;

void toList(int* array, int size){

root->value = array[0];

root->next = NULL;

node* temp = root;

for(int i = 1; i < size; i++){

node* n = new node;

n->next = NULL;

n->value = array[i];

temp->next = n;

temp = n;

}

}

void remove(){

node* temp = root;

while(temp->next != NULL){

if(temp->next->value % 2 == 0){

temp->next= temp->next->next;

}

else

temp = temp->next;

}

if(root->value % 2 == 0)

root = root->next;

}

## Comments

## Leave a comment