c++ 访问单个链表中的特定值
c++ Accessing specific value in Single Linked List
我的列表包含一个名称和一个校园ID(CWID)。如何将列表中的 cwid 与我传入的整数进行比较?我在下面写了我正在尝试做的事情的比较的伪代码。
void check_cwid(studentList& list, int cwid) {
studentNode *p = list.head_;
while(p != nullptr){
//Something like this
if *p.cwid() == cwid
//do something
p = p->next_;
}
我正在尝试完成上面代码中的内容。我只是不知道如何比较列表中的特定项目。以下是我的整个实践项目。
#include <iostream>
using namespace std;
class student {
public:
student(const string& sname = "", int cwid = 0) : sname_(sname), cwid_(cwid) {}
string sname() const {return sname_;}
int cwid() const {return cwid_;}
friend ostream& operator <<(ostream& os, student st){
return os << endl << st.sname_ << endl << st.cwid_ << endl;
}
private:
string sname_;
int cwid_;
};
struct studentNode {
studentNode(const string& sname, int cwid, studentNode* next=nullptr) :st_(sname, cwid), next_(next) {}
studentNode(student& st, studentNode* next=nullptr) : st_(st), next_(next) {}
friend ostream& operator << (ostream& os, studentNode node) {
return os << node.st_;
}
studentNode* next_;
student st_;
};
struct studentList {
studentList() : head_(nullptr), size_(0) {}
studentNode* head_;
size_t size_;
};
///******************** what im trying to do
void check_cwid(studentList& list, int cwid) {
studentNode *p = list.head_;
while(p != nullptr){
}
}
int check_cwid(studentList& list, int cwid) {
studentNode *p = list.head_;
int list_size = list.size_;
while(list_size--){
//Something like this
if (p -> st_.cwid() == cwid)
//do something
break;
else
p = p -> next_;
}
相关文章:
- 尝试从单向链表C++中删除单个节点时出现读取访问冲突
- 媒体基金会可以允许多个客户端同时访问单个网络摄像头设备吗?
- 如何从C 向量访问单个单词
- 通过单个实例从多个类访问一个类
- c++ 访问单个链表中的特定值
- 如何为用户生成单个访问令牌
- 访问文件中的单个字符效率低下?(C++)
- 访问单个类型的可变参数模板
- 访问CV :: MAT_16UC1的单个元素
- 在单个链接列表中间删除一个节点,仅给定对该节点的访问
- 多个应用程序访问的单个进程
- 访问指针数组/通过另一个指针并删除数组中的单个元素而不删除第一个指针或整个数组
- 垫子类型转换和访问单个像素时出现问题
- OpenCV 3.1.0:从 UMat 访问单个元素
- 在c++中的字符串类数组中不打印任何内容,即使我可以访问单个元素
- RapidXML使用以前的属性值访问单个属性值
- 如何访问单个TListViewItem上的文本按钮
- 允许类访问单个私有成员
- 在 c++ 中使用数组参数访问单个元素的字符
- C/ c++:从指针字符一维数组中访问单个字符