C++ programming

profiledalamri_055
include....docx

#include "studentList.h"

#include <iostream>

#include <string>

studentList::studentList():head(nullptr){}

bool studentList::isEmpty(){return head == nullptr;}

Node* studentList::getHead(){return head;}

void studentList::setHead(Node *n){head = n;}

//Purpose: Print a list of the students in the list from head to tail

//Preconditions: Calling object is a list of students

//Postconditions: Output each student name in the list, starting with the head and moving up to the tail.

void studentList::printStudentNamesFromHead(std::ostream& outstream) const{

Node* curr;

for( curr = head; curr != nullptr; curr = curr->getNext()){

curr->printName(outstream);

}

}

//Purpose: Print a list of the students in the list from head to tail

//Preconditions: output stream and the list of students

//Postconditions: Output each student name in the list to the output stream, starting with the head and moving up to the tail.

std::ostream& operator <<(std::ostream& outstream, const studentList& myStudents){

myStudents.printStudentNamesFromHead(outstream);

return outstream;

}

//Purpose: Add a new student to the head of the list.

//Preconditions: Calling object is a list of students, parameters are a string last name and first name.

//Postconditions: Add new student to the head of the list with the given name.

void studentList::addStudentHead(std::string newLname, std::string newFname){

Node* newHotness = new Node(newLname, newFname, head, nullptr);

if(head != nullptr){

head->setPrev(newHotness);

}

head = newHotness;

}

//Purpose: Add a new student to the head of the list.

//Preconditions: input stream and the list of students

//Postconditions: Add new student to the head of the list with the given name.

// Take the first string from the input stream up to whitespace as the fname.

// Take the second string from the input stream up to whitespace as the lname.

std::istream& operator >>(std::istream& instream, studentList& myStudents){

std::string newFname, newLname;

instream >> newFname >> newLname;

myStudents.addStudentHead(newLname, newFname);

return instream;

}

//Purpose: Remove a student from the list.

//Preconditions: Calling object is a list of students, parameters are a string last name and first name.

//Postconditions: Remove a student from the list with the given name; first matching student starting from the head.

// Return true if removal was successful and false otherwise (when there is no student with the given first and last name).

bool studentList::removeStudent(std::string newLname, std::string newFname){

Node* oldAndBusted;

for( oldAndBusted = head; oldAndBusted != nullptr; oldAndBusted = oldAndBusted->getNext()){

if( oldAndBusted->getFName()==newFname && oldAndBusted->getLName()==newLname){

if(oldAndBusted->getPrev() == nullptr){

//std::cout << "Prev student is nullptr" << std::endl;

//std::cout << "Next student is ";

//oldAndBusted->getNext()->printName();

head = oldAndBusted->getNext();

}

else if(oldAndBusted->getNext() == nullptr){

//std::cout << "Prev student is ";

//oldAndBusted->getPrev()->printName();

//std::cout << "Next student is nullptr" << std::endl;

oldAndBusted->getPrev()->setNext(nullptr);

}

else{

//std::cout << "Prev student is ";

//oldAndBusted->getPrev()->printName();

//std::cout << "Next student is ";

//oldAndBusted->getNext()->printName();

oldAndBusted->getPrev()->setNext(oldAndBusted->getNext());

oldAndBusted->getNext()->setPrev(oldAndBusted->getPrev());

}

delete oldAndBusted;

return true;

}

}

return false;

}

//Purpose: Delete all students in the list.

//Preconditions: Calling object is a list of students

//Postconditions: Safely deallocate memory for each node in the list.

studentList::~studentList(){

Node* curr = head;

Node* temp;

while(curr != nullptr){

temp = curr->getNext();

delete curr;

curr = temp;

}

}

//Purpose: Reverse the order of the list

//Preconditions: Calling object is a list of students

//Postconditions: Objects in the list are swapped into reverse order.

// Example, if original list was: A,B,C,D where A is the head

// After reverseList, new list is: D,C,B,A where D is the new head

void studentList::reverseList(){

Node *temp = nullptr;

Node *curr = head; //starting at the head

//swap next and prev values

//for each node

while (curr != nullptr)

{

temp = curr->getPrev(); //not, this will equal nullptr if this is the head

/*your

code

here*/

}

//set the new head if this is not an empty list or a list with only one node

if(temp != nullptr)

head = temp->getPrev();

/*Observation for why this works:

temp will be nullptr when the list is empty because we initialized it

at nullptr and never iterated through the while loop

temp will be nullptr when there was only one node because that one node was the head,

so temp = curr->getPrev(); gave it a nullptr value on the one iteration of the while loop.*/

}

//Purpose: Sort the list in ascending order by last name using Insertion Sort

//Preconditions: Calling object is a list of students

//Postconditions: Objects in the list are sorted in ascending order by last name

// Example, if original list was:

// Matt Spradling, John Grant, Tom Toybo, Sam Apples, David Pumpkins

// where Matt Spradling is the head

// After insertionSort, new list:

// Sam Apples, John Grant, David Pumpkins, Matt Spradling, Tom Toybo

// where Sam Apples is the new head

void studentList::insertionSort(){

}

/*

To help you design your algorithm for insertionSort, here is a variant of insertionSort for an array of integers:

Taken from: http://www.geeksforgeeks.org/insertion-sort/

It also shows a good example of how it works.

void insertionSort(int arr[], int n)

{

int i, key, j;

for (i = 1; i < n; i++)

{

key = arr[i];

j = i-1;

// Move elements of arr[0..i-1], that are

// greater than key, to one position ahead

// of their current position

while (j >= 0 && arr[j] > key)

{

arr[j+1] = arr[j];

j = j-1;

}

arr[j+1] = key;

}

}

*/