Question

Write C language code for a function that takes a doubly linked list as a parameter...

Write C language code for a function that takes a doubly linked list as a parameter and deletes all the nodes in even positions from the first to the last after displaying the content of each node to the console.

Homework Answers

Answer #1

function to delete all nodes at even position in C

// function to delete a node in a Doubly Linked List. 
// head_ref --> pointer to head node pointer. 
// del --> pointer to node to be deleted 
void deleteNode(Node** head_ref, Node* del) 
{ 
        // base case 
        if (*head_ref == NULL || del == NULL) 
                return; 

        // If node to be deleted is head node 
        if (*head_ref == del) 
                *head_ref = del->next; 

        // Change next only if node to be 
        // deleted is NOT the last node 
        if (del->next != NULL) 
                del->next->prev = del->prev; 

        // Change prev only if node to be 
        // deleted is NOT the first node 
        if (del->prev != NULL) 
                del->prev->next = del->next; 

        // Finally, free the memory occupied by del 
        free(del); 

        return; 
} 

// function to delete all the nodes at even position
// from the doubly linked list 
void deleteEvenPositionNodes(Node** head_ref) 
{ 
    int i = 0;
        Node* ptr = *head_ref; 
        Node* next; 

        while (ptr != NULL) { 
            i++;
                next = ptr->next; 
                printf("%d\n", ptr->data);
                if (i % 2 == 0) 
                        deleteNode(head_ref, ptr); 
                ptr = next; 
        } 
} 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c++ code for function that take doubly linked list as parameter and deleted all nodes in...
c++ code for function that take doubly linked list as parameter and deleted all nodes in even position from first to last
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
In C++ please Implement a class for doubly linked list. Your doubly linked list should have...
In C++ please Implement a class for doubly linked list. Your doubly linked list should have following members: - A head pointer - A tail pointer - Function: insert at head -Function: insert at tail - Function: delete a node - Function: search a node - Function: traverse - Function: reverse traverse Test your implementation by calling member functions inside main.
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head"...
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head" and "tail" Each node in FLL has "next" Each node in DLL has "next" and "prev" Q1: Complete the following constructor code for FLL FLL::FLL() { Q2: Complete the FLL insert-front code (which insert node new at the front of FLL) FLL::insert-front( node * new) { Q3: Complete the DLL insert-after code (which insert node new after node p) DLL::insert-after(node *p, node *new) {
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
Write Pseudocode to remove the node at position 1 in a doubly-linked list. Assume position follows...
Write Pseudocode to remove the node at position 1 in a doubly-linked list. Assume position follows classic indexing from 0 to item_count - 1, and there is a node at position 2.
a.   Write the separate function to delete the head node and tail node of a singly...
a.   Write the separate function to delete the head node and tail node of a singly linked list in java programming language? b.   Write the separate function to delete the head node and tail node of a doubly linked list in java programming language? c.   Write the best and worst case of the following sort algorithms. i.   Selection sort ii.   Bubble sort iii.   Insertion sort iv.   Merge sort
Using the given SList as a template to implement a doubly linked list with the following...
Using the given SList as a template to implement a doubly linked list with the following functionalities Add first: add an element to the head of the list Add last: add an element to the end of the list Print: print the content of the list starting from the head Find: a private method that will return a reference to a node if it contains the same value of a given argument Insert after: a method that takes a key...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT