| | |
initialise value in linklist
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 33
Reputation:
Solved Threads: 0
i wonder how to initialise some value in the linklist ,before insert the new node value?
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.
C++ Syntax (Toggle Plain Text)
#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.
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
Why not just 'add' the records you want in main, after you construct the list?
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.
c++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- laptop fails to initialise (Troubleshooting Dead Machines)
- linklist : find a node by its variable (C++)
- how can i Initialise 2 dimentional integer array in visual basic 6 (Visual Basic 4 / 5 / 6)
- Need Quick Help LinkList (C#)
- inserting a node befora certain node in doubly linklist (C++)
- Want to initialise text box on basis of another textbox value. (ASP.NET)
- Isass.exe unable to initialise (Troubleshooting Dead Machines)
- Yet another initialise question (Java)
Other Threads in the C++ Forum
- Previous Thread: control reaches end of non-void function
- Next Thread: Help With this program
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





