Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************
- Name: Mary Soy
- Date: 10/26/09
- Class: CSCI 3333
- Professor: Schweller
- File: LinkedList.h
- ************************/
- #ifndef H_LinkedList
- #define H_LinkedList
- #include <iostream>
- #include <cassert>
- #include <iomanip>
- using namespace std;
- struct StudentNode
- {
- int id;
- double GPA;
- StudentNode * next;
- //Constructor with 2 parameters.
- StudentNode(int i = 0, double d = 0.0)
- {
- id = i;
- GPA = d;
- next = NULL;
- }
- };
- class LinkedList
- {
- public:
- //insert student with data into linkedlist
- void insertStudent(StudentNode s);
- //returns the gpa of a studen with a given id.
- //return -1 if the student is not in the data structure.
- double getGPA(StudentNode s);
- //change the gpa of the student with the given id to newGPA
- void updateGPA(StudentNode s);
- //destroys all nodes in a list
- void destroyList();
- //displays the content of a list
- void printLL();
- //return true if list is empty
- bool isEmpty();
- //default constructor initialized head to NULL
- LinkedList();
- //destructor deletes all nodes from the list
- ~LinkedList();
- private:
- //he head & tail of a list
- StudentNode *head;
- StudentNode *tail;
- int numStudent;
- };
- LinkedList::LinkedList()
- {
- head = NULL;
- tail = NULL;
- numStudent = 0;
- }
- LinkedList::~LinkedList()
- {
- destroyList();
- }
- void LinkedList::destroyList()
- {
- StudentNode * temp;
- while( head != NULL )
- {
- temp = head;
- head = head->next;
- delete temp;
- }
- tail = NULL;
- numStudent = 0;
- }
- void LinkedList::insertStudent(StudentNode s)
- {
- StudentNode * newNode;
- newNode = new StudentNode(s);
- assert(newNode != NULL);
- if( head == NULL)
- {
- head = newNode;
- tail = newNode;
- numStudent++;
- }
- else
- {
- tail->next = newNode;
- tail = newNode;
- numStudent++;
- }
- }
- void LinkedList::printLL()
- {
- StudentNode * traverse;
- traverse = new StudentNode();
- traverse = head;
- while( traverse != NULL )
- {
- cout << "Student with id# " << traverse->id
- << " and a GPA of: " << fixed << showpoint << setprecision(1) << traverse->GPA << ".\n";
- traverse = traverse->next;
- }
- }
- bool LinkedList::isEmpty()
- {
- if (head == NULL)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- double LinkedList::getGPA(StudentNode s)
- {
- StudentNode * traverse = head;
- while(traverse != NULL)
- {
- if(traverse->id == s.id)
- {
- cout << "The GPA of student id # " << traverse->id << " is: ";
- return traverse->GPA;
- }
- traverse = traverse->next;
- }
- return -1;
- }
- void LinkedList::updateGPA(StudentNode s)
- {
- StudentNode * traverse = head;
- bool found = false;
- while(traverse != NULL)
- {
- if(traverse->id == s.id)
- {
- traverse->GPA = s.GPA;
- found = true;
- }
- traverse = traverse->next;
- }
- if(found == false)
- {
- cout << "Student with id: " << s.id << " could not be found.\n";
- }
- }
- #endif
Add Comment
Please, Sign In to add comment