【发布时间】:2015-11-20 09:58:56
【问题描述】:
我之前已经实现过这个类,但它不起作用。我在 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();
}
这个类用于:
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);
}
编辑:添加补充代码。
【问题讨论】:
-
你能显示更多代码吗,例如你如何使用你的清单?
-
@JBL 添加了更多代码。
标签: c++ data-structures graph linked-list