0
在向特定的成對頂點添加邊時,如何確定如何正確獲取指針,遇到了一些麻煩。使用鏈接列表的圖表表示
下面是關於頂點和節點完成輸入後鏈接列表應該如何看起來的一個簡短概念。
我該如何在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;
}
這應該是一個普通圖嗎?如果是這樣,請考慮使用鄰接列表:http://en.wikipedia.org/wiki/Adjacency_list –