#include<iostream>
using namespace std;
class TCSGraph{
public:
void addVertex(int vertex);
void display();
TCSGraph(){
head = NULL;
}
~TCSGraph();
private:
struct ListNode
{
string name;
struct ListNode *next;
};
ListNode *head;
}
void TCSGraph::addVertex(int vertex){
ListNode *newNode;
ListNode *nodePtr;
string vName;
for(int i = 0; i < vertex ; i++){
cout << "what is the name of the vertex"<< endl;
cin >> vName;
newNode = new ListNode;
newNode->name = vName;
if (!head)
head = newNode;
else
nodePtr = head;
while(nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
void TCSGraph::display(){
ListNode *nodePtr;
nodePtr = head;
while(nodePtr){
cout << nodePtr->name<< endl;
nodePtr = nodePtr->next;
}
}
int main(){
int vertex;
cout << " how many vertex u wan to add" << endl;
cin >> vertex;
TCSGraph g;
g.addVertex(vertex);
g.display();
return 0;
}
2
A
回答
2
有一個問題,您addvertex
方法:
您有:
if (!head)
head = newNode;
else
nodePtr = head;
while(nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
,但它應該是:
if (!head) // check if the list is empty.
head = newNode;// if yes..make the new node the first node.
else { // list exits.
nodePtr = head;
while(nodePtr->next) // keep moving till the end of the list.
nodePtr = nodePtr->next;
nodePtr->next = newNode; // add new node to the end.
}
而且你是不是使newNode
NULL
的next
字段爲:
newNode = new ListNode;
newNode->name = vName;
newNode->next= NULL; // add this.
另外它是一個很好的實踐來釋放動態分配的內存。因此,而不是有一個空的析構函數
~TCSGraph();
你可以釋放列表中的dtor。
編輯:更多的錯誤
你有一個丟失;類聲明後:
class TCSGraph{
......
}; // <--- add this ;
此外您的析構函數僅聲明。沒有def。如果你不想給任何def,你至少必須有一個空的身體。因此,與
~TCSGraph(){}
0
相關問題
- 1. 創建鄰接表
- 2. 如何從鄰接表創建圖形圖像?
- 3. 如何在繪製圖形後創建鄰接列表?
- 4. 從鄰接表中創建無向圖
- 5. 創建一個鄰接表
- 6. 創建鄰接單鏈表
- 7. 圖構建鄰接表
- 8. 使用Silverlight創建圖表/圖形
- 9. 在Python/R中創建節點邊緣三角形鄰接圖
- 10. 我想創建一個圖形使用矩陣鄰接輸入,任何想法?
- 11. 從JUNG圖創建鄰接矩陣
- 12. 使用鄰接列表和鄰接矩陣的圖的大小?
- 13. 從文件創建鄰接表
- 14. 爲DFS創建鄰接列表
- 15. NetLogo:如何導入2列相鄰列表來創建圖形?
- 16. 從python中的矩陣創建鄰接列表圖表
- 17. 使用igraph創建鄰接網絡矩陣(或列表)igraph
- 18. 在Python中使用字符創建鄰接列表
- 19. 如何在C++中使用向量對創建鄰接列表?
- 20. 鄰接表和圖
- 21. 使用Raphaël創建圖形
- 22. 用HashMap [Int,Vector [Int]](Scala)表示圖形(鄰接列表)?
- 23. 試圖使用鏈接列表和向量使鄰接列表
- 24. 從圖中創建鄰接表的兩種實現方式
- 25. 在C++中創建一個有向圖的鄰接表
- 26. 我使用鄰接表
- 27. Python:創建列表圖形
- 28. 使用Boost圖庫創建圖形
- 29. 多圖和鄰接表
- 30. 底圖:使用圖形和地圖創建圖形
哦,我的錯誤>< 但我仍面臨 這裏同樣的錯誤是這些錯誤消息: >錯誤:新的類型可能無法在返回類型中'addVertex」的聲明來定義>兩個或多個數據類型| >「TCSGraph TCSGraph :: addVertex(int)'的原型與'TCSGraph'類中的任何一個都不匹配。 > error:candidate is:void TCSGraph :: addVertex(int)| > error:'TCSGraph TCSGraph :: addVertex(int)'和'void TCSGraph :: addVertex(int)'不能被重載 – sum1needhelp 2010-04-20 06:07:33