Reputation: 1
I'm testing my doubly linked list and it doesn't display anything. I'm inserting a student data then trying to display it. I've only worked on the push function and display just to take it by steps. I'm new to doubly linked lists.
#include <iostream>
#include <string>
#include "stdlib.h"
#include "time.h"
#include <ctime>
#include <cstdlib>
#include <time.h>
using namespace std;
class student
{
public:
int id; //student ID number
string name; //student’s name
string university; //student’ university
double GPA; //student’ GPA
};
//student list is a doubly linked list of students.
class studentList
{
private:
class node
{
public:
student data;
node * next;
node * prev;
};
node * head;
public:
studentList()
{
head = NULL;
}
//be sure to free all dynamically allocated memory!
~studentList()
{
delete head;
}
//return true if the list is empty, false if not
bool empty()
{
if ( head == NULL)
{
return true;
}
else
return false;
}
//insert student s into the front of the linked list
void push(student s)
{
node *tmp;
tmp = new node;
tmp->data = s;
tmp->next = head;
head = tmp;
}
//remove and return the student at the front of the list
student pop();
//locate and remove the student with given ID number
void removeStudent(int id);
//locate and return a copy of the student with given ID number
student getStudent(int id);
//locate the student with given ID number in list and set their GPA to gpa
void updateGPA(int id, double gpa);
//arrange students into increasing order based on either ID, name, or GPA. If
//variable 'field' has value "id", sort into increasing order by id.
//If 'field' has value "name", sort into alphabetical order by name.
//If 'field' has value "GPA", sort into order by GPA.
void sort(string field);
//a test function to simply display the list of students to the screen
//in the order they appear in the list.
void display()
{
node *current = head;
while (current!=NULL)
{
cout << ¤t->data << endl;
current = current->next;
}
}
};
int main()
{
studentList *my;
student *edwin;
edwin->name = "edwin";
edwin->university = "lsu";
edwin->GPA = 4.0;
edwin->id = 33;
my->push(*edwin);
my->display();
return 0;
}
Upvotes: 0
Views: 2472
Reputation: 11058
You are not initializing edwin
properly. The line
student *edwin;
just declares a pointer to a student
struct. It does not allocate memory for it. Using edwin->name
immediately after that is incorrect.
Just use struct instead of a pointer to it student edwin; edwin.name="...";
.
Your push
copies the struct anyway.
As noticed by 40two, you are not initializing my
either. I'd advise you to try to find the correct way to initialize it yourself.
Another place that is likely wrong:
cout << ¤t->data << endl;
Can you explain this line? Why don't you do cout << current->data.name
?
Also note that you never set prev
pointer in node
(although you have this field). So it's not a correct double-linked list (it's single-linked, essentially).
Upvotes: 2
Reputation: 21
Your problem exists here, cout << ¤t->data << endl;
what exactly is data? is it some encapsulated structure? I am assuming it is the entire student struct. If that is the case, then you need to get at that struct and then cout it's various members. i.e. cout << ¤t->data->name << endl;
That is if data is of type student* if it is not, then you need to dereference the pointer to your student struct and then cout it's members.
Also what you have here is not a doubly-linked list, but is infact a single linked list. Doubly-Linked lists have a both next and prev pointer to the next and previous nodes in the list.
Upvotes: 2