//------------------------------------------------------------ // Example program to demonstrate a simple linked list. // Dynamic memory allocation is used. // Each node in the list holds one data value (a grade) // and has a pointer to the next node in the list. // // L.Aamodt // 4/29/14 //------------------------------------------------------------ #include using namespace std; struct node { int data; node *linkNext; }; class listType1 // Class definition { public: listType1(); // Default constructor void newNode(int value); // Create a new node in the list void print(void); // Output private: node *headptr; // Data node *ptr; }; listType1::listType1() // Default constructor { headptr = NULL; } void listType1::newNode(int value) // Create a new node in the list { ptr = new node; ptr->data = value; ptr->linkNext = headptr; headptr = ptr; return; } void listType1::print() // Print th e list { if (headptr == NULL) cout << "List is Empty" << endl; else { cout << "Stored grades are:" << endl; ptr = headptr; while (ptr != NULL) { cout << ptr-> data << endl; ptr = ptr->linkNext; } } } int main() { listType1 grades; int gradeValue; cout << "Enter grade data.\n (Enter negative # to quit)\n"; cout << "Grade: "; cin >> gradeValue; while (gradeValue >= 0) { grades.newNode(gradeValue); cout << "Grade: "; cin >> gradeValue; } grades.print(); return 0; }