Question

In: Computer Science

write a c++ member function that removes the first instance of a specific element in a...

write a c++ member function that removes the first instance of a specific element in a linked list and then return the size of the list after the removal whether it was successful or not.

Solutions

Expert Solution

// CPP program to remove first node of
// linked list.
#include <iostream>
using namespace std;

/* Link list node */
struct Node {
   int data;
   struct Node* next;
};

/* Function to remove the first node
of the linked list */
Node* removeFirstNode(struct Node* head)
{
   if (head == NULL)
       return NULL;

   // Move the head pointer to the next node
   Node* temp = head;
   head = head->next;

   delete temp;

   return head;
}

// Function to push node at head
void push(struct Node** head_ref, int new_data)
{
   struct Node* new_node = new Node;
   new_node->data = new_data;
   new_node->next = (*head_ref);
   (*head_ref) = new_node;
}

// Driver code
int main()
{
   /* Start with the empty list */
   Node* head = NULL;

   /* Use push() function to construct
   the below list 8 -> 23 -> 11 -> 29 -> 12 */
   push(&head, 12);
   push(&head, 29);
   push(&head, 11);
   push(&head, 23);
   push(&head, 8);

   head = removeFirstNode(head);
   for (Node* temp = head; temp != NULL; temp = temp->next)
       cout << temp->data << " ";

   return 0;
}

length function:

int getCount(Node* head)

{

    int count = 0; // Initialize count

    Node* current = head; // Initialize current

    while (current != NULL)

    {

        count++;

        current = current->next;

    }

    return count;

}


Related Solutions

write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a...
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a linked list. After attemtped removal return the SIZE of the linked lost whether or not the removal was successful.
Write a c++ member function that sequentially searches for a specific element in a doubly linked...
Write a c++ member function that sequentially searches for a specific element in a doubly linked list. return the position if found or -1 is the element cannot be found.
Write a c++ member function that attempts to insert a NON DUPLICATE element to a doubly...
Write a c++ member function that attempts to insert a NON DUPLICATE element to a doubly linked list, After the attempted insertion return the SIZE of the doubly linked list whether or not the insertion was successful.
3.) The function remove of the class arrayListType removes only the first occurrence of an element....
3.) The function remove of the class arrayListType removes only the first occurrence of an element. Add the function removeAll as an abstract function to the class arrayListType, which would remove all occurrences of a given element. Also, write the definition of the function removeAll in the class unorderedArrayListType and write a program to test this function. 4.) Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write...
C# Write a console application that takes the following passage and removes every instance of the...
C# Write a console application that takes the following passage and removes every instance of the word "not" using StringBuilder and prints the result out to the console: I do not like them In a house. I do not like them With a mouse. I do not like them Here or there. I do not like them Anywhere. I do not like green eggs and ham. I do not like them, Sam-I-am. Ensure that the resulting output reads normally, in...
C++ 1. The function removeAt of the class arrayListType removes an element from the list by...
C++ 1. The function removeAt of the class arrayListType removes an element from the list by shifting the elements ofthe list. However, if the element to be removed is at the beginning ofthe list and the list is fairly large, it could take a lot ofcomputer time. Because the list elements are in no particular order, you could simply remove the element by swapping the last element ofthe list with the item to be removed and reducing the length of...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
** * Write a recursive function that removes the first k even numbers * from the...
** * Write a recursive function that removes the first k even numbers * from the stack. If there are less than k even elements in the stack, * just remove all even elements. Do not use any loops or data structures * other than the stack passed in as a parameter. * @param stack * @param k * @return Returns the number of elements removed from the stack. */ public static int removeEvenNumbers(Stack<Integer> stack, int k) { return 0;...
Write a C program for the recursive algorithm that removes all occurrences of a specific character...
Write a C program for the recursive algorithm that removes all occurrences of a specific character from a string. (please comment the code)
haskell : write a function that reverse the first three element of a list, but not...
haskell : write a function that reverse the first three element of a list, but not the rest. example [1,2,3,4,5,6] == [3,2,1,4,5,6]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT