我的链表方法有问题
I'm having problems with my linkedlist method
我以前实现过这个 class,但它不起作用。我在 enqueue(edge)
方法中遇到错误。调试器说 head 不是空的,但它应该是。谁能解释一下为什么?
这是代码:
class eList
{
private:
class node
{
public:
edge e;
node * next;
node(edge x)
{
next = NULL;
e = x;
}
};
node * head, *tail;
public:
eList()
{
head = NULL;
tail = head;
}
这是我得到错误的方法:
void enqueue(edge component)
{
node * bby = new node(component);
if (head == NULL)
{
head = bby;
tail = head;
}
else
{
tail->next = bby;
tail = bby;
}
}
};
如何使用此列表:
class edgeTable
{
private:
//hash table conists of a table of lists
eList * table;
//size of table
int capacity;
//number of items in hash table
int numItems;
int createKey(edge e) {
unsigned int k1 = e.start->address * 37;
unsigned int k2 = e.end->address * 37;
return k1+k2%capacity;
}
int createKey(int addss) { return addss%capacity; }
void resize()
{
int oldCap = capacity;
capacity = capacity * 2;
edgeTable resized(capacity);
for (int i = 0; i < oldCap;i++)
resized.insert(table[i].strct());
table = resized.table;
}
void insert(int key, edge e)
{
table[key].enqueue(e);
numItems++;
}
public:
edgeTable()
{
capacity = 101;
table = new eList[capacity];
numItems = 0;
}
edgeTable(int cap)
{
capacity = cap;
table = new eList[capacity];
numItems = 0;
}
void insert(edge e)
{
insert(createKey(e), e);
if (numItems >= capacity / 2)
resize();
}
而这个 class 用于:
void addEdge(int x, int y, int w)
{
vertex * u = findVertex(x);
vertex * v = findVertex(y);
edge e(u,v,w);
u->edgeList.insert(e);
}
编辑:添加补充代码。
我暂时无法添加评论所以我回答,把它当作评论。
void enqueue(edge component)
确实没有问题,但我认为你在 void insert(edge e)
和 insert(createKey(e), e);
中有问题,尤其是 createKey(e)
.
在createKey
中k1+k2%capacit
不等于(k1+k2)%capacity
你要不要这个?因为是关键,它的意思是数组的索引,可能 return 错误的索引!
抱歉需要 50/50 才能发表评论!
我以前实现过这个 class,但它不起作用。我在 enqueue(edge)
方法中遇到错误。调试器说 head 不是空的,但它应该是。谁能解释一下为什么?
这是代码:
class eList
{
private:
class node
{
public:
edge e;
node * next;
node(edge x)
{
next = NULL;
e = x;
}
};
node * head, *tail;
public:
eList()
{
head = NULL;
tail = head;
}
这是我得到错误的方法:
void enqueue(edge component)
{
node * bby = new node(component);
if (head == NULL)
{
head = bby;
tail = head;
}
else
{
tail->next = bby;
tail = bby;
}
}
};
如何使用此列表:
class edgeTable
{
private:
//hash table conists of a table of lists
eList * table;
//size of table
int capacity;
//number of items in hash table
int numItems;
int createKey(edge e) {
unsigned int k1 = e.start->address * 37;
unsigned int k2 = e.end->address * 37;
return k1+k2%capacity;
}
int createKey(int addss) { return addss%capacity; }
void resize()
{
int oldCap = capacity;
capacity = capacity * 2;
edgeTable resized(capacity);
for (int i = 0; i < oldCap;i++)
resized.insert(table[i].strct());
table = resized.table;
}
void insert(int key, edge e)
{
table[key].enqueue(e);
numItems++;
}
public:
edgeTable()
{
capacity = 101;
table = new eList[capacity];
numItems = 0;
}
edgeTable(int cap)
{
capacity = cap;
table = new eList[capacity];
numItems = 0;
}
void insert(edge e)
{
insert(createKey(e), e);
if (numItems >= capacity / 2)
resize();
}
而这个 class 用于:
void addEdge(int x, int y, int w)
{
vertex * u = findVertex(x);
vertex * v = findVertex(y);
edge e(u,v,w);
u->edgeList.insert(e);
}
编辑:添加补充代码。
我暂时无法添加评论所以我回答,把它当作评论。
void enqueue(edge component)
确实没有问题,但我认为你在 void insert(edge e)
和 insert(createKey(e), e);
中有问题,尤其是 createKey(e)
.
在createKey
中k1+k2%capacit
不等于(k1+k2)%capacity
你要不要这个?因为是关键,它的意思是数组的索引,可能 return 错误的索引!
抱歉需要 50/50 才能发表评论!