Something like this... I'm sure someone will come in after me to put in their corrections.
In a linked list, each node contains a pointer to the node created after it -- except that last node, which points to NULL.
Basically you're going to be doing is creating a struct:
Code:
struct studentnode {
std::string name,
int id,
int gpa,
struct studentnode* next
};
The first node:
Code:
struct studentnode* firstnode = new struct studentnode;
first->name = "Jerry";
first->id = 1;
first->gpa = 3
first->next = NULL;
lastnode = firstnode;
Then your second node:
Code:
struct studentnode* newnode = new struct studentnode;
newnode->name = "Bobby"
newnode->id = 2;
newnode->gpa = 4
newnode->next = NULL;
lastnode->next = newnode;
lastnode = newnode;
The third node:
Code:
struct studentnode* newnode = new struct studentnode;
newnode->name = "Robbin"
newnode->id = 3;
newnode->gpa = 4
newnode->next = NULL
lastnode->next = newnode;
lastnode = newnode;
And so on and so on...
Jerry --> Bobby --> Robbin --> NULL
Now, to iterate through the linked list:
Code:
struct studentnode* itr;
for (itr = firstnode; itr->next != NULL; itr = itr->next) {
std::cout << "This students name is: " << itr->username << std::endl;
}
And finally to remove Bobby:
Code:
struct studentnode* itr = NULL;
struct studentnode* prev = NULL;
for (itr = firstnode; itr->next != NULL; itr = itr->next) {
if (itr->username == "Bobby") {
if (itr == firstnode)
firstnode = itr->next;
else if (itr == lastnode)
lastnode = prev;
else
prev->next = itr->next
delete itr;
break;
}
prev = itr;
}
I havn't used linked lists in a long time, so I'm sure there are some mistakes in here,
but hopefully it will give you a decent idea. Also, I'd recommend looking into vectors.
edit: what happened to my code tags?