Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <cstring>
- using namespace std;
- // Specification file for the ShoppingList class
- #ifndef SHOPPINGLIST_H
- #define SHOPPINGLIST_H
- class ShoppingList
- {
- private:
- // Declare a structure for the list
- struct ListNode
- {
- char name[25];
- struct ListNode *next;
- };
- ListNode *head;
- public:
- // Constructor
- ShoppingList()
- { head = NULL; }
- // Destructor
- ~ShoppingList();
- // Linked list operations
- void appendNode(char Name[]);
- void displayList() const;
- };
- #endif
- void ShoppingList::appendNode(char Name[])
- {
- ListNode *newNode; // To point to a new node
- ListNode *nodePtr; // To move through the list
- // Allocate a new node and store item there.
- newNode = new ListNode;
- strcpy( newNode->name, Name );
- newNode->next = NULL;
- // If there are no nodes in the list make newNode the first node.
- if (!head)
- head = newNode;
- else
- {
- // Initialize nodePtr to head of list.
- nodePtr = head;
- // Find the last node in the list.
- while (nodePtr->next)
- {
- if(nodePtr->name > newNode->name)
- {
- newNode->
- }
- nodePtr = nodePtr->next;
- }
- // Insert newNode as the last node.
- nodePtr->next = newNode;
- }
- }
- void ShoppingList::displayList() const
- {
- ListNode *nodePtr; //To move through list
- //Position nodePtr at the head of the list.
- nodePtr = head;
- //While nodePtr points to a node, traverse the list.
- while (nodePtr)
- {
- //Display the value in this node.
- cout << nodePtr->name << endl;
- // Move to the next node.
- nodePtr = nodePtr->next;
- }
- }
- ShoppingList::~ShoppingList()
- {
- ListNode *nodePtr;
- ListNode *nextNode;
- nodePtr = head;
- while (nodePtr != NULL)
- {
- nextNode = nodePtr->next;
- delete nodePtr;
- nodePtr = nextNode;
- }
- }
- int displayMenu()
- {
- int selection = 0; // stores the menu option number
- // display the menu
- cout << "\nSelect one of the following options" << endl;
- cout << "1 - Enter new item" << endl;
- cout << "2 - Display list" << endl;
- cout << "3 - Exit the application" << endl;
- cout << "Enter selection: ";
- cin >> selection;
- return selection; // return the selection
- } // end function displayMenu
- int main()
- {
- int selection=0;
- ShoppingList list;
- char itemName[25];
- while ( selection != 3 )
- {
- selection = displayMenu();
- switch ( selection )
- {
- // enter item information
- case 1:
- cin.ignore();
- cout<<"\nEnter item name: ";
- cin>>itemName;
- list.appendNode(itemName);
- break;
- // view all items in the list
- case 2:
- list.displayList();
- break;
- default:
- cout << "\nError: Enter a valid menu selection";
- } // end switch
- } // end while
- //Display the list.
- cout << "Here are the items on the shopping list:\n";
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement