Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. // Specification file for the ShoppingList class
  7. #ifndef SHOPPINGLIST_H
  8. #define SHOPPINGLIST_H
  9.  
  10. class ShoppingList
  11. {
  12. private:
  13. // Declare a structure for the list
  14. struct ListNode
  15. {
  16. char name[25];
  17. struct ListNode *next;
  18. };
  19.  
  20. ListNode *head;
  21.  
  22. public:
  23. // Constructor
  24. ShoppingList()
  25. { head = NULL; }
  26.  
  27. // Destructor
  28. ~ShoppingList();
  29.  
  30. // Linked list operations
  31. void appendNode(char Name[]);
  32. void displayList() const;
  33. };
  34. #endif
  35.  
  36. void ShoppingList::appendNode(char Name[])
  37. {
  38. ListNode *newNode; // To point to a new node
  39. ListNode *nodePtr; // To move through the list
  40.  
  41. // Allocate a new node and store item there.
  42. newNode = new ListNode;
  43. strcpy( newNode->name, Name );
  44. newNode->next = NULL;
  45.  
  46. // If there are no nodes in the list make newNode the first node.
  47. if (!head)
  48. head = newNode;
  49. else
  50. {
  51. // Initialize nodePtr to head of list.
  52. nodePtr = head;
  53.  
  54. // Find the last node in the list.
  55. while (nodePtr->next)
  56. {
  57. if(nodePtr->name > newNode->name)
  58. {
  59. newNode->
  60. }
  61. nodePtr = nodePtr->next;
  62. }
  63.  
  64.  
  65. // Insert newNode as the last node.
  66. nodePtr->next = newNode;
  67.  
  68.  
  69.  
  70. }
  71. }
  72.  
  73. void ShoppingList::displayList() const
  74. {
  75. ListNode *nodePtr; //To move through list
  76.  
  77. //Position nodePtr at the head of the list.
  78. nodePtr = head;
  79.  
  80. //While nodePtr points to a node, traverse the list.
  81. while (nodePtr)
  82. {
  83. //Display the value in this node.
  84. cout << nodePtr->name << endl;
  85.  
  86.  
  87. // Move to the next node.
  88. nodePtr = nodePtr->next;
  89. }
  90. }
  91.  
  92.  
  93.  
  94. ShoppingList::~ShoppingList()
  95. {
  96. ListNode *nodePtr;
  97. ListNode *nextNode;
  98.  
  99. nodePtr = head;
  100.  
  101. while (nodePtr != NULL)
  102. {
  103. nextNode = nodePtr->next;
  104.  
  105. delete nodePtr;
  106.  
  107. nodePtr = nextNode;
  108. }
  109. }
  110.  
  111. int displayMenu()
  112. {
  113. int selection = 0; // stores the menu option number
  114.  
  115. // display the menu
  116. cout << "\nSelect one of the following options" << endl;
  117. cout << "1 - Enter new item" << endl;
  118. cout << "2 - Display list" << endl;
  119. cout << "3 - Exit the application" << endl;
  120. cout << "Enter selection: ";
  121. cin >> selection;
  122.  
  123. return selection; // return the selection
  124.  
  125. } // end function displayMenu
  126.  
  127. int main()
  128.  
  129. {
  130. int selection=0;
  131. ShoppingList list;
  132. char itemName[25];
  133. while ( selection != 3 )
  134. {
  135. selection = displayMenu();
  136.  
  137. switch ( selection )
  138. {
  139. // enter item information
  140. case 1:
  141. cin.ignore();
  142.  
  143. cout<<"\nEnter item name: ";
  144. cin>>itemName;
  145.  
  146.  
  147. list.appendNode(itemName);
  148. break;
  149.  
  150. // view all items in the list
  151. case 2:
  152. list.displayList();
  153. break;
  154.  
  155. default:
  156. cout << "\nError: Enter a valid menu selection";
  157.  
  158. } // end switch
  159.  
  160. } // end while
  161.  
  162.  
  163. //Display the list.
  164. cout << "Here are the items on the shopping list:\n";
  165.  
  166. cout << endl;
  167.  
  168.  
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement