i wonder how to initialise some value in the linklist ,before insert the new node value?

#include <iostream>
#include <conio.h>


using namespace std;

struct employeeInfo
{
       string name;
       int id;
       string department;
       employeeInfo *next;
};
const int SIZE = 10;
employeeInfo employed[SIZE] = {{"Michael bay",1234,"Design"},{"Enrique",5678,"Production"},{"Fernando ",9012,"Management"}};

class List {
	public:
		List(void) { head = NULL; } // constructor
		~List(void); // destructor
		bool IsEmpty() { return head == NULL; }

		employeeInfo* InsertEmployee(string ,int ,string );
		void DisplayList(void);
        
  	 private:
		employeeInfo* head;
};

List::~List(void) {
	employeeInfo* currNode = head, *nextNode = NULL;
	while (currNode != NULL) {
		nextNode = currNode->next;
		// destroy the current node
		delete currNode;
		currNode = nextNode;
	}
}

employeeInfo* List::InsertEmployee(string nama,int number,string affiliate) {
	int currIndex = 0;
	employeeInfo* currNode = head;
	employeeInfo* prevNode = NULL;
	while (currNode) {
		prevNode = currNode;
      currNode = currNode->next;
		currIndex++;
	}

   employeeInfo* newNode = new employeeInfo;
	newNode->name = nama;
	newNode->id=number;
	newNode->department= affiliate;
	if (currIndex == 0) {
		newNode->next = head;
		head = newNode;
  	 } else {
		newNode->next = prevNode->next;
		prevNode->next = newNode;
   	}
 return newNode;
}
 
 void List::DisplayList()
{
	int num = 0;
	employeeInfo* currNode = head;
		cout << "Employee Name :"<<currNode->name <<endl;
		cout << "Employee ID   :"<<currNode->id<<endl;;
		cout << "Age           :"<<currNode->department<<endl;
		currNode = currNode->next;
}     


int main()
{
    List test;
   
    test.DisplayList();
    getch();
    return 0;
}

The "employed" object in the above codeing is a sets of value which i want to initialised with the linklist before i want to insert the new Employee during runtime of the program.

Why not just 'add' the records you want in main, after you construct the list?

int main()
{
    List test;

    test.InsertEmployee("Michael bay",1234,"Design");
    test.InsertEmployee("Enrique",5678,"Production");
    test.InsertEmployee("Fernando ",9012,"Management");
   
    test.DisplayList();
    getch();
    return 0;
}

Note that DisplayList() as written is broken and will only display one record.

I think that InsertEmployee() will work, though it could be simplified a little.

An alternative to the above hard-coded calls to InsertEmployee would be to get the data to add from the array you built, but you're going to have to call InsertEmployee for each of the data items anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.