C++ 双链表实现c++;
我正在测试我的双链接列表,它没有显示任何内容。 我正在插入一个学生数据,然后尝试显示它。 我只做了推送功能和显示,只是为了一步一步地完成它。 我不熟悉双链接列表C++ 双链表实现c++;,c++,doubly-linked-list,C++,Doubly Linked List,我正在测试我的双链接列表,它没有显示任何内容。 我正在插入一个学生数据,然后尝试显示它。 我只做了推送功能和显示,只是为了一步一步地完成它。 我不熟悉双链接列表 #include <iostream> #include <string> #include "stdlib.h" #include "time.h" #include <ctime> #include <cstdlib> #include <time.h> using nam
#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;
}
}
您的问题存在于此处,cout data您的问题存在于此处,cout data您没有正确初始化
edwin
。线路
student *edwin;
只声明一个指向student
struct的指针。它不为它分配内存。紧接着使用edwin->name
是不正确的
只需使用struct而不是指向它的指针student-edwin;edwin.name=“…”代码>。
您的push
仍然会复制结构
正如402所注意到的,您也没有初始化my
。我建议您自己尝试找到正确的初始化方法
另一个可能是错误的地方:
cout << ¤t->data << endl;
cout data您没有正确初始化edwin
。线路
student *edwin;
只声明一个指向student
struct的指针。它不为它分配内存。紧接着使用edwin->name
是不正确的
只需使用struct而不是指向它的指针student-edwin;edwin.name=“…”代码>。
您的push
仍然会复制结构
正如402所注意到的,您也没有初始化my
。我建议您自己尝试找到正确的初始化方法
另一个可能是错误的地方:
cout << ¤t->data << endl;
cout数据是如何双向链接的?我得说它是一个单链表。节点的构造函数是做什么的呢?
如果你展示一个完整的程序,而不仅仅是它的一部分,那会更容易。它是如何双链的呢?我得说这是一个单链表。节点的构造函数做什么呢?
如果你展示一个完整的程序,而不仅仅是其中的一部分,会更容易。实际上他根本没有初始化它。也不初始化我的
。实际上他根本没有初始化它。也不初始化my
。