Help w/ dynamic list funtction definitions

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

Join Date: Dec 2004
Posts: 1
Reputation: pacbeach is an unknown quantity at this point 
Solved Threads: 0
pacbeach pacbeach is offline Offline
Newbie Poster

Help w/ dynamic list funtction definitions

 
0
  #1
Dec 15th, 2004
I'm having trouble defining the functions get_last, delete_last, and add_item. I've been working on the program for days and can't get the function definitions to work at all, so i left them blank. COULD SOMEBODY PLEASE HELP!!!

This program is suppose to define a List class that holds a list of values of type double. The class contains a default constructor that initializes the list to empty with a default maximum size, a constructor with a single argument of type int that specifies the maximum number of entries in the list (the list is initialized to empty), a member function add_item that adds an item to the end of the list, a member function full that returns true if the list is full and false otherwise, a member function get_size that returns the number of values in the list, a member function get_last that returns the value of the last item in the list, and a member function delete_last that deletes the last item on the list. The operator << is overloaded to print objects of type List. The List is implemented using dynamic arrays. Furthermore, the list is itself dynamic in that if there is an attempt to add to a full list the maximum size of the list is doubled, and if deletion causes the size to drop below half of the maximumnumber of entries the maximum size is reduced to half of the original.
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. const int MAX_LIST_SIZE = 50;
  9.  
  10. typedef double* ArrayPtr;
  11.  
  12. class List
  13. {
  14. public:
  15. List();
  16. // Initializes the List object to an empty list with size MAX_LIST_SIZE
  17.  
  18. List(int maxSize);
  19. // Initializes the List object to be a list with at most
  20. // maxSize entries.
  21.  
  22. void add_item(double number);
  23. // Precondition: number has a value
  24. // Postcondition: The argument number has been added to
  25. // the list. If the list was full before adding, its maximum
  26. // size is double what it was before adding.
  27.  
  28. bool full() const;
  29. // Returns true if the list if full; false otherwise.
  30.  
  31. friend ostream& operator <<(ostream& outs, const List& theObject);
  32. // Overloads the << operator so it can be used to output
  33. // values of type List.
  34. // Precondition: If outs is a file stream then outs has
  35. // already been connected to a file.
  36.  
  37. int get_size() const;
  38. // Returns the number of values in the list.
  39.  
  40. int get_max() const;
  41. // Returns the maximum number of entries in the list.
  42.  
  43. double get_last ();
  44. // Precondition: The List object is defined and non-empty.
  45. // Returns the last value in the list.
  46.  
  47. void delete_last();
  48. // Precondition: The List object is defined.
  49. // Postcondition: If the list was nonempty, the last item has
  50. // been deleted from the list array and size has been decremented;
  51. // otherwise the list remains empty. If deletion of the item
  52. // caused the size of the list to drop below half the maximum,
  53. // then the maximum is reduced to half the original and the
  54. // amount of space used to store the array is half what it was.
  55.  
  56.  
  57. private:
  58. ArrayPtr list;
  59. int max; // maximum number of elements
  60. int size; // number of array positions filled
  61. };
  62.  
  63.  
  64. int main()
  65. {
  66. //
  67. // Variable declarations
  68. //
  69. double num; // a number to be added to the list
  70. char ans; // loop control
  71. int size;
  72.  
  73. cout << endl << "Testing the List Class" << endl << endl;
  74.  
  75. //
  76. // Get the maximum number of entries and create a List object
  77. //
  78. cout << "What is the maximum number of entries in the list? ";
  79. cin >> size;
  80. cout << endl;
  81.  
  82. List testList(size);
  83.  
  84. //
  85. // Let the user test add_item, delete_last, and get_last as long
  86. // as he/she wishes
  87. //
  88. do
  89. {
  90. cout << endl;
  91. cout << "Enter a to add to the list, d to delete the last entry, " << endl;
  92. cout << "p to print the last item (without deleting), or s to stop: ";
  93. cin >> ans;
  94.  
  95. if (ans == 'a' || ans == 'A')
  96. {
  97. //
  98. // Get the number to be added, add it, then output the list
  99. //
  100. cout << "Enter a number to add to the list: ";
  101. cin >> num;
  102. testList.add_item(num);
  103. }
  104. else if (ans == 'd' || ans == 'D')
  105. testList.delete_last();
  106. else if (ans == 'p' || ans == 'P')
  107. cout << endl << "The last item in the list is "
  108. << testList.get_last() << endl;
  109. else if (ans != 's' && ans != 'S')
  110. cout << "Not a valid response - try again!" << endl;
  111.  
  112. cout << endl << "Current List" << endl;
  113. cout << "Capacity: " << testList.get_max() << " Size: "
  114. << testList.get_size() << endl;
  115. cout << "List entries: " << testList << endl;
  116.  
  117. }
  118. while (ans != 's' && ans != 'S');
  119.  
  120. return 0;
  121. }
  122.  
  123. List::List()
  124. {
  125. size = 0;
  126. max = MAX_LIST_SIZE;
  127. }
  128.  
  129.  
  130. List::List (int maxSize)
  131. {
  132. max = maxSize;
  133. size = 0;
  134. list = new double[maxSize];
  135. }
  136. void add_item(double number)
  137. {
  138. //???????????????
  139. }
  140.  
  141. double get_last()
  142. {
  143. //??????????????
  144. }
  145.  
  146. void delete_last()
  147. {
  148. //??????????????
  149. }
  150.  
  151. bool List::full() const
  152. {
  153. return (size == max);
  154. }
  155.  
  156.  
  157. ostream& operator <<(ostream& outs, const List& theObject)
  158. {
  159. for (int i = 0; i < theObject.size; i++)
  160. outs << theObject.list[i] << " ";
  161. return outs;
  162. }
  163.  
  164.  
  165. int List::get_size() const
  166. {
  167. return size;
  168. }
  169.  
  170.  
  171. int List::get_max() const
  172. {
  173. return max;
  174. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Help w/ dynamic list funtction definitions

 
0
  #2
Dec 18th, 2004
i have a post on the c/c++ snippets about linked lists. it might help!
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC