【问题标题】:How to do linear search using pointers?如何使用指针进行线性搜索?
【发布时间】:2017-01-04 03:53:59
【问题描述】:

我还是编程新手。我只是想问我如何使用指针进行线性搜索。我想制作一个图书管理程序,并且我制作了一个带有指针的程序。

This is the example of how i want it.

这是编码

#include <iostream>
#define MAX 5
using namespace std;
struct record
{
int id;//stores id
float price;//store price
int qty;//stores quantity
record* next;//reference to the next node
};

record* head;//create empty record
record* tail;//the end of the record
void push(record *& head, record *&tail, int id, float price, int qty)
{
if (head == NULL)
{
    record* r = new record;
    r->id = id;
    r->price = price;
    r->qty = qty;
    r->next = NULL;//end of the list
    head = r;
    tail = r;
}
else if (head != NULL && (MAX - 1))
{
    record* r = new record;
    r->id = id;
    r->price = price;
    r->qty = qty;
    r->next = head;
    head = r;
}
}

int  pop(record *&head, record *& tail)
{
if (head == NULL)
{
    cout << "No record in memory" << endl;
}
 else if (head == tail)
 {
    cout << "The record "<<"ID: " << head->id << "\nPrice: " << head->price             << "\nQuantity: " << head->qty << "\n" << "was deleted" << endl; //CORRECTION  HERE
}
else
{
    record* delptr = new record;
    delptr = head;
    head = head->next;
    cout << "The record " << delptr->id << ", " << delptr->price << ", " << delptr->qty << " was deleted" << endl; //CORRECTION HERE
    delete delptr;

}
return 0;
}


void display(record *&head)
{
record* temp = new record; //CORRECTION HERE
temp = head;
if (temp == NULL)
{
    cout << "No record in memory" << endl;

}
else
{


        cout << "Record : " << endl;
        while (temp != NULL)
        {
            cout <<"\nID: "<< temp->id << "\nPrice: " << temp->price << "\nQuantity: " << temp->qty <<"\n"<< endl;  //CORRECTION HERE
            temp = temp->next;
        }

  }
}

  int LinearSearch(record *&head) {


}

char menu()
{
char choice;

cout << "\t::MENU::\n" << endl;
cout << "1. Add new record\n" << endl;
cout << "2. Delete record\n" << endl;
cout << "3. Show record\n" << endl;
cout << "4. Quit\n" << endl;
cout << "-----------------------\n" << endl;
cout << "\nEnter selection : " << endl;
cin >> choice;
return choice;
}

int main()
{
record* head;
record* tail;
head = NULL;
tail = NULL;
char choice;
do
{
    cout << "---------------------- - \n" << endl;
    choice = menu();
    switch (choice) {   //CORRECTION HERE
    case '1':
        int id, qty;
        float price;
        cout << "Enter ID:";
        cin >> id;   // Please correct yourself here, what is r here, r is not declared anywhere
        cout << "\nEnter Price: ";
        cin >> price;
        cout << "\nEnter Quantity: ";
        cin >> qty;
        push(head, tail, id, price, qty);
        break;
    case '2':
        pop(head, tail);
        break;
    case'3':
        display(head);
        break;
    default:
        cout << "Quiting...\n";
    }

} while (choice != '4');

return 0;
}

如何为这种编码编写指针代码的线性搜索?我尝试在整个网络上查找示例,但当我执行它时,它不起作用,所以我将其留空。

【问题讨论】:

  • 线性搜索就是遍历链表。请阅读更多相关信息并尝试实施。
  • 我建议使用标准容器。向量或列表。然后您将拥有可以使用的查找功能。
  • 同意保罗。首先使用预定义的数据结构学习(C++)编程的基础知识。您需要自己学习实现数据结构的指针,但那时您应该已经精通使用数据结构。

标签: c++ pointers search


【解决方案1】:

嗯,我看到你有一个列表,你正在处理指针。

如果你想在记录id中做线性搜索,例如,你可以这样做:

record *aux = head;
while(aux != NULL){
    if(aux->id == id_you_want_to_find){
        printf("I found it\n");
    }
    aux = aux->next;
}

你通常使用object.attribute来访问普通对象的属性,但是当你有一个指向对象的指针时,你必须使用pointerToObject-&gt;attribute

【讨论】:

    【解决方案2】:

    您可以根据需要编写一个,但如果已经存在为您执行此操作的库,则无需编写。由于您使用的是列表结构,因此我使用简单的std::list 来展示这一点。您也可以将其更改为 std::vector 并使用索引符号进行简单的 for 循环迭代,因为搜索它们的速度是恒定的,而不是线性的。这是通过线性列表进行搜索的一种方法。

    #include <list>
    
    record* searchRecords( std::list<record>& records, int id ) {
        if ( records.empty() ) {
            std::ostringstream strStream;
            strStream << __FUNCTION__ << " Invalid list of records: list is empty.";
            throw ExceptionHandler( strStream ); // Not Written, but what should be done instead of returning.
            return nullptr;
        }
    
        std::list<record>::iterator it = records.begin();
        while ( it != records.end() ) {
            if ( it->id == id ) {
                return (&(*it));
            } 
            ++it;               
        }
    
        std::ostringstream strStream;
        strStream << __FUNCTION__ << " No entry found in search with ID{" << id << "}.";
        Logger::log( strStream, Logger::LOGGER_INFO ); // Not implemented here same as above for ExceptionHandler
        return nullptr;
    }
    

    由于链表不是关联的,它们必须从头到尾遍历列表中的每个条目 N 以插入、查找或删除。这里的时间复杂度是线性的。

    如果您希望在大型列表中更快地插入时间,您可以使用&lt;multiset&gt;(如果可能存在重复项)或&lt;set&gt;(如果每个已知项都是唯一的)。这些具有即时插入功能。如果您想要持续搜索并且不关心插入时间,那么 &lt;vector&gt; 就是您想要的。

    【讨论】:

      【解决方案3】:

      通常的答案是:不要自己编写线性搜索,它称为std::find_if。但是,C++ 期望您的数据结构公开迭代器。迭代器指的是一条记录(或列表的末尾)。在记录上调用operator*获取实际记录,调用operator++获取下一条记录。

      是的,这类似于指针。这是故意的;指针连续数组的迭代器。这意味着您可以在数组上调用std::find_if。但是由于您选择实现链表而不是数组,因此您需要实现自己的迭代器类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-24
        • 1970-01-01
        • 2014-05-29
        • 1970-01-01
        • 1970-01-01
        • 2019-03-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多