30/09/2018, 16:39
Lỗi về sử dụng con trỏ trong danh sách liên kết
cho em hỏi về bài tập bucket sort, không hiểu sao em đã ghi đúng giải thuật mà khi chạy nó bị bug hoài cho em hỏi về cái lỗi của ngữ pháp ạ
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
using namespace std;
struct NODE
{
float _gtri;
struct NODE *_next;
};
struct LIST
{
struct NODE *_dau;
struct NODE *_cuoi;
};
void XUAT(LIST mother);
NODE*TAONUT(float gtri);
void THEMNUTVAO(LIST&mother, NODE*pnut);
int NHAP(LIST&mother);
LIST NOILIST(LIST&mother,LIST*&son,int n);
void SWAP(NODE*&a, NODE*&b);
void SAPXEP(LIST son);
void BUCKET_SORT(LIST&mother, int n);
int main()
{
LIST mother;
mother._dau=NULL;
int n=NHAP(mother);
XUAT(mother);
BUCKET_SORT(mother,n);
XUAT(mother);
system("pause");
}
void XUAT(LIST mother)
{
NODE*pnut=mother._dau;
while(pnut != NULL)
{
cout<<pnut->_gtri<<" ";
pnut=pnut->_next;
}
cout<<endl;
}
NODE*TAONUT(float gtri)
{
NODE*pnut=new NODE;
pnut->_gtri=gtri;
pnut->_next=NULL;
return pnut;
}
void THEMNUTVAO(LIST&mother, NODE*pnut)
{
if(mother. _dau == NULL)
{
mother. _dau=pnut;
mother. _cuoi=pnut;
}
else
{
mother. _cuoi-> _next = pnut;
mother. _cuoi = pnut;
}
}
int NHAP(LIST&mother)
{
int n=0,i=0;
cout<<"Nhap vao so luong phan tu: ";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"a["<<i<<"]= ";
float temp;
cin>>temp;
THEMNUTVAO(mother,TAONUT(temp));
}
return n;
}
LIST NOILIST(LIST&mother,LIST*&son,int n)
{
int i=0;
NODE*pnut=(son+i)->_dau;
NODE*bnut=mother._dau;
for(;;pnut=pnut->_next)
{
if(i<=n)
{
if(pnut==NULL)
{
i++;
pnut=(son+i)->_dau;
bnut->_next=pnut;
bnut=pnut;
}
else
{
bnut->_next=pnut;
bnut=pnut;
}
}
}
mother._cuoi=bnut;
mother._cuoi->_next=NULL;
return mother;
}
void SWAP(NODE*&a, NODE*&b)
{
NODE*temp=a;
a=b;
b=temp;
}
void SAPXEP(LIST son)
{
for(NODE *pnut=son._dau;pnut!=NULL;pnut=pnut->_next)
for(NODE *bnut=pnut->_next;bnut!=NULL;bnut=bnut->_next)
if(pnut->_gtri > bnut->_gtri)
SWAP(pnut,bnut);
}
void BUCKET_SORT(LIST&mother, int n)
{
int i=0;
float temp=mother._dau->_gtri;
for(NODE*pnut=mother._dau;pnut!=NULL;pnut=pnut->_next)
if(temp < pnut->_gtri)
temp=pnut->_gtri;
LIST*son=new LIST [n*int(temp)+sizeof(float)];
for(NODE*pnut=mother._dau;pnut!=NULL;pnut=pnut->_next)
THEMNUTVAO(son[n*int(pnut->_gtri)],pnut);
for(i=0;i<=n*int(temp);i++)
SAPXEP(son[i]);
mother=NOILIST(mother,son,n*int(temp));
}
Bài liên quan
I moved a post to an existing topic: Duplicate posts will be moved here