我想知道確定單鏈表是否爲空的最好和最簡單的方法。檢查單向鏈表是否爲空C++
我需要創建一個布爾方法嗎?
感謝
Read方法
無效列表::閱讀(istream的& R) {
char c[13];
r >> c;
r >> numberOfInts;
Node *node = new Node();
for(int i = 0; i < numberOfInts; i++)
{
r >> node->data;
cout << node->data << endl;
node->next = new Node;
//node = node->next;
head = node;
}
}
else
{
if(node->data > head->data)
{
head->next;
}
else if(node->data < head->data)
{
Node* tempNode;
tempNode = head;
head->data = node->data;
node->data = tempNode->data;
}
}
system("pause");
}
頭文件
class Node
{
public:
Node() {}
Node(int d, Node* q = 0) : data(d), next(q) {} //constructor with parameters data and next
int data; //holds data in node
Node* next;//pointer to next node
};
class List
{
public:
void Read(istream&);
void Write(ostream&);
void setReadSort(bool);
void sortOwn();
void insertionSort(Node*);
bool isEmpty();
bool _sortRead;
int numberOfInts;
List(void);
~List(void);
protected:
Node *head;
Node current;
};
對不起,如果您需要了解更多信息 – 2011-04-27 16:19:01
「列表」,不應該有一個「當前」數據成員,因爲「當前」節點僅在列表中執行某些操作的情況下才有意義 - 根據定義,它是暫時的,而不是數據模型的一部分。 – 2011-04-27 17:39:43