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

Using an appropriate definition of listnode, design a simple linked list class w

ID: 3625552 • Letter: U

Question

Using an appropriate definition of listnode, design a simple linked list class with only two member function and a defult constructor:

void add(double x)
boolean isMumber(double x)
LinkedList();

The add function adds a new node containing x to the front (head) of the list,
while the isMember function tests to see if the list contains a node with the value x. Test your linked list class by adding various numbers to the list and then testing for membership

second part of the question :

Modify your list class of programming challenge 1 to add a copy constructor. Test your class by making a copy of a list and then testing membership on the copy.

third part :

Modify the list class you created in the previous programming challenge to add a print member function. Test the class by starting with an empty list, adding some elements, and then printing the resulting list out.

Explanation / Answer

Dear User,

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

class linkedlist

{

private:

struct listNode

{

double x;

struct listNode *next;

};

listNode *head;

public:

linkedlist()

{

head = NULL;

}

// copy constructor

linkedlist( const linkedlist& otherList );

//add member fuunction

void add(double )

//isMumber member function

boolean isMumber(double )

//Print memberfunction

void print() const;

};

#endif

#include<iostream>

#include "linkedlist.h"

using namespace std;

linkedlist::linkedlist( const linkedlist& otherList )

{

head = NULL;

listNode *newNode;

listNode *nodePtr;

listNode *tempPtr;

if ( !otherList.head )

return;

nodePtr = otherList.head;

head = new listNode;

head->x = nodePtr->x;

head->next = NULL;

nodePtr = nodePtr->next;

tempPtr = head;

while ( nodePtr != NULL )

{

newNode = new listNode;

newNode->x = nodePtr->x;

newNode->next = NULL;

tempPtr->next = newNode;

tempPtr = newNode;

nodePtr = nodePtr->next;

}

}

void linkedlist::add( double x )

{

listNode *newNode;

listNode *nodePtr;

listNode *previousNode = NULL;

newNode = new listNode;

newNode->x = x;

newNode->next = NULL;

if ( !head )

head = newNode;

else

{

nodePtr = head;

previousNode = NULL;

while ( nodePtr != NULL && nodePtr->x < x )

{

previousNode = nodePtr;

nodePtr = nodePtr->next;

}

if ( previousNode == NULL )

{

head = newNode;

newNode->next = nodePtr;

}

else

{

previousNode->next = newNode;

newNode->next = nodePtr;

}

}

}

linkedlist::isMumber(double x)

{

listNode *nodePtr;

int pos=-1;

if ( !head )

{

cout << "The list is empty";

return-1;

}

nodePtr = head;

while (nodePtr)

{

pos++;

if(nodePtr->x== x)

return pos;

else

nodePtr=nodePtr->next;

}

return -1;

}

void linkedlist::print() const

{

listNode *nodePtr;

if ( !head )

{

cout << "The list is empty.";

return;

}

nodePtr = head;

cout << " The elements in the list are:";

while (nodePtr)

{

cout << nodePtr->x << " -> ";

nodePtr = nodePtr->next;

}

cout << "Null";

}

#include<iostream>

using std::cout;

using std::cin;

#include "linkedlist.h"

int main()

{

linkedlist myList;

char choice;

int n;

do {

cout << " Insert a node : I";

cout << " Quit : Q";

cout << " tEnter your choice: ";

cin >> choice;

switch ( choice )

{

case 'I' :

case 'i' :

cout<<" Enter an integer: ";

cin >> n;

myList.add( n );

break;

case 'Q' :

case 'q' :

choice = 'q';

}

} while ( choice != 'q' );

return 0;

}

I hope this will helps to You !

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