Guest User

Untitled

a guest
Jul 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. /***********************
  2. Name: Mary Soy
  3. Date: 10/26/09
  4. Class: CSCI 3333
  5. Professor: Schweller
  6. File: LinkedList.h
  7. ************************/
  8. #ifndef H_LinkedList
  9. #define H_LinkedList
  10.  
  11. #include <iostream>
  12. #include <cassert>
  13. #include <iomanip>
  14. using namespace std;
  15.  
  16. struct StudentNode
  17. {
  18. int id;
  19. double GPA;
  20. StudentNode * next;
  21.  
  22. //Constructor with 2 parameters.
  23. StudentNode(int i = 0, double d = 0.0)
  24. {
  25. id = i;
  26. GPA = d;
  27. next = NULL;
  28. }
  29. };
  30.  
  31. class LinkedList
  32. {
  33. public:
  34. //insert student with data into linkedlist
  35. void insertStudent(StudentNode s);
  36. //returns the gpa of a studen with a given id.
  37. //return -1 if the student is not in the data structure.
  38. double getGPA(StudentNode s);
  39. //change the gpa of the student with the given id to newGPA
  40. void updateGPA(StudentNode s);
  41. //destroys all nodes in a list
  42. void destroyList();
  43. //displays the content of a list
  44. void printLL();
  45. //return true if list is empty
  46. bool isEmpty();
  47.  
  48. //default constructor initialized head to NULL
  49. LinkedList();
  50. //destructor deletes all nodes from the list
  51. ~LinkedList();
  52.  
  53. private:
  54. //he head & tail of a list
  55. StudentNode *head;
  56. StudentNode *tail;
  57. int numStudent;
  58. };
  59.  
  60. LinkedList::LinkedList()
  61. {
  62. head = NULL;
  63. tail = NULL;
  64. numStudent = 0;
  65. }
  66.  
  67. LinkedList::~LinkedList()
  68. {
  69. destroyList();
  70. }
  71.  
  72. void LinkedList::destroyList()
  73. {
  74. StudentNode * temp;
  75.  
  76. while( head != NULL )
  77. {
  78. temp = head;
  79. head = head->next;
  80. delete temp;
  81. }
  82.  
  83. tail = NULL;
  84. numStudent = 0;
  85. }
  86.  
  87. void LinkedList::insertStudent(StudentNode s)
  88. {
  89. StudentNode * newNode;
  90. newNode = new StudentNode(s);
  91.  
  92. assert(newNode != NULL);
  93.  
  94. if( head == NULL)
  95. {
  96. head = newNode;
  97. tail = newNode;
  98. numStudent++;
  99. }
  100. else
  101. {
  102. tail->next = newNode;
  103. tail = newNode;
  104. numStudent++;
  105. }
  106. }
  107.  
  108. void LinkedList::printLL()
  109. {
  110. StudentNode * traverse;
  111. traverse = new StudentNode();
  112. traverse = head;
  113.  
  114. while( traverse != NULL )
  115. {
  116. cout << "Student with id# " << traverse->id
  117. << " and a GPA of: " << fixed << showpoint << setprecision(1) << traverse->GPA << ".\n";
  118. traverse = traverse->next;
  119. }
  120. }
  121.  
  122. bool LinkedList::isEmpty()
  123. {
  124. if (head == NULL)
  125. {
  126. return false;
  127. }
  128. else
  129. {
  130. return true;
  131. }
  132. }
  133.  
  134. double LinkedList::getGPA(StudentNode s)
  135. {
  136. StudentNode * traverse = head;
  137. while(traverse != NULL)
  138. {
  139. if(traverse->id == s.id)
  140. {
  141. cout << "The GPA of student id # " << traverse->id << " is: ";
  142. return traverse->GPA;
  143. }
  144. traverse = traverse->next;
  145. }
  146.  
  147. return -1;
  148. }
  149.  
  150. void LinkedList::updateGPA(StudentNode s)
  151. {
  152. StudentNode * traverse = head;
  153. bool found = false;
  154. while(traverse != NULL)
  155. {
  156. if(traverse->id == s.id)
  157. {
  158. traverse->GPA = s.GPA;
  159. found = true;
  160. }
  161. traverse = traverse->next;
  162. }
  163. if(found == false)
  164. {
  165. cout << "Student with id: " << s.id << " could not be found.\n";
  166. }
  167. }
  168.  
  169. #endif
Add Comment
Please, Sign In to add comment