i have 2 class , and i want to do linked-list for each class contains the objects of its class .. i see many link and many youtube , but nothing help. what is I want : change the below code to make linkedlist conations object not variable
1- Passenger linked-list contains Passenger objects for insert - delete -search - update 2- City linked-list contains City objects for insert - delete -search - update
the code in https://www.onlinegdb.com/ here
#include <iostream>
#include <string.h>
using namespace std;
// Class Passenger
class Passenger
{
public:
string Name;
int ID;
string contactInfo;
string bookingRef;
public:
// Setter
void setName(string Name)
{
this->Name = Name;
}
void setID(int ID)
{
this->ID = ID;
}
void setContactInfo(string contactInfo)
{
this->contactInfo = contactInfo;
}
void setBookingRef(string bookingRef)
{
this->bookingRef = bookingRef;
}
// Getter
string getName()
{
return Name;
}
int getID()
{
return ID;
}
string getContactInfo()
{
return contactInfo;
}
string getBookingRef()
{
return bookingRef;
}
void display()
{
}
};
// Class City
class City
{
public:
string Name;
string cityCode;
string countryCode;
public:
// Setter
void setName(string Name)
{
this->Name = Name;
}
void setCityCode(string cityCode)
{
this->cityCode = cityCode;
}
void setCountryCode(string countryCode)
{
this->countryCode = countryCode;
}
// Getter
string getName()
{
return Name;
}
string getCityCode()
{
return cityCode;
}
string getCountryCode()
{
return countryCode;
}
void display()
{
}
};
// Node class to represent
// a node of the linked list.
class Node
{
public:
public:
double data; // data
Node *next; // pointer to next
};
// Linked list class to
// implement a linked list.
class List
{
public:
List(void) { head = NULL; } // constructor
~List(void); // destructor
bool IsEmpty() { return head == NULL; }
Node *InsertNode(int index, double x);
int FindNode(double x);
int DeleteNode(double x);
void DisplayList(void);
private:
Node *head;
};
Node *List::InsertNode(int index, double x)
{
if (index < 0)
return NULL;
int currIndex = 1;
Node *currNode = head;
while (currNode && index > currIndex)
{
currNode = currNode->next;
currIndex ;
}
if (index > 0 && currNode == NULL)
return NULL;
Node *newNode = new Node;
newNode->data = x;
if (index == 0)
{
newNode->next = head;
head = newNode;
}
else
{
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
int List::FindNode(double x)
{
Node *currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x)
{
currNode = currNode->next;
currIndex ;
}
if (currNode)
return currIndex;
return 0;
}
int List::DeleteNode(double x)
{
Node *prevNode = NULL;
Node *currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x)
{
prevNode = currNode;
currNode = currNode->next;
currIndex ;
}
if (currNode)
{
if (prevNode)
{
prevNode->next = currNode->next;
delete currNode;
}
else
{
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
void List::DisplayList()
{
int num = 0;
Node *currNode = head;
while (currNode != NULL)
{
cout << currNode->data << endl;
currNode = currNode->next;
num ;
}
cout << "Number of nodes in the list: " << num << endl;
}
int main()
{
List list;
list.InsertNode(0, 7.0); // successful
list.DisplayList();
if (list.FindNode(5.0) > 0)
cout << "5.0 found" << endl;
list.DeleteNode(7.0);
list.DisplayList();
return 0;
}
CodePudding user response:
You can do this one of 2 ways (there are other odd ones but these are the two obvious ones). This is all coming from you saying that you are not permitted to use the standard STL list
- either you can create 2 class sets, one for Passenger one for City
- create a templated Class set
(By 'class set' I mean List and Node objects)
The 'correct' way is the second one, this allowed you to store any type in the list, so if you need to add Country or Train objects later you can do it easily.
I hope that you have covered templated classes in c , if not there are a ton of resources on the net. Its too complicated to write a tutorial here
The 'simple' but long winded way is to create a 'dedicated' class set for each object type.
So you would have
class PassengerNode{
Passenger p;
PassengerNode *_next;
}
class PassengerList{
PassengerNode *_head;
...
}
You might want to store pointers to Passenger objects by using std::unique_ptr or std::shared_ptr instead of storing the actual object in the node - it you have to do alot of moving of nodes you will keep creating new copies and thats expensive and bug prone (you can end up operating on different copies of what you think are the 'same' object)
CodePudding user response:
I have like two recommendation to you
first one: declare LinkedList to store object
LinkedList<object> ls=new LinkedList<>();
and insert your objects trough the getter and setter
second approach (I like)
build your own linkedlist from scratch and do struct for your classes to make them as datatype
you can find how to make your own linkedlist https://www.tutorialspoint.com/cplusplus-program-to-implement-singly-linked-list