Please help me fix my compile errors , C++ linkedlist program is written but i c
ID: 3810630 • Letter: P
Question
Please help me fix my compile errors , C++ linkedlist program is written but i cant fix this last error
LinkedList.h File
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <string>
using namespace std;
struct node
{
int data;
struct node *next;
}*head;
class LinkedList
{
public:
LinkedList();
struct node *create(string name);
void insert(string name);
void remove(string name);
void print(string name);
void printAll();
};
#endif
LinkedList.cpp
#include "LinkedList.h"
#include <iostream>
#include <string>
using namespace std;
class LinkedList
{
LinkedList()
{
head = NULL;
}
struct node *create(string name)
{
struct node *t;
t = new(struct node);
if (t == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
t->data = name;
t->next = NULL;
return t;
}
}
void insert(string name)
{
struct node *t, *p;
t = create(name);
if (head == NULL)
{
head = t;
head->next = NULL;
}
else
{
p = head;
head = temp;
head->next = p;
}
}
void remove(string name)
{
struct node *pre = NULL, *del = NULL;
if (_head->data == name) {
del = head;
head = del->next;
delete del;
return;
}
pre = head;
del = head->next;
while (del != NULL) {
if (del->data == name) {
pre->next = del->next;
delete del;
break;
}
pre = del;
del = del->next;
}
}
void print(string name)
{
bool f = false;
if (head == NULL)
{
cout<<"There are no entries in the list to display"<<endl;
return;
}
cout << "Displaying the single entry in the list: "endl;
cout<< name <<endl;
struct node *t;
t = head;
while (t != NULL)
{
if (t->data == name)
{
f = true;
cout<< name << "is in the List "<<endl;
}
t = t->next;
}
if (!f)
cout<<name<<" is not found in the list"<<endl;
}
void printAll()
{
int count =0;
struct node *t;
if (head == NULL)
{
cout<<"There are no entries in the list to display"<<endl;
return;
}
t = head;
while (temp != NULL)
{
count++;
t = t->next;
}
cout<<"Displaying all" <<count << "the entries in the list: "<<endl;
while (temp != NULL)
{
cout<<t->data<<endl;
t = t->next;
}
cout<<"There are no entries in the list to display"<<endl;
}
};
LinkedList-main.cpp
Explanation / Answer
I fixed erros in the file LinkedList.h and LinkedList.cpp
//LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <string>
using namespace std;
//chegg EA , declared head as private member of LinkedList
struct node
{
//declare data as string , as needed by program
//int data;
string data;
struct node *next;
};
class LinkedList
{
struct node *head;
public:
LinkedList();
struct node *create(string name);
void insert(string name);
void remove(string name);
void print(string name);
void printAll();
};
#endif
--------------------------------------------------------------------------
//LinkedList.cpp
//LinkedList.cpp
#include "LinkedList.h"
#include <iostream>
#include <string>
using namespace std;
LinkedList:: LinkedList()
{
head = NULL;
}
struct node *LinkedList::create(string name)
{
struct node *t;
t = new(struct node);
if (t == NULL)
{
cout << "Memory not allocated " << endl;
return 0;
}
else
{
t->data = name;
t->next = NULL;
return t;
}
}
void LinkedList::insert(string name)
{
struct node *t, *p,*temp = NULL;
t = create(name);
if (head == NULL)
{
head = t;
head->next = NULL;
}
else
{
p = head;
head = t;
head->next = p;
}
}
void LinkedList::remove(string name)
{
struct node *pre = NULL, *del = NULL;
if (head->data == name) {
del = head;
head = del->next;
delete del;
return;
}
pre = head;
del = head->next;
while (del != NULL) {
if (del->data == name) {
pre->next = del->next;
delete del;
break;
}
pre = del;
del = del->next;
}
}
void LinkedList::print(string name)
{
bool f = false;
if (head == NULL)
{
cout << "There are no entries in the list to display" << endl;
return;
}
cout << "Displaying the single entry in the list: "<<endl;
cout << name << endl;
struct node *t;
t = head;
while (t != NULL)
{
if (t->data == name)
{
f = true;
cout << name << "is in the List " << endl;
}
t = t->next;
}
if (!f)
cout << name << " is not found in the list" << endl;
}
void LinkedList::printAll()
{
int count = 0;
struct node *t,*temp;
if (head == NULL)
{
cout << "There are no entries in the list to display" << endl;
return;
}
t = head;
//t is initialized so , use t in the logic of the program
while (t != NULL)
{
count++;
t = t->next;
}
cout << "Displaying all" << count << "the entries in the list: " << endl;
while (t != NULL)
{
cout << t->data << endl;
t = t->next;
}
cout << "There are no entries in the list to display" << endl;
}
I didn't change main file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.