Aristides
Senior Member
Mọi người ơi, không biết cái linked list này bị lỗi chỗ nào mà biên dịch thành công, runtime thành công mà bảng console lại không hiện số là sao?
C++:
#include<iostream>
template<class X>
struct Node
{
Node<X>* next;
X value;
Node()
{
next = nullptr;
value = 0;
}
Node(X val)
{
next = nullptr;
value = val;
}
Node(X val, Node *nextnode)
{
next = nextnode;
value = val;
}
};
/*****************************************************/
template<class Y>
class LinkedList
{
private:
Node<Y> *head;
unsigned int size;
public:
LinkedList()
{
head = nullptr;
size = 0;
}
void push(Y val)
{
if (size == 0)
{
head = new Node<Y>(val);
size++;
return;
}
Node<Y> *dummy = new Node<Y>();
dummy->next = head;
while (head != nullptr)
{
head = head->next;
}
head = new Node<Y>(val);
head = dummy->next;
size++;
}
unsigned int length()
{
return size;
}
};
int main()
{
int n = 10;
std::cout << "[ ";
LinkedList<int> ListNode;
for (int i = 1; i <= n; i++)
{
ListNode.push(i);
std::cout << ", ";
}
std::cout << "]" << endl;
std::cout << "Size: " << ListNode.length() << std::endl;
return 0;
}
/* Output:
*
* [ , , , , , , , , , ]
* Size: 10
*
*/