Using an appropriate definition of ListNode, design a simple linked list class w
ID: 3641896 • Letter: U
Question
Using an appropriate definition of ListNode, design a simple linked list class with only two member functions and a default constructor:void add(double x);
boolean isMember(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.
Then add a print member function. Test the class by starting with an empty list, adding some elements, and then printing the resulting list out.
Then modify your program to include a member function
int search(double x)
that returns the position of a number x on the list. The first node in the list is at position 0, the second node is at position 1, and so on. If x is not found on the list, the search should return -1. Test the new member function using an appropriate driver program. Please show source code and output,using visual studio.
#include<iostream>
#include<cmath>
using namespace std;
struct nodeType
{
int data; // store information,private data
nodeType *next; //the reference to the next node
};
class LinkedList
{
public:
void add(double x)
{
nodeType *c;
c=new nodeType;
c->badge=x;
c->link=0;
if(head==0)
head=c;
else
{
nodeType *p;
p=head;
while(p->link!=0)
p=p->link;
p->link=c;
}
}
boolean isMember(double x)
{
nodeType *c;
c=head;
while(c!=0)
{
if(x==c->badge)
return true;
c=c->link;
}
return false;
}
void print()
{
nodeType *current=head;
if(head!=0)
cout<<"Badge number(s) are "<<endl;
while(current != NULL)
{
//cout<<badge<<endl;
current = current -> link;
}
}
LinkedList()
{
head=new nodeType;
head=0;
}
protected:
nodeType *head;
};
int main()
{
LinkedList obj;
double badge;
for(int i=0;i<5;i++)
{
cin>>badge;
obj.add(badge);
}
obj.print();
int num;
cout<<"This program demonstrates search of a linked list."<<endl;
cout<<"Enter 5 numbers: "<<endl;
cin>>num;
if(obj.isMember(num))
cout<<"badge number "<<" exist"<<endl;
else
cout<<"badge number "<<" does not exist"<<endl;
system("paused")
return 0;
}
}
Explanation / Answer
Hi sunshine, First the code you posted is wrong in a million different ways. If you have any questions regarding linked list feel free to msg me and i will do my best to explain this code for you anyways,Here you go: #include using namespace std; //if we place an element at the start of the list we get a First in last out form of a data structure class LinkedList { private: struct myList { double data; myList* nxt; }; myList* head; public: LinkedList() { //use default constructor to make the head ready head = new myList; head->data = NULL; head->nxt=NULL; } void add(double x) { //add in front of list myList* temp = head; head = new myList; head->nxt = temp; head->data = x; } int search(double x) { int i = 0; myList* temp = head; while(temp->data!=NULL) { if(temp->data==x) { //the value is in the list return i; } temp = temp->nxt; i++; } //-1 for not found return -1; } bool isMember(double x) { myList* temp = head; while(temp->data!=NULL) { if(temp->data==x) { //the value is in the list return true; } temp = temp->nxt; } return false; } void printMyList() { myList* temp = head; int i=0; while(temp->data!=NULL) { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.