这是在传递 int num 时创建搜索函数的正确方法吗?使用链表库
Is this the proper way to create a search function while passing int num? Using a linked list libary
头文件:
#ifndef LL_H
#define LL_H
// include this library to use NULL, otherwise use nullptr instead
#include <cstddef>
// include iostream so anything that includes this file can use cout
#include <iostream>
// Struct which will be the building block of our list
struct node
{
int val;
node* next;
};
// Contents of 11.h
// Linked list class definition
class LL
{
public:
LL();
bool removeFront();
bool removeBack();
node* search(int);
void print();
private:
node* head;
};
#endif
源文件:
#include "ll.h"
LL::LL()
{
head = NULL;
}
void LL::search (int num)//heading of the function
{
node* newNode = new node;
newNode->val = num;
newNode->next = NULL;
while (newNode == NULL)
{
if (newNode->val == num)
{
return newNode;
}
newNode = newNode->next; //point to the next node
}
return; //not sure if needed
}
该程序将在 名为"cmd.txt"的文本文件,它将指示要运行的操作。我们的程序应该实现一个C++类,该类将用于表示链表。
搜索应该看起来更像:
node* LL::Search(int num)
{
node* current = head;
while (current != null)
{
if (current->val == num)
{
return current;
}
current = current->next;
}
return null;
}
这是正确的格式
node* LL::search(int num)
{
node* newNode = new node;
newNode = head;
while (newNode->next != NULL)
{
if (newNode->val == num)
{
return newNode;
}
newNode = newNode->next;
}
return NULL;
}
相关文章:
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 这是在传递 int num 时创建搜索函数的正确方法吗?使用链表库
- 在C++中实现双链表的最佳操作系统不可知方法
- 双向链表插入方法实现 - 正在搜索哪个节点
- 链表的析构函数方法
- 在没有 const 强制转换的链表中实现高级方法
- 是否可以从 const 方法迭代链表
- 对链表复制构造函数和赋值运算符使用 copy() 方法
- 这是在双链表的第一个节点之后插入节点的正确方法吗?
- C++,在哈希表中显示内容的方法.使用双链表
- 这是删除整个链表的有效方法吗?
- 关于链表,这两种方法有什么不同?C++中的1个自变量与2个自变量
- 通过此方法删除链表中的数据会发生什么情况
- 如何为链表编写_find方法
- 编码链表的更好方法
- C++将节点添加到链表的开头一次后失败.其他方法也有问题
- 在常量时间内表达单向链表方法:O(1)
- 在 Mac 上的 Xcode 中使用 C++ 中的堆栈、队列和链表的最简单方法是什么?
- 双向链表气泡排序,反向方法现已中断
- 链表推送方法