繁体   English   中英

在C ++中搜索和保护单链列表

[英]Search and protecting singly linked list in C++

在使用C ++的情况下,我有一个关于在ints单链接列表中搜索元素的问题。 我正在创建自己的锻炼列表。 这是代码

假设我有两个搜索功能。 我知道我们需要遍历整个列表直到找到元素,因为我们没有像数组那样的直接访问。 这两个功能是:

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种情况:我的列表包含以下元素:[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

您第一个问题的答案取决于您的需求。 如果您将其作为学习项目进行,请实施您认为合适的一切。 您所描述的内容适合按值搜索。

在这种情况下,防止用户直接访问您的Node成员的最佳方法是完全抽象Node类型。 您可以简单地通过在源文件中声明和定义Node并在标头中使用Node *的正向声明来做到这一点。 这样,包含您的标头的用户将不会对您的Node类型有任何概念。

// SinglyLinkedList.h
class SinglyLinkedList {
  //...//
  struct Node* head; // head node is forward declared
  //...//
}

// SinglyLinkedList.cc
struct Node {
  //...
};

// define ll methods

如果确实希望用户知道Node类型,则一种解决方案是将其成员设为私有,创建一个公共值访问器方法,并使Node成为SinglyLinkedList类的朋友

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM