Mình có học theo video này về danh sách liên kết trong C.
Danh sách liên kết trong C
Sau đó mình đã chuyển code trong video sang của C++.
Mà sao code chạy đến phần nhập xong data thì không in ra được nhỉ.
Mọi người giúp mình với.
Mình cảm ơn.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct SoNguyen
{
int data;
struct SoNguyen *Tiep;
};
typedef struct SoNguyen SONGUYEN;
void TaoDanhSach(SONGUYEN *pDau)
{
int repeat;
SONGUYEN *p;
do
{
if(pDau==NULL)
{
SONGUYEN *pDau = new SONGUYEN;
p = pDau;
}
else
{
p = pDau;
while(p->Tiep != NULL)
{
p = p->Tiep;
}
p->Tiep = new SONGUYEN;
p = p->Tiep;
}
cout<<"\nNhap du lieu:";
cin>>(p->data);
cout<<"Ban muon nhap tiep khong:";
cin>>repeat;
}while(repeat!=0);
}
void Xuat(SONGUYEN *pDau)
{
SONGUYEN *p = pDau;
if(p==NULL)
{
return;
}
while(p!=NULL)
{
cout<<(p->data);
p = p->Tiep;
}
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
SONGUYEN *pDau = NULL;
TaoDanhSach(pDau);
Xuat(pDau);
return 0;
}