【问题标题】:Using linked lists with class [closed]使用带类的链表[关闭]
【发布时间】:2020-07-08 02:36:21
【问题描述】:

以下是发送给我的帮助代码示例。我确实了解了一些事情,尽管我现在不确定是否应该为我创建的每个变量设置 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();
    }
}

【问题讨论】:

  • 内部链表或外部ie。标准::列表。

标签: c++ class oop linked-list


【解决方案1】:

不太清楚,你需要什么。但是,如果您希望使用链表std::list 的基于类的实现(而不是功能C 方式),那么您的关键是。下面提供了显示如何使用 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;
}

如果您刚刚开始使用 C++,强烈建议您花一些时间阅读一本好的 C++ 书籍。 C++ Primer 5th Edition (Not Primer plus) 通常是一本适合初学者的好书。

【讨论】:

  • 我想知道如何在面向对象编程中实现链表。我创建了一个程序,但它使用的是结构。这次我想使用类,因为它是我大学给我们的任务。顺便说一句,感谢您的建议。
  • @NewBee 如果您的大学课程要求您实现自己的链表类(编程课程中的常见任务),那么您肯定对 C++ 有所了解。如果您需要有关实施的帮助,则必须发布现有代码。请阅读How do I ask and answer homework questions?
  • 我希望您能收到此评论的通知。我已经编辑了问题,并与您发送给我的链接中的指南保持一致。我有一个作为帮助发送给我的代码示例。虽然我不太清楚我是否真的需要为我为程序创建的每个变量设置一个 setter 和 getter。太感谢了。希望听到积极的回应。
猜你喜欢
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 2019-07-07
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多