Question

In: Computer Science

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.

Solutions

Expert Solution

here is a program with function definition and implementation i returned the size as well output the message.

--------------------------------------------------------------------------------------

#include <bits/stdc++.h>

using namespace std;
class dll //class defining doubly linked list
{
public:
int data;
dll *next;
dll *prev;
};

dll *head=NULL; //creating head node
dll *curr=NULL; //creating current node
int size =0; // initially size is 0
int insertifnotpresent(int val) // function for insertion if not duplicate number
{
dll *temp=head;   
for(int i=0;i<size;i++) //checking if val is duplicate or not
{
if(temp->data==val) // if duplicate size is returned and further function will not run
{
cout<<"size = "<<size<<", insertion is not successful.\n";
return size;
}
temp=temp->next;
}


dll *newnode = new dll(); // creating a new node
newnode->data = val; // initializing the node data
newnode->next=NULL;
newnode->prev=NULL;
if(head== NULL) // if newnode is first node
{
curr=head = newnode;
size++;
}
else
{
curr->next =newnode;
newnode->prev = curr;
curr = newnode;
size++;
  
}
cout<<"size = "<<size<<", insertion is successful.\n";
return size;

}

int main()
{
  
int n,val;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>val;
insertifnotpresent(val);
}
  
  
return 0;
}

--------------------------------------------------------------------------------------------------

input and corresponding output screen shot

------------------------------

---------------------------------------------------------------------------

feel free to write in comment section if you have any doubt.

do upvote if you like the answer.


Related Solutions

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 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.
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.
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows...
Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows classic indexing from 0 to item_count - 1)
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2,4, 5, and 8 and array 2 has the values 1, 0, 4, and 6, the function assigns array 3 the values 3, 4, 9, and 14. The function should take three array names and an array size as arguments. Test the function in a...
Write the methods that insert and remove an element at the kth position in Java using...
Write the methods that insert and remove an element at the kth position in Java using recursion (NOT iteration) (Hint for the remove method: we have to recursive position -1 times, and each time we do the recursion, we have to create a method to move the head to the right) public void insertRecursive(int position, int data) { //enter code here } public int removeAtRecursive(int position) { //enter code here } Here is the given class for the Node: public...
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT