Question

In: Computer Science

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.

Solutions

Expert Solution

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.

Code :-

​
#include <iostream>
using namespace std; 

struct Node 
{ 
        int data; 
        struct Node *next; 
        struct Node *prev; 
}; 

void insertNode(struct Node** start, int value) 
{ 
        if (*start == NULL) 
        { 
                struct Node* new_node = new Node; 
                new_node->data = value; 
                new_node->next = new_node->prev = new_node; 
                *start = new_node; 
                return; 
        } 

        Node *last = (*start)->prev; 
        struct Node* new_node = new Node; 
        new_node->data = value; 

        new_node->next = *start; 
 
        (*start)->prev = new_node; 
 
        new_node->prev = last; 

        last->next = new_node; 
} 

void displayList(struct Node* start) 
{ 
        struct Node *temp = start; 

        while (temp->next != start) 
        { 
                printf("%d ", temp->data); 
                temp = temp->next; 
        } 
        printf("%d ", temp->data); 
} 
 
int searchList(struct Node* start, int search) 
{ 
        struct Node *temp = start; 
        int count=0,flag=0,value; 

        if(temp == NULL) 
                return -1; 
        else
        { 
                while(temp->next != start) 
                { 
                        count++; 

                        if(temp->data == search) 
                        { 
                                flag = 1; 
                                count--; 
                                break; 
                        } 
                        temp = temp->next; 
                }
                if(temp->data == search) 
                { 
                        count++; 
                        flag = 1; 
                } 
                
                // If flag is true, then element found
                // else the element cannot be found so return -1
                if(flag == 1) 
                        cout<<"\n"<<search <<"\nfound at location "<<count<<endl; 
                else
                        cout<<"\n"<<search <<"\n-1"<<endl; 
        } 
} 

int main() 
{ 
        /* Start with the empty list */
        struct Node* start = NULL; 

        // Insert 17. So linked list becomes 17->NULL 
        insertNode(&start, 17); 

        // Insert 25. So linked list becomes 17->25 
        insertNode(&start, 25); 

        // Insert 9. So linked list 
        // becomes 17->25->9 
        insertNode(&start, 9); 

        // Insert 8. So linked list 
        // becomes 17->25->9->18 
        insertNode(&start, 18); 

        // Insert 6. So linked list 
        // becomes 17->25->9->18->6 
        insertNode(&start, 6); 

        printf("Created doubly linked list is: "); 
        displayList(start); 
        printf("\nEnter the element you want to search: ");
        searchList(start, 9); 
        
return 0;
}

​

Screenshot of code and output :-

Code :-

Output :-


Related Solutions

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.
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.
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly...
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly linked lists if you can make it clean and short that be nice
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT