Using Classes: Define the functions declared in LinkedList.h so that main.cpp pr
ID: 3572777 • Letter: U
Question
Using Classes: Define the functions declared in LinkedList.h so that main.cpp produces results similar to when LinkedList.obj is included in the project
Three files are provided to you.
1. Main.cpp contains the driver that will run the declared functions to ensure they work properly, it does not need to be altered or submitted.
2. LinkedList.h contains the declarations and function prototypes related to the class, it does not need to be altered or submitted.
3. LinkedList.obj contains a compiled solution that will allow you to test existing functionality. It needs to be removed from your project for you to test your own implementation
|
V
https://www.mediafire.com/folder/s8snn01u7zxvf5z,s197fojdx2kogo2,a75wovfge1j2udo/shared
Your submission should contain five functions, including a constructor and destructor.
Explanation / Answer
Constructor:
LinkedList()
{
head = null;
}
Deconstructor:
~LinkedList()
{
Node* temp;
for(;head;head = temp)
{
temp = head->next;
delete head;
}
}
Diplay of LinkedList:
void LinkedList:display()
{
Node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
Insert into LinkedList:
void LinkedList:insert(int data)
{
if (head == nullptr)
{
Node temp = {data,null}
head = temp;
}
else
{
Node temp = {data,null}
// First find a pointer to the last item in the chain.
Node* last = head;
while(last->next)
{
last = last->next;
}
// Now just add the node.
last->next = temp;
}
}
Search in LinkedList:
int LinkedList:searchList(int item)
{
Node *temp=head;
while(temp!=NULL)
{
if(temp->data==item){
temp =null;
return 1;
}
temp=temp->next;
}
return 0;
}