PHP Code:
#include <iostream.h>
#include <conio.h>
class Node
{
private:
float data;
Node *next;
public:
Node()
{
data = 0;
next = NULL;
}
Node(float x)
{
data = x;
next = NULL;
}
void setnext(Node *p)
{
next = p;
}
Node *getnext()
{
return next;
}
void setdata(float x)
{
data = x;
}
float getdata()
{
return data;
}
};
class List
{
private:
Node *head;
public:
List()
{head = NULL;}
List(Node *x)
{head = x;}
void chenxapxep(float x)
{
Node *p,*q;
p = new Node(x);
q = head;
if(head == NULL)
head = p;
else
{
if(p->getdata() < head->getdata())
{
p->setnext(head);
head = p;
}
else
{
while(q->getnext() )
{
if((p->getdata() > q->getdata()) && (p->getdata() < q->getnext()->getdata()))
{
p->setnext(q->getnext());
q->setnext(p);
}
q = q->getnext();
}
}
}
}
void show()
{
Node * p = head; // cho p = head
if( head != NULL)
{
while(p->getnext() != NULL) //kiem tra den chung nao p = Null
{
cout<<p->getdata()<<" ";// hien ra data cua p
p = p->getnext(); // lai cho no tro den node tiep theo;
}
}
}
};
void main()
{
float so;
List p;
do
{
cout<<"Nhap vao so: ";
cin>>so;
p.chenxapxep(so);
}while(so != 0);
p.show();
getch();
}
Cho mình hỏi chút, cái hàm vừa chèn vừa xắp xếp của mình chạy toàn bị sai ko biết có lỗi cú pháp nào không mong các đại ca sửa dùm em.