(1) build link list with even numbers. build it felling up the number 2 to 20..(
ID: 3530003 • Letter: #
Question
(1) build link list with even numbers. build it felling up the number 2 to 20..(2) Have a search options if we put 15 it should say not found..(3) Insert an item example number 22 another even number . (PLESE SHOW ME THE WHOLE CODE USING C++ IT BE VERY HELPFUL) THANKSExplanation / Answer
#include #include /* a node of the singly linked list */ struct node { int data; struct node *next; }; void segregateEvenOdd(struct node **head_ref) { struct node *end = *head_ref; struct node *prev = NULL; struct node *curr = *head_ref; /* Get pointer to the last node */ while(end->next != NULL) end = end->next; struct node *new_end = end; /* Consider all odd nodes before the first even node and move then after end */ while(curr->data %2 != 0 && curr != end) { new_end->next = curr; curr = curr->next; new_end->next->next = NULL; new_end = new_end->next; } // 10->8->17->17->15 /* Do following steps only if there is any even node */ if (curr->data%2 == 0) { /* Change the head pointer to point to first even node */ *head_ref = curr; /* now current points to the first even node */ while(curr != end) { if ( (curr->data)%2 == 0 ) { prev = curr; curr = curr->next; } else { /* break the link between prev and current */ prev->next = curr->next; /* Make next of curr as NULL */ curr->next = NULL; /* Move curr to end */ new_end->next = curr; /* make curr as new end of list */ new_end = curr; /* Update current pointer to next of the moved node */ curr = prev->next; } } } /* We must have prev set before executing lines following this statement */ else prev = curr; /* If the end of the original list is odd then move this node to end to maintain same order of odd numbers in modified list */ if((end->data)%2 != 0) { prev->next = end->next; end->next = NULL; new_end->next = end; } return; } /* UTILITY FUNCTIONS */ /* Function to insert a node at the beginning of the Doubly Linked List */ void push(struct node** head_ref, int new_data) { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the data */ new_node->data = 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 doubly linked list This function is same as printList() of singly linked lsit */ void printList(struct node *node) { while(node!=NULL) { printf("%d ", node->data); node = node->next; } } /* Drier program to test above functions*/ int main() { /* Start with the empty list */ struct node* head = NULL; /* Let us create a sample linked list as following 17->15->8->12->10->5->4->1->7->6 */ push(&head, 6); push(&head, 7); push(&head, 1); push(&head, 4); push(&head, 5); push(&head, 10); push(&head, 12); push(&head, 8); push(&head, 15); push(&head, 17); printf(" Original Linked list "); printList(head); segregateEvenOdd(&head); printf(" Modified Linked list "); printList(head); getchar(); return 0; } Output: Original Linked list 17 15 8 12 10 5 4 1 7 6 Modified Linked list 8 12 10 4 6 17 15 5 1 7Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.