如何将自己的链表实现从存储整数更改为存储个人数据

How to change own implementation of linked list from storing integers, to store personal data

本文关键字:存储 数据 整数 自己的 链表 实现      更新时间:2023-10-16

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

  • 指数

  • 名称

  • 姓氏

  • 大学

  • 研究领域

  • 学习年份

  • 年龄

索引就像键一样,如果我们想在列表中找到学生,我们键入索引来找到它并显示有关学生的完整信息。删除指定的学生也是如此 - 我们键入索引来删除它。

谁能给我一个建议或一个例子,如何更改它以存储有关学生的信息?我想过询问构造函数中的每个信息 - 创建对象时,它将显示有关该项目的查询,并且该对象将被添加到列表中。:

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++版本。

使用模板定义链表node

template<class T>
class node<T>{
public:
node<T>();
T data;
node<T> *next;
};

这意味着您现在只需访问node's数据属性成员即可拥有任何类型的数据类型node

然后定义一个新的容器类/结构来保存您提到的其他相关信息,包括:

class data {
public:
/* ctor, dtor, accesor and modifier operations */
private:
unsigned int index, age;
std:string name, surname, university, fieldOfStudy, yearOfStudy;
};

您需要重新定义linkedList类以使用模板:

template<class T>
class linkedList<T> {
/* ...declaration and implementation taking in to account <T> data type.
};

然后在您的范围之外,例如在main()中,您可以执行以下操作:

int main() {
linkedList<data> list;
list.insertBegin();
list.insertLast();
// ... etc.
return 0;
}

您也可以不使用通用编程/模板并使您的node具有您想要的特定数据

这也会对代码产生较小影响,因为在添加模板时,您可能必须添加和更改更多代码行。

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

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方法进行简单的修改,主要是在实例化node对象时。

此解决方案的问题在于它将您绑定到上面选择的那些属性。以前的通用解决方案不会将您绑定到任何数据类型,并且所有 linkedList 功能仍然适用于您决定使用的任何数据类型(C++ int、double、float、std::string、自定义类)。