Entry0x8 Entry0x8 -4 years ago 80
C Question

Searching element more than once - Singly Linked List

I have a singly linked-list like so:

struct ctx {
PVOID EMV;
PVOID *track1;
PVOID *track2;

ctx *Next;
};

struct pci808_ {
DWORD SKU_id;
DWORD Purchase_id;
DWORD *Orders;

DWORD UniqueOffset;

DWORD_PTR Ordinal;
DWORD_PTR Export;

pci808_ *Next;
ctx *Bridge;
};
extern pci808_ pci808_global;


I search the singly linked-list like so:

PVOID Parser::Search(pci808_ *head, DWORD SKU_id) {
EnterCriticalSection(&Parser_CSection);
Parser::POS_Tools::pci808_ *Temp = head;
while (Temp != ERROR) {
if (PE::SearchUniqueOffset(Temp->UniqueOffset, WSIF_CONTEXT_HANDLER|WSIF_MIX_TRACK_DATA, FALSE) == Global::Flags::TERMINAL_DATA_OK) {
LeaveCriticalSection(&Parser_CSection);
return Temp;
}
Temp = Temp->Next;
}
LeaveCriticalSection(&Parser_CSection);
return ERROR;
}


I call it like so:

Search(pci808_global, some_id_here);


So the problem is, after I search the data once I cannot search for the same element again.

So if I do something like

Search(pci808_global, 48540);
Search(pci808_global, 48540);


Then the data inside the
ctx
struct is empty for the current element. But also,
Temp
in
Parser::Search
assumes that it left off on the same
head
instance that it was at when it was called the previous time ... What am I doing wrong here?

Answer Source

To be able to search more than one occurence, you must:

  • either search for all occurences and returns a list (a C++ vector, a C dynamic array, etc.)
  • or create a context on first call that is used for following ones to keep where to restart the search (ala strtok static way or strtok_r non static way of the C library)
  • or use an iterator that internally stores the above context and return next occurence on each call (like the C++ standard library iterators)

Said differently you problem is not in the implementation but in the conception phase.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download