# 关于自定义单链表实现Student类型数据存放的问题，主要是结点的构造不太明白！(语言-c++)

###### 结点的构造
``````template<typename T>
class SLNode
{
public:
T _data;
SLNode<T>* next;

SLNode()
{
this->_data = NULL;
this-> next = NULL;
}
};
``````
###### 链表的实现
``````template<typename T>
{
private:
int _count;

public:

{
this->_count = NULL;
}

{
SLNode<T>* new_node = new SLNode();
new_node->_data = data;
new_node->next = NULL;

for (int i = 0; i < _count; i++)
{
last_node = last_node->next;
}
new_node->next = last_node->next;
last_node->next = new_node;
}
void insert(SingleLinkedList* L, int pos, T data)
{
if (L == NULL)
{
pos = 0;
}
SLNode<T>* new_node = new SLNode();
new_node->_data = data;
new_node->next = NULL;

for (int i = 0; i < pos; i++)
pos_node = pos_node->next;
new_node->next = pos_node->next;
pos_node->next = new_node;
L->_count++;
}

{
if (L->_count == 0)
cout << "该链表中没有数据" << endl;
else {
while (pcurrent != NULL)
{
cout << pcurrent->_data << endl;
pcurrent = pcurrent->next;
}
cout << endl;
}
}

};

``````
###### 测试函数
``````int main()
{

Student s1 = { 001,"桑桑",18.0f,88.0l };
Student s2 = { 002,"稚稚",8.0f,77.0l };
Student s3 = { 000,"渊渊",25.0f,99.0l };
cout << s1 << endl;
L1.push_back(&L1,s1);
L1.push_back(&L1,s2);
cout << s1 << endl;
L1.insert(&L1, 0, s3);
L1.show(&L1);
}

``````
###### Student类
``````class Student
{
public:
Student(int id, string name, float age, double score);
~Student();
friend ostream& operator<<(ostream&os, const Student&s)
{
os << "学号:" << s._id << "  姓名：" << s._name << "  年龄" << s._age << "  成绩：" << s._score << endl;
return os;

}
string str()
{
cout << "内部输出:" << endl;
cout << "学号:" << _id << "  姓名：" << _name << "  年龄" << _age << "  成绩：" << _score << endl;
return "\n";
};
Student& operator =(const Student&x)
{
this->_id = x._id;
this->_age = x._age;
this->_score = x._score;
this->_name = x._name;

}
private:
int _id;
string _name;
float _age;
double _score;

};

Student::Student(int id, string name, float age, double score)
{
_id = id;
_name = name;
_age = age;
_score = score;
}

Student::~Student()
{
}

``````
###### 报错
``````已启动生成…
1>------ 已启动生成: 项目: ex4p4, 配置: Debug x64 ------
1>p4.cpp
1>已完成生成项目“ex4p4.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个，失败 1 个，最新 1 个，跳过 0 个 ==========

``````
• 写回答

#### 3条回答默认 最新

• 关注

代码修改如下：

``````#include <iostream>
using namespace std;

template<typename T>
{
private:
class SLNode
{
public:
T _data;
SLNode* next;

SLNode()
{
//this->_data = 0;
this->next = NULL;
}
};
int _count;

public:

{
this->_count = 0;
}

void push_back( T data)
{
SLNode* new_node = new SLNode();
new_node->_data = data;
new_node->next = NULL;

for (int i = 0; i < _count; i++)
{
last_node = last_node->next;
}
new_node->next = last_node->next;
last_node->next = new_node;
}
void insert(int pos, T data)
{
{
pos = 0;
}
SLNode* new_node = new SLNode();
new_node->_data = data;
new_node->next = NULL;

for (int i = 0; i < pos; i++)
pos_node = pos_node->next;
new_node->next = pos_node->next;
pos_node->next = new_node;
_count++;
}

void show()
{
if (_count == 0)
cout << "该链表中没有数据" << endl;
else {
while (pcurrent != NULL)
{
cout << pcurrent->_data << endl;
pcurrent = pcurrent->next;
}
cout << endl;
}
}

};

class Student
{
public:
Student();
Student(int id, string name, float age, double score);
~Student();
friend ostream& operator<<(ostream& os, const Student& s)
{
os << "学号:" << s._id << "  姓名：" << s._name << "  年龄" << s._age << "  成绩：" << s._score << endl;
return os;

}
string str()
{
cout << "内部输出:" << endl;
cout << "学号:" << _id << "  姓名：" << _name << "  年龄" << _age << "  成绩：" << _score << endl;
return "\n";
};
Student& operator =(const Student& x)
{
this->_id = x._id;
this->_age = x._age;
this->_score = x._score;
this->_name = x._name;
return *this;

}
private:
int _id;
string _name;
float _age;
double _score;

};
Student::Student()
{
_id = 0;
_name = "";
_age = 0;
_score = 0;
}
Student::Student(int id, string name, float age, double score)
{
_id = id;
_name = name;
_age = age;
_score = score;
}

Student::~Student()
{
}

int main()
{

Student s1 = { 001,"桑桑",18.0f,88.0l };
Student s2 = { 002,"稚稚",8.0f,77.0l };
Student s3 = { 000,"渊渊",25.0f,99.0l };
cout << s1 << endl;
L1.push_back(s1);
L1.push_back(s2);
cout << s1 << endl;
L1.insert(0, s3);
L1.show();
return 0;
}

``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

• 系统已结题 4月5日
• 已采纳回答 3月28日
• 修改了问题 3月28日
• 创建了问题 3月28日

#### 悬赏问题

• ¥20 关于php中URL传递GET全局变量的问题
• ¥15 怎么改成循环输入删除(语言-c语言)
• ¥15 安卓C读取/dev/fastpipe屏幕像素数据
• ¥15 pyqt5tools安装失败
• ¥15 mmdetection
• ¥15 nginx代理报502的错误
• ¥100 当AWR1843发送完设置的固定帧后，如何使其再发送第一次的帧
• ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
• ¥100 描述一下元器件的基本功能，pcba板的基本原理
• ¥15 STM32无法向设备写入固件