C++ 如何将链表的自身实现从存储整数改为存储个人数据

C++ 如何将链表的自身实现从存储整数改为存储个人数据,c++,data-structures,linked-list,C++,Data Structures,Linked List,我已经编写了自己的链表实现,它存储数字。这很简单。现在我必须更改它来存储关于学生的信息。添加到列表中的每个学生必须包含: 索引 名字 姓 大学 研究领域 学习年份 年龄 索引是类似于键的,如果我们想在列表中找到学生,我们可以键入index来找到它,并显示关于学生的完整信息。删除指定的学生也一样-我们键入index来删除它 谁能给我一个建议或一个例子,如何改变它来存储有关学生的信息?我考虑过询问构造函数中的每个信息——当创建对象时,它会显示一个关于该项的查询,并且该对象会被添加到列表中: link

我已经编写了自己的链表实现,它存储数字。这很简单。现在我必须更改它来存储关于学生的信息。添加到列表中的每个学生必须包含:

索引

名字

大学

研究领域

学习年份

年龄

索引是类似于键的,如果我们想在列表中找到学生,我们可以键入index来找到它,并显示关于学生的完整信息。删除指定的学生也一样-我们键入index来删除它

谁能给我一个建议或一个例子,如何改变它来存储有关学生的信息?我考虑过询问构造函数中的每个信息——当创建对象时,它会显示一个关于该项的查询,并且该对象会被添加到列表中:

linkedList::linkedList(){
head = NULL;
cout << "Type index: ";
cin >> index;
cout << "Type name: ";
cin >> name;
.
.
.
}
但我不知道它是否能正常工作,也不知道它是否是一个好主意。任何建议都会有帮助。提前谢谢你

以下是在列表中存储数字的代码:

class node{
public:
node();
int data;
node *next;
};

node::node(){
}

class linkedList{
node * head;
public:
linkedList();
node * createNode(int);
void insertBegin();
void insertPos();
void insertLast();
void deletePos();
void display();
void deleteList();
~linkedList();
};

linkedList::linkedList(){
head = NULL;
}

node *linkedList::createNode(int value){
node *temp;
temp = new(node);
temp->data = value;
temp->next = NULL;
return temp;
}

void linkedList::insertBegin(){
int value;
cout << "------------------------" << endl;
cout << "Type value: ";
cin >> value;
node *temp, *p;
temp = createNode(value);
if (head == NULL){
head = temp;
head->next = NULL;
}
else{
p = head;
head = temp;
head->next = p;
}
cout << "Element added." << endl;
cout << "------------------------" << endl;
}

void linkedList::insertLast(){
int value;
cout << "------------------------" << endl;
cout << "Type a value:: ";
cin >> value;
node *temp, *s;
temp = createNode(value);
if (head == NULL){
head = temp;
head->next = NULL;
} else{
s = head;
while (s->next != NULL){
  s = s->next;
}
temp->next = NULL;
s->next = temp;
}
cout << "Element added." << endl;
cout << "------------------------" << endl;
}

void linkedList::insertPos(){
int value, pos, counter = 0;
cout << "------------------------" << endl;
cout << "Type value: ";
cin >> value;
node *temp, *s, *ptr;
temp = createNode(value);
cout << "Type position: ";
cin >> pos;
cout << "------------------------" << endl;
int i;
s = head;
while (s != NULL){
s = s->next;
counter++;
}
if (pos == 1){
if (head == NULL){
head = temp;
head->next = NULL;
}
else{
ptr = head;
head = temp;
head->next = ptr;
}
}
else if (pos > 1  && pos <= counter){
s = head;
for (i = 1; i < pos; i++){
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else{
cout << "------------------------" << endl;
cout << "There is no such position. Type a value from 1 to " << counter << 
endl;
cout << "------------------------" << endl;
}
cout << "Element added." << endl;
cout << "------------------------" << endl;
}

void linkedList::deletePos(){
int pos, i, counter = 0;
if (head == NULL){
cout << "------------------------" << endl;
cout << "List is empty" << endl;
cout << "------------------------" << endl;
return;
}
cout << "------------------------" << endl;
cout << "Type position: ";
cin >> pos;
cout << "------------------------" << endl;
node *s, *ptr;
s = head;
if (pos == 1){
head = s->next;
}
else{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter){
s = head;
for (i = 1;i < pos;i++){
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else{
cout << "There is no such position. Type a value from 1 to " << counter <<                 
endl;
cout << "------------------------" << endl;
}
delete s;
cout << "Element added." << endl;
cout << "------------------------" << endl;
}
}

void linkedList::display(){
node *temp;
if (head == NULL){
cout << "------------------------" << endl;
cout << "List is empty" << endl;
cout << "------------------------" << endl;
return;
}
temp = head;
cout << "------------------------" << endl;
cout << "List elements:: " << endl;
while (temp != NULL){
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL" << endl;
cout << "------------------------" << endl;
}

void linkedList::deleteList (){
node *temp;
while (head != NULL){
temp = head;
head = head -> next;
delete temp;
}
cout << "------------------------" << endl;
cout << "List deleted." << endl;
cout << "------------------------" << endl;
}

linkedList::~linkedList(){
deleteList();
}
class menu{
public:
void displayMenu();
};

void menu::displayMenu(){
linkedList link;
int option;
do
{
cout << "1.Add element at the first pos" << endl;
cout << "2.Add element at the last pos" << endl;
cout << "3.Add element at the pos" << endl;
cout << "4.Delete specific pos" << endl;
cout << "5.Delete whole list" << endl;
cout << "6.Print list" << endl;
cout << "0.End " << endl;
cout << "Choose an option: ";
cin >> option;
switch(option){
case 1:
link.insertBegin();
break;
case 2:
link.insertLast();
break;
case 3:
link.insertPos();
break;
case 4:
link.deletePos();
break;
case 5:
link.deleteList();
break;
case 6:
link.display();
break;
}
} while (option != 0);
}

int main(){
menu k;
k.displayMenu();
return 0;
}
使用模板。C++版本的泛型编程。 使用模板定义链接列表节点:

template<class T>
class node<T>{
   public:
     node<T>();
     T data;
     node<T> *next;
};
您需要重新定义linkedList类以同时使用模板:

使用模板。C++版本的泛型编程。 使用模板定义链接列表节点:

template<class T>
class node<T>{
   public:
     node<T>();
     T data;
     node<T> *next;
};
您需要重新定义linkedList类以同时使用模板:

您也不能使用通用编程/模板,使您的节点具有所需的特定数据 这也会减少对代码的影响,因为在添加模板时,您可能需要添加和更改更多的代码行

您可以做的是简单地更改节点类,添加我们添加到数据类的所有属性,如下所示:

class node {
   public:
      /* ctor, dtor, accesor and modifier operations */
   private:
      node();
      node *next;
      unsigned int index, age;
      std:string name, surname, university, fieldOfStudy, yearOfStudy; // Now we have all the attributesyou needed and we omit the data attribute member.

};
您需要对linkedList方法进行简单的修改,主要是在实例化节点对象时

这个解决方案的问题是,它将您绑定到上面选择的那些属性。前一个通用解决方案不会绑定到任何数据类型,而且所有的LINKEDLIST功能都适用于您决定使用C++ INT、双、浮点、STD::String、自定义类。< /P>您也不能使用泛型编程/模板,并使节点具有特定数据。 这也会减少对代码的影响,因为在添加模板时,您可能需要添加和更改更多的代码行

您可以做的是简单地更改节点类,添加我们添加到数据类的所有属性,如下所示:

class node {
   public:
      /* ctor, dtor, accesor and modifier operations */
   private:
      node();
      node *next;
      unsigned int index, age;
      std:string name, surname, university, fieldOfStudy, yearOfStudy; // Now we have all the attributesyou needed and we omit the data attribute member.

};
您需要对linkedList方法进行简单的修改,主要是在实例化节点对象时


这个解决方案的问题是,它将您绑定到上面选择的那些属性。前一个通用解决方案不会绑定到任何数据类型,仍然所有的链接功能将适用于任何您决定使用C++ int、双、浮点、STD::String、自定义类。< /P>使用自链表而不是标准库中的容器的任何数据类型。他说我们不能使用STL:/为什么要使用自己的链表而不是标准库中的容器?因为讲师,他说我们不能使用STL:/谢谢。我将尝试使用模板来实现它。模板在一开始很难实现,当只有一个简单的错误时,通常会显示数百万个错误。始终关注解决第一个出现的问题。另外,在.h文件中声明并实现类定义和实现。相信我,这会帮你省去很多麻烦的!当然,我会把它标记为正确的。但我还有一个问题。我刚刚检查了我的下一个任务是使用模板制作,首先我必须在没有模板的情况下制作。你知道没有模板怎么做吗?@Ensz会给出另一个答案。非常感谢。我将尝试使用模板来实现它。模板在一开始很难实现,当只有一个简单的错误时,通常会显示数百万个错误。始终关注解决第一个出现的问题。另外,在.h文件中声明并实现类定义和实现。相信我,这会帮你省去很多麻烦的!当然,我会把它标记为正确的。但我还有一个问题。我刚刚检查了我的下一个任务是使用模板制作,首先我必须在没有模板的情况下制作。你知道在没有模板的情况下怎么做吗?@Ensz会给出另一个答案。@Ensz完成了!这是一种不使用模板的可能方法。@Ensz Done!这是一种没有模板的可能方法。
class node {
   public:
      /* ctor, dtor, accesor and modifier operations */
   private:
      node();
      node *next;
      unsigned int index, age;
      std:string name, surname, university, fieldOfStudy, yearOfStudy; // Now we have all the attributesyou needed and we omit the data attribute member.

};