/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
using namespace std;
struct node_t{
unsigned v;
struct node_t* next;
};
struct node_t* even_nodes(struct node_t** listHead){
struct node_t* current = *listHead;
struct node_t* previous = NULL;
struct node_t* head = NULL;
bool headFoundFlag = 0;
while (current->next != NULL){ //check if end of list
if (current->v % 2 == 0){
if (!headFoundFlag){
head = current;
headFoundFlag = 1;
}
previous = current;
current = current->next;
}
else {
previous->next = current->next; //remove current entry from chain
current = current->next;
}
}
return head;
};
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning */
void push(struct node_t** head_ref, int new_data)
{
/* allocate node */
struct node_t* new_node =
(struct node_t*) malloc(sizeof(struct node_t));
/* put in the data */
new_node->v = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print nodes in a given linked list */
void printList(struct node_t* node)
{
while (node!=NULL)
{
printf("%d ", node->v);
node = node->next;
}
}
int main()
{
/* Start with the empty list */
struct node_t* head = NULL;
/* Let us create a sample linked list as following
0->2->4->6->8->10->11 */
push(&head, 11);
push(&head, 10);
push(&head, 8);
push(&head, 6);
push(&head, 4);
push(&head, 2);
push(&head, 0);
printf("\nOriginal Linked list \n");
printList(head);
printf("\nModified Linked list \n");
printList(even_nodes);
return 0;
}