【发布时间】:2019-07-24 00:05:43
【问题描述】:
我有一个关于在 ints 的单链表上搜索元素的问题,在这种情况下,使用 C++。我正在创建自己的锻炼列表版本。这是代码
假设我有两个搜索功能。我知道我们需要遍历整个列表直到找到元素,因为我们没有像数组那样的直接访问权限。 这两个功能是:
bool search(int n); // Traverse the list till find n.
bool search(Node* node, int n); Traverse the list till find n only after *node (included)
1 例:我的列表有以下元素:[0, 1, 2, 3]
如果我搜索 3,我很容易在列表末尾找到。不错。
问题: 2 case:我的列表有以下元素:[0, 1, 2, 3, 3, 3, 4, 5, 6]
如果我搜索 3:
bool search(int n);
我将始终获取第一个 3 元素,除非我有对第二个或第三个 3 元素的引用以传递给该函数:
bool search(Node* node, int n);
我的问题是这是否是单链表中的正确搜索算法。这两种类型的函数或者我是否应该有其他类型。
下面是我的实际代码的代码(我没有放代码用于搜索):
SingleLinkedList.h
struct Node {
int data;
Node* next;
Node(int d = 0)
: data {d}, next {nullptr}
{}
};
class SinglyLinkedList {
public:
SinglyLinkedList();
~SinglyLinkedList();
void display();
bool addFirst(const int); // Add a node to the beginning of the list.
bool addFirst(Node*); // Add a node to the beginning of the list.
bool addLast(const int); // Add a node to the end of the list.
bool addLast(Node*); // Add a node to the end of the list.
private:
Node* head;
Node* tail;
};
SinglyLinkedList.h
#include "SinglyLinkedList.h"
#include <iostream>
SinglyLinkedList::SinglyLinkedList()
: head {nullptr}, tail {nullptr}
{}
SinglyLinkedList::~SinglyLinkedList() {
Node* iterationNode = head;
Node* actualNode {nullptr};
while (iterationNode != nullptr) {
actualNode = iterationNode;
iterationNode = iterationNode->next;
delete actualNode;
}
}
void SinglyLinkedList::display() {
std::cout << "################### Displaying Linked List ###################" << std::endl;
if (head == nullptr) {
std::cout << "Linked List is empty!" << std::endl;
}
else {
Node* iterationNode = head;
std::cout << "[ ";
while (iterationNode != nullptr) {
std::cout << iterationNode->data << " ";
iterationNode = iterationNode->next;
}
iterationNode = nullptr;
std::cout << "]" << std::endl;
}
std::cout << "##############################################################" << std::endl;
}
bool SinglyLinkedList::addFirst(const int n) {
Node* element = new Node {n};
if (head == nullptr) {
head = element;
tail = element;
}
else {
element->next = head;
head = element;
}
return true;
}
bool SinglyLinkedList::addFirst(Node* element) {
if (head == nullptr) {
head = element;
tail = element;
}
else {
element->next = head;
head = element;
}
return true;
}
bool SinglyLinkedList::addLast(const int n) {
Node* element = new Node {n};
if (head == nullptr) {
head = element;
tail = element;
}
else {
tail->next = element;
tail = element;
}
return true;
}
bool SinglyLinkedList::addLast(Node* element) {
if (head == nullptr) {
head = element;
tail = element;
}
else {
tail->next = element;
tail = element;
}
return true;
}
Program.cpp
#include <iostream>
#include "SinglyLinkedList.h"
int main() {
{
SinglyLinkedList list;
list.display();
list.addFirst(5);
list.addFirst(4);
list.addFirst(3);
Node* secondNode = new Node {2};
list.addFirst(secondNode);
Node* firstNode = new Node {1};
list.addFirst(firstNode);
Node* zeroNode = new Node;
list.addFirst(zeroNode);
list.addLast(6);
list.display();
}
system("pause");
}
另一个问题是,我怎样才能保护我的结构,以使程序的用户不会弄乱直接更改链接/引用。例如,在Program.cpp 中,任何程序员都可以简单地这样做:
secondNode->next = zeroNode
【问题讨论】:
-
您为什么要手动执行此操作,而不是使用标准的
std::list和std::find()? -
@RemyLebeau 因为我正在创建自己的链接列表版本,以便更好地理解、练习和学习。
标签: c++ algorithm search linked-list singly-linked-list