so far my code is as below. I need to insert records in order and I'm not sure how to.
The program contains pre-defined data which is inserted when the program runs, but it also accepts user input which needs to put in order according to the student number.
thanks
PHP Code:#include <iostream>
#include <string>
#include <stdlib.h>
struct link {
int stnum;
string name;
link *next;
};
class linklist {
private:
link *first;
public:
linklist() {
first = NULL;
}
void additem(int s, string n);
void display();
void insertitem(int s, string n);
};
void linklist::additem(int s,string n) {
link * newlink = new link;
newlink -> stnum = s;
newlink -> name = n;
newlink -> next = first;
first = newlink;
}
void linklist::insertitem(int s,string n) {
link * newlink = new link;
newlink -> stnum = s;
newlink -> name = n;
newlink -> next = first;
first = newlink;
}
void linklist::display() {
link *current = first;
while(current != NULL) {
cout << current->stnum << " " << current->name <<endl;
current = current -> next;
}
}
int main() {
linklist li;
li.additem(1315,"Mary");
li.additem(1307,"John");
li.additem(1293,"Kim");
li.additem(1270,"Marty");
li.additem(1258,"Linda");
li.display();
cout << endl << "Add Students - '0' to end" << endl;
string student;
int studentnum;
while (student != "0") {
cout << "Student Name: ";
cin >> student;
if (student != "0") {
cout << "Student Number: ";
cin >> studentnum;
cout << endl;
li.insertitem(studentnum,student);
}
}
cout << endl;
li.display();
cin.ignore();
cin.get();
return 0;
}




Reply With Quote