这是在传递int num时创建搜索功能的正确方法吗?使用链表列表

时间:2017-08-27 23:27:16

标签: c++

标题文件:

func findRect(forTextMatching match:String, in textField:UITextField) -> CGRect? {
    if let text = textField.text,
       let range = text.range(of: match)
    {
        let offset1 = text.distance(from: text.startIndex, to: range.lowerBound)
        let offset2 = text.distance(from: text.startIndex, to: range.upperBound)

        // beginningOfDocument is nil unless textField is first responder
        textField.becomeFirstResponder()
        defer {
            textField.resignFirstResponder()
        }

        if let pos1 = textField.position(from: textField.beginningOfDocument, offset: offset1),
            let pos2 = textField.position(from: textField.beginningOfDocument, offset: offset2)
        {
            if let textRange = textField.textRange(from: pos1, to: pos2)
            {
                var rect = textField.firstRect(for: textRange)

                // firstRect is actually relative to editingRect, not textField
                let top = textField.editingRect(forBounds: textField.bounds).origin.y
                let left = textField.editingRect(forBounds: textField.bounds).origin.x
                rect = CGRect(x: rect.origin.x+left, y: rect.origin.y+top, width: rect.width, height: rect.height)

                return rect
            }
        }
    }

    return nil
}

源文件:

#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

该程序将读入一个 名为“cmd.txt”的文本文件,用于指示要运行的操作。我们的程序应该实现一个C ++类,用于表示链表。

2 个答案:

答案 0 :(得分:1)

搜索应该更像:

node* LL::Search(int num)
{
    node* current = head;
    while (current != null)
    {
        if (current->val == num)
        {
            return current;
        }
        current = current->next;
    }
    return null;
}

答案 1 :(得分:0)

这是正确的格式

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;
    }