#include<iostream>
#include <string>
using std::string;
using namespace std;
class Database;
class Record
{
int student_ID;
int stud_age;
string first_name;
string last_name;
string major;
char grade;
Record *next;
public:
friend class Database;
friend ostream &operator<<(ostream &, Record &);
Record(const int, const int, string, string, string);
int getID() const;
};
class Database
{
Record *firstptr;
public:
friend ostream &operator<<(ostream &, Database &);
Database();
void print(Database);
bool isEmpty() const;
void insert();
void deleteRecord();
Record *retrieveRecord(int);
};
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
Record::Record(const int id, const int age, string firstname, string lastname, string major)
{
student_ID = id;
stud_age = age;
first_name = firstname;
last_name = lastname;
major = major;
next = NULL;
}
ostream &operator<<(ostream &output, Record &newrecord)
{
output << "Student ID: " << newrecord.student_ID << endl;
output << "Age: " << newrecord.stud_age << endl;
output << "First Name: " << newrecord.first_name << endl;
output << "Last Name: " << newrecord.last_name << endl;
output << "Major: " << newrecord.major << endl;
return output;
}
int Record::getID() const
{
return student_ID;
}
//--------------------------------------------------------------------------
Database::Database(): firstptr(NULL)
{
//empty
}
bool Database::isEmpty() const
{
return (firstptr == 0);
}
Record *Database::retrieveRecord(int y)
{
int y;
cout<<"Please enter the student id number of the record you want to retrieve";
cin>> y;
Record *temp = firstptr;
while(temp != NULL)
if (temp->student_ID == y)
return temp;
return 0;
}
void Database::deleteRecord(int deletenum)
{
int deletenum;
cout << "What record would you like to delete? ";
cin >> deletenum;
Record *temp = firstptr;
Record *prev = temp;
while(temp != NULL)
{
if(temp->student_ID == deletenum)
break;
prev = temp;
temp = temp->next;
}
if(temp == NULL)
cout << "There was no match.";
else if (temp == prev)
{
firstptr = temp->next;
delete temp;
}
else
{
prev->next = temp->next;
delete temp;
}
}
void Database::print(Database a)
{
Record *currentPtr = a.firstptr;
while ( currentPtr != NULL)
{
cout << currentPtr->student_ID << " ";
currentPtr = currentPtr->next;
}
}
void Database::insert(void)
{
int id;
int age;
string firstname;
string lastname;
string major;
cout << "Please enter id: ";
cin >> id;
cout << "Please enter age: ";
cin >> age;
cout << "Please enter first name: ";
cin >> firstname;
cout << "Please enter last name: ";
cin >> lastname;
cout << "Please enter major: ";
cin >> major;
Record *newNode = new Record(id, age, firstname, lastname, major);
if ( isEmpty() )
firstptr = newNode;
else
{
Record *temp = firstptr;
Record *prev = temp;
while (temp != NULL && temp->student_ID < id)
{
prev = temp;
temp = temp->next;
}
if(prev == temp)
{
newNode->next = firstptr;
firstptr = newNode;
}
else
{
newNode->next = prev->next;
prev->next = newNode;
}
}
}
int main()
{
int option;
cout<<"What would you like to do with the database? Please type in the number \
corresponding to the options given.\n \
1. Display the records. \n \
2. Insert a record. \n \
3. Retrieve a specific record. \n \
4. Delete a record. \n";
cin>>option;
switch(option){
case 1: print();
break;
case 2: insert();
break;
case 3: retrieveRecord();
break;
case 4: deleteRecord();
break;
default:
cout<< "You have entered an invalid choice, please pick again.\n \
1 to display, 2 to insert, 3 to retrieve and 4 to delete. \n";
cin>>option;
}
return 0;
}
Well there's the code, but I don't know what parameters to put into the function calls, or when I define the functions either. I'm not really a programming type of person, I just don't understand this stuff. It's difficult enough for me to tell you what's going on in that linked list (had a friend help me) let alone tell you what the program is doing.
Feedback?

New Topic/Question
Reply




MultiQuote





|