954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

initialise value in linklist

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.

low1988
Light Poster
44 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You