C++ 将链表与类一起使用

C++ 将链表与类一起使用,c++,class,oop,linked-list,C++,Class,Oop,Linked List,下面是一个代码示例,作为帮助发送给我。虽然我现在不确定是否应该为我创建的每个变量设置setter和getter,但我确实理解了一些事情 我正在做的程序有很多变量,因为它是一个存储信息和东西的程序。如果不是太多,如果有人能简化它,可以吗?如果在这种情况下这是不可能的,那也没关系 #include<iostream> #include<string> using namespace std; //This is the class to be used //as a node

下面是一个代码示例,作为帮助发送给我。虽然我现在不确定是否应该为我创建的每个变量设置setter和getter,但我确实理解了一些事情

我正在做的程序有很多变量,因为它是一个存储信息和东西的程序。如果不是太多,如果有人能简化它,可以吗?如果在这种情况下这是不可能的,那也没关系

#include<iostream>
#include<string>
using namespace std;
//This is the class to be used
//as a node in the linked list.
class Node
{
    //this are private members,
    //since they are declared as private
    //they can only be accessed by this class
    //which will ensure data security :) (this is called encapsulation)
    private:
        int id;
        string name;
        Node *next;

    //This are public member functions
    //These functions are the only way to access
    //the data members of the Node class
    public:
        //These function are called setter/mutators
        void setId(int);//this function will set the id data member
        void setName(string);//this function will set the name data member
        void setNext(Node*);//this function will set the next pointer

        //These functions are called getters/accessors
        int getId();//this function will get/return the value of the id data member
        string getName();//this function will get/return the value of the name data member
        Node* getNext();//this function will get/return the address of the next node
}*head = NULL, *tail = NULL;

//This function will traverse the linked list.
//It is placed outside the class because it is not needed
//to be one of the class function members 
void traverse(Node*);

int main()
{
    char response = 'y';
    //variable declarations for inputs
    int inputData;
    string inputName;

    while(response == 'y')//this will loop the input phase
    {
        system("cls");
        cout<<"Enter ID: ";
        cin>>inputData;
        cin.ignore();
        cout<<"Enter name: ";
        getline(cin,inputName);
        //Note about the dot (.) operator and the arrow (->)
        //operator: the dot operator is used when accessing
        //structure or class members without using a pointer,
        //while the arrow operator is used to access structure
        //or class members when using pointers
        Node *node = new Node;
        node->setId(inputData);//calls the setId member function
        node->setName(inputName);//calls the setName member function
        node->setNext(NULL);//calls the setNext member function
        if(head==NULL)
        {   
            head = node;
        }

        if(tail==NULL)
        {
            tail = node;
        }
        else
        {
            tail->setNext(node);
            tail = node;
        }

        cout<<"Enter another?: ";
        cin>>response;
    }
    traverse(head);
    return 0;
}




//Here are the function members definitions
void Node::setId(int id)
{
    this->id = id;
}



void Node::setName(string name)
{
    this->name = name;
}



void Node::setNext(Node *next)
{
    this->next = next;
}



int Node::getId()
{
    return this->id;
}



string Node::getName()
{
    return this->name;
}



Node* Node::getNext()
{
    return this->next;
}



//This is the function definition of the traverse function
void traverse(Node *node)
{
    system("cls");
    while(node != NULL)
    {
        cout<<"ID: "<<node->getId()<<endl;
        cout<<"Name: "<<node->getName()<<endl<<endl;
        node = node->getNext();
    }
}
#包括
#包括
使用名称空间std;
//这是要使用的类
//作为链接列表中的节点。
类节点
{
//这是私人会员,,
//因为它们被宣布为私有
//它们只能由此类访问
//这将确保数据安全:)(这称为封装)
私人:
int-id;
字符串名;
节点*下一步;
//这是公共成员功能
//这些功能是访问的唯一方式
//节点类的数据成员
公众:
//这些函数称为setter/mutators
void setId(int);//此函数将设置id数据成员
void setName(string);//此函数将设置数据成员的名称
void setNext(Node*);//此函数将设置下一个指针
//这些函数称为getter/accessor
int getId();//此函数将获取/返回id数据成员的值
string getName();//此函数将获取/返回name数据成员的值
Node*getNext();//此函数将获取/返回下一个节点的地址
}*head=NULL,*tail=NULL;
//此函数将遍历链接列表。
//它被放置在类之外,因为它不是必需的
//成为类函数成员之一
空心导线(节点*);
int main()
{
字符响应='y';
//输入的变量声明
int输入数据;
字符串输入名;
while(response=='y')//这将循环输入阶段
{
系统(“cls”);
coutinputData;
cin.ignore();
coutsetId(inputData);//调用setId成员函数
node->setName(inputName);//调用setName成员函数
node->setNext(NULL);//调用setNext成员函数
if(head==NULL)
{   
头部=节点;
}
if(tail==NULL)
{
尾=节点;
}
其他的
{
tail->setNext(节点);
尾=节点;
}
coutresponse;
}
导线(头部);
返回0;
}
//下面是函数成员的定义
void节点::setId(int-id)
{
这个->id=id;
}
void节点::setName(字符串名称)
{
此->名称=名称;
}
void节点::setNext(节点*next)
{
这个->下一个=下一个;
}
int节点::getId()
{
返回此->id;
}
字符串节点::getName()
{
返回此->名称;
}
Node*Node::getNext()
{
返回此->下一步;
}
//这是遍历函数的函数定义
空心导线测量(节点*节点)
{
系统(“cls”);
while(节点!=NULL)
{

cout不太清楚,您在这里需要什么。但是,如果您希望使用链表的基于类的实现(与函数式C方式相反)的话,
std::list
是您的关键。下面提供了显示如何使用for each循环添加和遍历两个元素的最少代码:

#include <list>
#include <iostream>

int main(){
    std::list<int> my_list;
    my_list.push_back(12);
    my_list.push_back(34);

    for(const auto i: my_list){
        std::cout << i << " ";
    }
    std::cout << std::endl;
}
#包括
#包括
int main(){
std::列出我的清单;
我的列表。向后推(12);
我的清单。推回(34);
for(const auto i:my_list){

std::cout内部链表或外部ie。std::list。我想知道如何在面向对象编程中实现链表。我创建了一个程序,但它使用的是struct。这次我想使用class,因为它是在我的大学里交给我们的任务。顺便说一句,谢谢你的建议。@NewBee,如果你的大学班级要求你实现y我们自己的链表类(编程类中的常见任务)如果你想在一个实现中帮助你,你必须发布你的现有代码。请阅读我希望你能得到这个评论的通知。我已经编辑了这个问题,我与你发给我的链接的指导方针相一致。我有一个C++的例子。我想知道我是否真的需要为我为程序创建的每个变量设置一个setter和getter。非常感谢。希望听到积极的回应。