【发布时间】:2013-08-03 03:00:58
【问题描述】:
在将边添加到某个配对顶点时,我在试图弄清楚如何正确获取指针时遇到了一些麻烦。
下面是关于在顶点和节点输入完成后链表应该是什么样子的简短想法。
我怎样才能在neighborList 上也保持秩序?如果当前顶点中已经存在顶点边,是否还有其他条件?
这是我试图构建的结构化类:
class graph{
private:
typedef struct node{
char vertex;
node * nodeListPtr;
node * neighborPtr;
}* nodePtr;
nodePtr head;
nodePtr curr;
public:
graph();
~graph();
void AddNode(char AddData);
void AddEdge(char V, char E);
void printList();
};
graph::graph(){
head = NULL;
curr = NULL;
}
// Adds a node to a linked list
void graph::AddNode(char AddData){
nodePtr n = new node;
n->nodeListPtr = NULL;
n->vertex = AddData;
if(head != NULL){
curr = head;
while(curr->nodeListPtr != NULL){
curr = curr->nodeListPtr;
}
curr->nodeListPtr = n;
}
else{
head = n;
}
}
// takes 2 Parameters (V is pointing to E)
// I want to set it up where the neighborptr starts a double linked List basically
void graph::AddEdge(char V, char E){
// New Node with data
nodePtr n = new node;
n->neighborPtr = NULL;
n->vertex = E;
// go to the first node in the nodeList and go through till you reach the Vertex V
curr = head;
while(curr->vertex != V){
curr = curr->nodeListPtr;
}
//Once the Vertex V is found in the linked list add the node to the neighborPtr.
curr->neighborPtr = n;
}
【问题讨论】:
-
这应该是一个通用图吗?如果是这样,请考虑使用邻接列表:en.wikipedia.org/wiki/Adjacency_list
标签: c++ class graph doubly-linked-list