如何使用指针进行线性搜索?

时间:2017-01-04 03:54:00

标签: c++ pointers search

我还是编程新手。我只是想问一下如何使用指针进行线性搜索。我想制作一本图书管理程序,我已经制作了一个编写指针的程序。

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

如何为此编码编写指针代码的线性搜索?我尝试在整个网络上找到例子,当我执行它时,它没有用,所以我把它留空了。

3 个答案:

答案 0 :(得分:0)

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

例如,如果要在记录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->attribute

答案 1 :(得分:0)

如果您愿意,您可以编写一个,但是当您已经存在为您执行此操作的库时,则无需编写。由于您使用的是列表结构,因此我使用简单的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从头到尾遍历插入,查找或删除。这里的时间复杂度是线性的。

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

答案 2 :(得分:0)

正常的答案是:不要自己写一个线性搜索,它叫做std::find_if。但是,C ++希望您的数据结构公开迭代器。迭代器是指记录(或列表的末尾)。您可以通过在记录上调用operator*来获得实际记录,并通过调用operator++获得下一条记录。

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