I've managed to figure out how to insert an assignment after I've found a certain due date. I've ended up making two search function: "search_and_remove" and "search_and_insert" that removes and inserts a new assignment in a list. I've tested it a couple of times and it seems to work ok, except when the date to be searched is the last one in the list.
I'm excluding my h file since all I've added was my function
definitions for each search function. Any other critics on how I could improve my code would be appreciated as well.
list.cpp
Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include "List.h"
using namespace std;
List::List(){
head = NULL;
current = NULL;
temp = NULL;
}
/*
adds a list of elements to
* assignment list.
*/
void List::AddAssign(string addDate,string addName,string addGrade){
Node n = new node;
n->next = NULL;
n->date = addDate;
n->name = addName;
n->grade = addGrade;
if(head != NULL){
current = head;
while(current->next != NULL){
current = current->next;
}
current->next = n;
}
else{
head = n;
}
}
/*
Deletes an Assignment
*/
void List::DeleteAssign(string delData){
Node delPtr = NULL;
temp = head;
current = head;
while(current != NULL && current->date != delData){
temp = current;
current = current->next;
}
if(current == NULL){
cout << delData << " was not in the list\n";
delete delPtr;
}else{
delPtr = current;
current = current->next;
temp->next = current;
if(delPtr == head){
head = head->next;
temp = NULL;
}
delete delPtr;
cout << "The Value " << delData << " was deleted\n";
}
}
/*
Prints the list.
*/
void List::PrintList(){
current = head;
while(current!= NULL){
cout << current->date<< ","<<current->name <<","
<<current->grade <<endl;
current = current->next;
}
}
/*
Searches the current date in the list and
* deletes the assignment from the list
* if found.
*/
void List::search_and_remove(string findDate){
current = head;
while(current!= NULL){
if(current->date == findDate){
cout << current->date << " found\n";
DeleteAssign(current->date);
}
current = current->next;
}
}
/*
Searches and inserts a new assignment if a date is found.
*/
void List::search_and_insert(string findDate,string date,string name,string grade){
current = head;
Node insertData = new node;
insertData->date = date;
insertData->name = name;
insertData->grade = grade;
while(current!= NULL){
if(current->date == findDate){
cout << current->date << " found\n";
//inserts the new assignment
insertData->next = current->next;
current->next = insertData;
}
current = current->next;
}
}
listmain.cpp
Code:
#include <cstdlib>
#include "List.h"
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
//Tests a linked list.
List assign;
assign.AddAssign("3/14/13","Statistics","43");
assign.AddAssign("3/12/13","Geometry","43");
assign.AddAssign("3/4/13","Calculus","45");
assign.AddAssign("3/12/13","Java","43");
assign.AddAssign("4/11/13","Biology","55");
assign.AddAssign("5/13/13","Economics","77");
assign.AddAssign("3/11/13","English 101","100");
assign.AddAssign("3/9/13","French","86");
assign.AddAssign("5/23/13","Spanish","55");
assign.AddAssign("3/3/13","Biology","55");
cout << "Printing list...\n";
assign.PrintList();
cout << "Deleting assignments with 3/12/13\n";
assign.search_and_remove("3/12/13");
cout<< "New list without dates 3/12/13\n";
assign.PrintList();
cout << "search assignments with dates 3/9/13\n";
assign.search_and_insert("3/9/13","3/24/13","PhysicsII","89");
cout << "updated list with inserted assignment"
" 3/24/13 PhysicsII 89 \n";
assign.PrintList();
cout << "End of list. \n";
return 0;
}