Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 双链表实现c++;_C++_Doubly Linked List - Fatal编程技术网

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 << &current->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 << &current->data << endl;

cout data您没有正确初始化
edwin
。线路

student *edwin;
只声明一个指向
student
struct的指针。它不为它分配内存。紧接着使用
edwin->name
是不正确的

只需使用struct而不是指向它的指针
student-edwin;edwin.name=“…”。
您的
push
仍然会复制结构

正如402所注意到的,您也没有初始化
my
。我建议您自己尝试找到正确的初始化方法

另一个可能是错误的地方:

cout << &current->data << endl;

cout数据是如何双向链接的?我得说它是一个单链表。
节点的构造函数是做什么的呢?
如果你展示一个完整的程序,而不仅仅是它的一部分,那会更容易。它是如何双链的呢?我得说这是一个单链表。
节点的构造函数做什么呢?
如果你展示一个完整的程序,而不仅仅是其中的一部分,会更容易。实际上他根本没有初始化它。也不初始化我的
。实际上他根本没有初始化它。也不初始化
my