Chào mọi người, mọi người có thể xem bài này lỗi ở đâu không ạ?
#include "stdafx.h"
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#include <windowsx.h>
#include <tchar.h>
using namespace std;
template <class T> class Node
{
private:
T1 key;
Node<T>* next;
public:
Node(T k)
{
key = k;
next = NULL;
}
~Node() {};
friend class List;
};
template <class T> class List
{
Node<T>* first;
public:
List()
{
first = NULL;
}
~List() {}
void PrintList();
void InsertFirst(T k);
void InsertLast(T k);
bool DeleteNode(T k);
//Chen y vao sau x
void InsertAfter(T x, T y);
//Kiem tra co trong danh sach khong
bool search(T x);
void nhapds(List<T> &l);
};
template <class T> void List<T>::nhapds(List<T> &l)
{
int n;
cout << "\nnhap so ptu ban dau cua List: ";
cin >> n;
for (int i = 1; i <= n; i++)
InsertFirst(i);
cout << endl;
};
template <class T> void List <T>::PrintList()
{
for (Node*p = first; p != NULL; p = p->next)
{
cout << p->key << ' ';
}
cout << endl;
};
template <class T>
void List<T>::InsertFirst(T k)
{
Node<T>* pNew = new Node(k);
pNew->next = first;
first = pNew;
};
template <class T>
void List<T>::InsertLast(T k)
{
cout << "\nNhap k = ";
cin >> k;
Node* pNew = new Node(k);
Node* t = first;
while (t->next != NULL) {
t = t->next;
}
t->next = pNew;
pNew->next = NULL;
};
template <class T>
bool List<T>::DeleteNode(T k)
{
cout << "\nNhap k = ";
cin >> k;
Node*p = first;
Node* temp = NULL;
if (first == NULL || search(k) == false)
{
cout << "Khong tim thay Node co gia tri " << k << " trong danh sach\n";
return false;
}
else
{
if (p->key == k)
{
first = p->next;
delete p;
}
else
{
while (p != NULL && p->key != k)
{
temp = p;
p = p->next;
}
temp->next = p->next;
delete p;
}
}
return true;
};
template <class T>
void List<T>::InsertAfter(T x, T y)
{
Node* t = first;
Node* pNew = new Node(y);
Node* p;
if (first == NULL || search(x) == false)
cout << "Khong tim thay Node co gia tri " << x << " trong danh sach\n";
else {
while (t != NULL && t->key != x)
{
t = t->next;
}
p = t->next;
t->next = pNew;
pNew->next = p;
}
};
template <class T>
bool List<T>::search(T x)
{
for (Node*p = first; p != NULL; p = p->next)
{
if (p->key == x)
return true;
}
return false;
};
void main()
{
List<int> L;
int k = 0;
L.nhapds(L);
cout << "Danh sach:\n";
L.PrintList();
cout << endl;
cout << "Them vao cuoi danh sach cac gia tri:\n";
L.InsertLast(k);
L.PrintList();
cout << endl;
cout << "Xoa nut trong danh sach\n";
L.DeleteNode(k);
L.PrintList();
cout << endl;
cout << "Chen nut 100 sau nut 9\n";
L.InsertAfter(9, 100);
L.PrintList();
List<float> A;
float x = 0;
A.nhapds(A);
cout << "Danh sach:\n";
A.PrintList();
cout << endl;
cout << "Them vao cuoi danh sach cac gia tri:\n";
A.InsertLast(k);
A.PrintList();
cout << endl;
cout << "Xoa nut trong danh sach\n";
A.DeleteNode(x);
A.PrintList();
cout << endl;
cout << "Chen nut 100 sau nut 9\n";
A.InsertAfter(12, 100);
A.PrintList();
}