0

嘿伙计们,我正在处理我的第一个链接列表以保存学生记录(名称 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;

}

4

3 回答 3

2

好吧,最好将实际的链表设为自己的类,您甚至可以将结构设为自己的类,以便将来重用这些类。Node 可以是一个模板类,但实际的链表类可以保持不变;它只有一个“LinkedListNode”类型的数据成员,它会根据您需要的数据而改变。

按照现在的设置方式,您必须重新编写大部分内容。只是为了实现第二个链表并稍作更改。使链表成为类也意味着您可以编写实际的插入/删除/搜索等。函数,而不是在 main.

于 2012-09-06T15:55:30.123 回答
2

好吧,我不知道如何使用模板,除非您的意思是使用标准列表而不是自己制作。在main 函数的主体中包含struct 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

我不知道,我很无聊,你们怎么看……虽然重温一些基本的数据结构很有趣。

于 2012-09-06T16:42:27.623 回答
1

结构定义不应进入 main() 内部。从技术上讲,因为这是 C++,所以你真的应该使用类而不是结构。

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

于 2012-09-06T15:56:30.337 回答