initialise value in linklist

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 33
Reputation: low1988 is an unknown quantity at this point 
Solved Threads: 0
low1988 low1988 is offline Offline
Light Poster

initialise value in linklist

 
0
  #1
Sep 28th, 2009
i wonder how to initialise some value in the linklist ,before insert the new node value?

  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. struct employeeInfo
  8. {
  9. string name;
  10. int id;
  11. string department;
  12. employeeInfo *next;
  13. };
  14. const int SIZE = 10;
  15. employeeInfo employed[SIZE] = {{"Michael bay",1234,"Design"},{"Enrique",5678,"Production"},{"Fernando ",9012,"Management"}};
  16.  
  17. class List {
  18. public:
  19. List(void) { head = NULL; } // constructor
  20. ~List(void); // destructor
  21. bool IsEmpty() { return head == NULL; }
  22.  
  23. employeeInfo* InsertEmployee(string ,int ,string );
  24. void DisplayList(void);
  25.  
  26. private:
  27. employeeInfo* head;
  28. };
  29.  
  30. List::~List(void) {
  31. employeeInfo* currNode = head, *nextNode = NULL;
  32. while (currNode != NULL) {
  33. nextNode = currNode->next;
  34. // destroy the current node
  35. delete currNode;
  36. currNode = nextNode;
  37. }
  38. }
  39.  
  40. employeeInfo* List::InsertEmployee(string nama,int number,string affiliate) {
  41. int currIndex = 0;
  42. employeeInfo* currNode = head;
  43. employeeInfo* prevNode = NULL;
  44. while (currNode) {
  45. prevNode = currNode;
  46. currNode = currNode->next;
  47. currIndex++;
  48. }
  49.  
  50. employeeInfo* newNode = new employeeInfo;
  51. newNode->name = nama;
  52. newNode->id=number;
  53. newNode->department= affiliate;
  54. if (currIndex == 0) {
  55. newNode->next = head;
  56. head = newNode;
  57. } else {
  58. newNode->next = prevNode->next;
  59. prevNode->next = newNode;
  60. }
  61. return newNode;
  62. }
  63.  
  64. void List::DisplayList()
  65. {
  66. int num = 0;
  67. employeeInfo* currNode = head;
  68. cout << "Employee Name :"<<currNode->name <<endl;
  69. cout << "Employee ID :"<<currNode->id<<endl;;
  70. cout << "Age :"<<currNode->department<<endl;
  71. currNode = currNode->next;
  72. }
  73.  
  74.  
  75. int main()
  76. {
  77. List test;
  78.  
  79. test.DisplayList();
  80. getch();
  81. return 0;
  82. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: initialise value in linklist

 
0
  #2
Sep 29th, 2009
Why not just 'add' the records you want in main, after you construct the list?

  1. int main()
  2. {
  3. List test;
  4.  
  5. test.InsertEmployee("Michael bay",1234,"Design");
  6. test.InsertEmployee("Enrique",5678,"Production");
  7. test.InsertEmployee("Fernando ",9012,"Management");
  8.  
  9. test.DisplayList();
  10. getch();
  11. return 0;
  12. }

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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC