Печать записи сотрудника, сохраненной в связанном списке - PullRequest
0 голосов
/ 01 мая 2020

** Я храню данные о классе сотрудника (имя, адрес электронной почты, номер телефона) на узле в единственном списке. пожалуйста, подскажите, как распечатать такой список ... спасибо

здесь используется функция печати, которую я использовал ранее для печати целочисленных данных, здесь я пытаюсь сохранить данные о сотрудниках на узлах в списке и я не уверен в том, как правильно печатать данные типа класса из списка. **

#include<iostream>
using namespace std;
class Employee{
    private:
        int EID;
        string name;
        int Ephone;
    public:
        Employee(){

        }
        Employee(int id, string n,int Ephone){
            this->EID=id;
            this->name=n;
            this->Ephone=Ephone;
        }
};
class Node{
    private:
        Employee data;
        Node* next;
    public:
        void SetData(Employee);
        Employee GetData();
        void SetNext(Node*);
        Node* GetNext();
};
void Node::SetData(Employee info){
    data=info;
}
Employee Node::GetData(){
    return this->data;
}
void Node::SetNext(Node* ptr){
    this->next=ptr;
}
Node* Node::GetNext(){
    return next;
}
class List{
    private:
        Node* current;
    public:
        List();
        void insertData(Employee);
        void PrintList();

};
List::List(){
    current=NULL;
}
void List::insertData(Employee data){
    Node *newnode=new Node();
    newnode->SetData(data);
    newnode->SetNext(NULL);

    Node *temp=current;
    if(current!=NULL){
        while(temp->GetNext()!=NULL){
            temp=temp->GetNext();
        }
        temp->SetNext(newnode);
    }
    else{
        current=newnode;
    }
}

void List::PrintList(){
    int count=0;
    Node *temp=current;
    if(temp->GetNext()==NULL){
        cout<<temp->GetData();
        cout<<" -> ";
        cout<<"NULL";
    }
    else{
        do{

                cout<<temp->GetData();
                cout<<" -> ";
                temp=temp->GetNext();
                count++;

        }while(temp!=NULL);
        cout<<" NULL ";
    }
    cout<<"\n Elements in the list: "<<count<<endl;
}
int main(){
    Employee emp(1,"tony",031245);
    List l1;
    l1.insertData(emp);
    return 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...