C++我的第一个链表我做错了什么

C++ My First linked list what am I doing wrong?

本文关键字:错了 什么 链表 我的 第一个 C++      更新时间:2023-10-16

嘿,伙计们,我正在处理我的第一个链接列表,以保存学生记录(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++,所以您应该真正使用类而不是结构。

更重要的是,循环需要在每次迭代中分配新的数据结构。也就是说,每次用户输入学生信息时,都应该进入一个新的链表元素,然后添加到现有列表中。