944,005 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 259
  • C++ RSS
Sep 28th, 2009
0

initialise value in linklist

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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
low1988 is offline Offline
44 posts
since Mar 2009
Sep 29th, 2009
0

Re: initialise value in linklist

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

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: control reaches end of non-void function
Next Thread in C++ Forum Timeline: Help With this program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC