【发布时间】:2011-04-27 16:16:40
【问题描述】:
我正在尝试找出确定单链表是否为空的最佳和最简单的方法。
我需要创建一个布尔方法吗?
谢谢
读取方法
void List::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;
};
【问题讨论】:
-
抱歉,如果您需要更多信息
-
'List' 不应有 'current' 数据成员,因为“current”节点仅在对列表执行某些操作的上下文中才有意义 - 根据定义,它是临时的,而不是数据模型的一部分。
标签: c++ if-statement singly-linked-list