C++我的第一个链表我做错了什么
C++ My First linked list what am I doing wrong?
嘿,伙计们,我正在处理我的第一个链接列表,以保存学生记录(name id gpa addr),但我遇到了错误,我想知道你们能不能发现错误?
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct Student
{
string name;
string address;
double id;
double gpa;
Student *next;
};
Student *head;
head = NULL;
string Student_name;
double Student_id;
double Student_gpa;
string Student_address;
for (int i = 0; i<20; i++)
{
cout << "What is the student's name?";
getline (cin, Student_name);
cout << "What is " << Student_name << "'s ID Number?";
cin >> Student_id;
cout << "What is " << Student_name << "'s GPA?";
cin >> Student_gpa;
cout << "What is " << Student_name << "'s address?";
getline (cin, Student_address);
}
Student *newStudent;
Student *Student_ptr;
newStudent = new Student;
newStudent->name = Student_name;
newStudent->id = Student_id;
newStudent->gpa = Student_gpa;
newStudent->address = Student_address;
newStudent->next = NULL;
if (!head)
head = newStudent;
else
{
Student_ptr = head;
while (Student_ptr -> next)
Student_ptr = Student_ptr->next;
Student_ptr->next = newStudent;
}
cout << endl;
Student *Display_ptr;
Display_ptr = head;
while (Display_ptr)
{
cout << Display_ptr-> name << endl;
cout << Display_ptr-> id << endl;
cout << Display_ptr-> gpa << endl;
cout << Display_ptr-> address << endl;
Display_ptr = Display_ptr->next;
cout << endl;
}
return 0;
}
嗯,我不知道如何使用模板,除非你的意思是使用std列表,而不是自己制作。在主函数的主体中有结构Student对我来说似乎不太好。这是重写代码的一部分,做一些类似的事情会很好
using namespace std;
namespace MyDataStructs{
class MyLinkedListNode
{
public:
MyLinkedListNode():
name("Empty"),
address("Empty"),
studentID(0.0),
gpa(0.0),
next(NULL)
{
}
~MyLinkedListNode();
void setName(const string& a_name){name = a_name;}
void setAddress(const string& a_address){address = a_address;}
void setID(const int& a_Id){studentID = a_Id;}
void setGPA(const double& a_GPA){gpa = a_GPA;}
void setNextNode(MyLinkedListNode* a_node){next = a_node;}
string getName(){return name;}
string getAddress(){return address;}
int getId(){return studentID;}
double getGPA(){return gpa;}
MyLinkedListNode* getNextNode(){return next;}
private:
string name;
string address;
int studentID;
double gpa;
MyLinkedListNode *next;
};
class MyLinkedList
{
MyLinkedList():
m_size(0)
{
}
~MyLinkedList()
{
clear();
}
void push_back(const MyLinkedListNode& a_node)
{
MyLinkedListNode* newNode = new MyLinkedListNode(a_node);
if(m_end == NULL)
{
m_head = newNode;
m_end = newNode;
}
else
{
m_end->setNextNode(newNode);
m_end = newNode;
}
m_size++;
}
bool deleteNode(const int& a_index)
{
if(a_index >= m_size)
return false;
if(m_head == NULL)
return false;
m_size--;
MyLinkedListNode* currentNode;
if(a_index == 0)
{
currentNode = m_head->getNextNode();
delete m_head;
m_head = currentNode;
return true;
}
curentNode = m_head;
MyLinkedListNode* previousNode;
for(int i = 0; i < a_index; i++)
{
previousNode = currentNode;
currentNode = currentNode->getNextNode();
}
if(currentNode == m_end)
{
m_end = previousNode;
}
previousNode->setNextNode(currentNode->getNextNode());
delete currentNode;
return true;
}
void clear()
{
MyLinkedListNode* currentNode = m_head;
MyLinkedListNode* nextNode;
while(currentNode != NULL)
{
nextNode = currentNode->getNextNode();
delete currentNode;
currentNode = nextNode;
}
}
private:
MyLinkedListNode* m_head;
MyLinkedListNode* m_end;
int m_size;
};
}//MyDataStructs
我不知道,我很无聊,你们怎么想。。。。不过,重新访问一些基本数据结构很有趣。
好吧,最好将实际链表作为自己的类,甚至可以将结构作为自己的类别,这样将来就可以重用这些类。Node可以是一个模板类,但实际的链表类可以保持不变;它只需要一个类型为"LinkedListNode"的数据成员,它会根据您需要的数据而更改。
按照现在的设置方式,你必须重写大部分内容。只是为了实现第二个链表,只做了一个小的更改。使链表成为一个类也意味着您可以编写实际的insert/delete/searc/等。函数,而不是主要做那些事情。
结构定义不应进入main()内部。从技术上讲,由于这是C++,所以您应该真正使用类而不是结构。
更重要的是,循环需要在每次迭代中分配新的数据结构。也就是说,每次用户输入学生信息时,都应该进入一个新的链表元素,然后添加到现有列表中。
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 我在查找和字符串上做错了什么?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 插入排序:我做错了什么?