1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <iostream>
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];
double price;
int priority;
struct ListNode *next;
};
ListNode *head;
public:
// Constructor
ShoppingList()
{ head = NULL; }
// Destructor
~ShoppingList();
// Linked list operations
void appendNode(char Name[],double Price,int Priority);
void deleteNode(char Name[],double Price,int Priority);
void sortNode(char Name[],double Price,int Priority);
void displayList() const;
};
#endif
void ShoppingList::appendNode(char Name[], double Price, int Priority)
{
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;
newNode->priority = Priority;
strcpy_s( newNode->name, Name );
newNode->price = Price;
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)
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;
cout << nodePtr->price << endl;
cout << nodePtr->priority << endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
void ShoppingList::deleteNode(char Name[], double Price, int Priority)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->priority == Priority)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
//Initialize nodePtr to head of the list
nodePtr = head;
// Skip all nodes whose item member is not equal to num.
while (nodePtr != NULL && nodePtr->priority != Priority)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of th list, link the previous node
// to the node after nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
void ShoppingList::sortNode(char Name[], double Price, int Priority)
{
ListNode *nodePtr;
nodePtr = head;
for (bool didSwap = true;
didSwap;)
{
didSwap = false;
for (head = nodePtr;
head->next != NULL;
head = head->next)
{
if (head->priority > head->next->priority)
{
nodePtr->priority = head->priority;
head->priority = head->next->priority;
head->next->priority = nodePtr->priority;
didSwap = true;
}
}
}
}
ShoppingList::~ShoppingList()
{
ListNode *nodePtr;
ListNode *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
int main()
{
double budget;
ShoppingList list;
cout << "Enter your budget: $";
cin >> budget;
// Build the list.
list.appendNode("Computer",599.99,1);
list.appendNode("Ipad",499.99,3);
list.appendNode("HD T.V.",329.99,2);
//Display the list.
cout << "Here are the items on the shopping list:\n";
list.displayList();
cout << endl;
//Sort the list.
list.sortNode("Computer",599.99,1);
list.sortNode("Ipad",499.99,3);
list.sortNode("HD T.V.",329.99,2);
//Displat the sorted list.
cout << "Here are the sorted items on the shopping list:\n";
list.displayList();
cout << endl;
return 0;
}
|