我試圖做一個seq搜索,檢查重複鍵,如果存在,我只需要更新值。但是當我嘗試使用鏈表來完成時,我遇到了內存問題。使用鏈接列表而不使用重複鍵的順序搜索
無需檢查重複鍵(在代碼中註釋掉)就可以正常放置值的代碼正常工作。
class seqSearch
{
public:
int N;
//helper class node
class node
{
public:
char key;
int val;
node *next;
node(){ key = NULL; val = NULL; next = NULL;}
node(char k,int v, node *nxt)
{
this->key = k;
this->val = v;
this->next = nxt;
}
};
node *first;
seqSearch(){N=0;}
bool isEmpty(){return first == NULL;}
int size(){return N;}
void put(char,int);
int get(char);
};
void seqSearch::put(char k, int v)
{
/*
node *oldfirst = first;
//first = new node(k,v,oldfirst);
first = new node;
first->key = k;
first->val = v;
first->next = oldfirst;
N++;
*/
for (node *x = first; x!= NULL; x=x->next)
{
//node *oldfirst = first;
//first = new node(k, v, oldfirst);
if(x->key == k)
{
x->val = v;
return;
}
}
first = new node(k, v, first); N++;
}
定義「內存問題」 – 2013-04-08 19:48:53
什麼錯誤,你得到 – nommyravian 2013-04-08 19:51:34