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

LINKED LIST C++ I have created two header file but both are showing some error a

ID: 3810645 • Letter: L

Question

LINKED LIST C++

I have created two header file but both are showing some error and I am also not sure which will work perfectly. I am also attaching driver file which was given with this assignment.

----------My Work------Header file (.h)

/* Header for Hospital linked list

LinkedList.h file

Abhijeet Solanki

*/

//Guard

#indef Linked_List_h

#define Linked_List_h

//Node

struct Node(

string name;

Node* next;

);

//Call to functions

class patientList{

public:

  void insert(string);

  void remove(string);

  void print(string);

  void printAll(string);

private:

Int Itm;

Node* head;

Node* curr;

};

#endif

-----------My work--------- Header file (.h)

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

#include

#include

using namespace std;

struct node{

int data;

struct node *next;

}*head;

  

class LinkedList

{

public:

LinkedList(); // defualt constructor

struct node *create(string name);

void insert(string name);

void remove(string name);

void print(string name);

void printAll();

};

#endif

ASSIGNMENT DETAIL

the LinkedList class should use a pointer based LinkedList, that will need a constructor and destructor, for our insertion methods, insert at the beginning of the list. without modifying the LinkedList-main.cpp , fix the the LinkedList.h (header File) and create a LinkedList.cpp (implementation) file.

the driver code produces these following commands

A (admits patient NAME to the beginning of the list)

D (discharges patient NAME if NAME is in the list, silently continues otherwise)

F (prints patient NAME if found in the list)

V (visits all patients and displays their names in list order)

and the input file is

V

F Sophia

D Sophia

A Sophia

V

F Sophia

D Sophia

V

F Sophia

D Sophia

A Jackson

A Emma

A Aiden

A Olivia

A Lucas

V

D Jackson

D Emma

D Aiden

D Olivia

D Lucas

V

Explanation / Answer

So you are asking to write the header file and the implementation of it i.e. the LinkedList.cpp will be with you as you said execution gives an error, am I right?

See, there are few reasons why both of these header files showing you an error while you execute the driver with either of them. In simple terms, the reason is int in place of string. (in second header file).

See in your driver class, you have some methods like insert(name), remove(name), print(name) which means these methods of linked list are taking a string argument right (as the name is string so..)

Like if you want to find someone with his/her name, the node of the linked list must have that value. But inside your node struct, you have defined data to be an integer. So, we need to change the data from int to string. And to make sure that 'string' is recognized, as it is not the basic datatype, you should include string header file inside your LinkedList.h.

One more thing I want to add here. You have defined a class of LinkedList having 2 nodes: head and curr(in header file 1). This is not as per the logic because the linked list only has the head node accessible, you don't save the current node inside the class, then also the methods will be working.

The methods definition you have written are almost perfect, just one modification I want to do. In the method printAll(), you will not provide any string, you are supposed to print every values from the list nodes so you do not need to find a particular string for that, having *head is enough for this task. (header file 1).

So it seems that 1 or 2 modifications in header file 1 are needed. Similar case is with header file 2. So I am writing a new header file for this, check it out and see if it runs with your code. As per me, it won't give any error. Just see it:

LinkedList.h

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED
#include<string>
using namespace std;
struct node
{
string data;
node *next;
};

class LinkedList
{
private:
struct node *head;

public:
LinkedList();
struct node *create(string);
void insert(string);
void remove(string);
void print(string);
void printAll();
};


#endif // LINKEDLIST_H_INCLUDED

This solves the errors I described above and is the solution to this question.

Try this and do comment if there is any query. Thank you. :)