Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can we fix this function? The goal is to ask the user to enter a number then

ID: 3761616 • Letter: H

Question

How can we fix this function? The goal is to ask the user to enter a number then function should search to find the number. if no number then return false otherwise true. The function returns 1 all the time.

What can we do to fix it?

Topic is: Linked Lists

Language c++

Here is the function:

//

bool NumberList::findNode (double value)

{

ListNode * nodeptr;;

  

if (head == nullptr)

{

cout << "List is empty." << endl;

return false;

}

  

nodeptr = head;

while (nodeptr)

{

if(nodeptr->value == value)

return true;

nodeptr = nodeptr->next;

  

}

return false;

}

FUNCTION MAIN:

NumberList list;

double n;

int position;

  

cout << "Enter a number to be searched: " << endl;

cin >> n;

position = list.findNode(n);

if (position == !n)

cout << "Element is not found in list " << endl;

else

cout << "Element found at: " << position << endl;

Explanation / Answer

#include <iostream>

#include <cstring>

#include <iomanip>

#include "DoublyLinkedList.h"

using namespace std;

DoublyLinkedList::DoublyLinkedList (int data)

{

            head = new Node (data, NULL, NULL);

            head ->next = head ->previous = head;

            size = 3;

            //int list [16];

}

DoublyLinkedList::DoublyLinkedList()

{

            Node *temp = this->head;

            Node *temp2;

            while (temp->previous)

                        temp = temp ->previous;

            while(temp)

            {

                        temp2 = temp ->next;

                        delete temp;

                        temp = temp2;

            }

            temp = temp2 = NULL;

}

bool DoublyLinkedList::empty()

{

            return (this ->size == 0);

}

void DoublyLinkedList ::addNodeBefore (int data)

{

            Node *next = head;

            Node *previous = head ->previous;

            Node *temp2 = new Node (data, previous, next);

            size++;

}

void DoublyLinkedList::addNodeAfter(int data)

{

            Node *next = head ->next;

            Node *previous = head;

            Node *temp2 = new Node (data, previous, next);

            size++;

}

/*void DoublyLinkedList::out(bool directly)

{

            if (directly)

            {

                        Node *temp = head;

                        do

                        {

                                    cout<data;

                                    temp = temp->next;

                        }

                        while (temp != head);

            }

            else

            {

                        Node *temp = head;

                        do

                        {

                                    cout<data;

                                    temp = temp ->previous;

                        }

                        while (temp != head);

            }

            cout<

void DoublyLinkedList::setData( int Data)

{

            this ->head->data = Data;

}

void DoublyLinkedList::setPrevious(Node* Previous)

{

            this ->head->previous = Previous;

}

void DoublyLinkedList::setNext( Node* Next)

{

            this ->head->next = Next;

}

bool DoublyLinkedList::findNode (double value)

{

            int counterStart = 0;

            Node *temp = head;

            while (temp ->next != head)

            {

                        if (temp ->data == find)

                                    counterStart++;

                        temp = temp->next;

            }

            if (counterStart > 0)

            {

                        cout<<" ' "<" ' was found"<" time(s)"<return true;

            }

            else

            {

                        cout<<" ' "<" ' was not found"<return false;

            }

}

void DoublyLinkedList::deleteData(int find, bool everything)

{

            Node *temp = head;

            while (temp)

            {

                        if (temp ->data == find)

                        {

                                    cout<<"Removing "<previous->next = temp ->next;

                                    temp ->next->previous = temp ->previous;

                                    if (false)

                                                return;

                        }

                        temp = temp ->next;

            }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote