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;
}
}