Xuất màn hình của linked list

a/c cho em hỏi, em viết code như này tại sao lúc xuất màn hình không được ạ, mặc dù vẫn chạy được

#include<iostream>
#include<string>
using namespace std;
class sinhvien{
	private:
		long ma;
		char ten[8];
	public:
		void xuat(){
			cout<<"ma: "<<ma<<endl;
			cout<<"ten: "<<ten<<endl;
		}
		void nhap(){
			cout<<"nhap ma: "; cin>>ma; cin.ignore();
			cout<<"nhap ten: "; gets(ten);
		}
};
struct node{
	sinhvien info;
	node *next;
};
node* khoitaonode(sinhvien x){
	node*p=new node;
	p->info=x;
	p->next=NULL;
	return p;	
}
class list{
	private:
	node *phead;
	node *ptail;
	public:
		friend void themsauk(list &l,node *p);
		friend void xemmh(list l);
		friend void danhsach(list &l);
		friend void themcuoi(list &l, node *p);
		friend void themsauk(list &l,node *p, int n);
		friend void xemmh(list l);
		};
void danhsach(list &l){
	l.phead=NULL;
	l.ptail=NULL;
}
void themcuoi(list &l, node *p){
	if(l.ptail==NULL){
		l.phead=l.ptail=NULL;
	}
	else{
		l.ptail->next=p;
		l.ptail=p;
	}
}
void themsauk(list &l,node *p, int n){
	cout<<"nhap phan tu thu k can them vao sau: "; cin>>n; cin.ignore();
	int dem=0;
	for(node *k=l.phead;k!=NULL;k=k->next){
		dem++;
		if(dem==n){
			node*h=khoitaonode(p->info);
			node*g=k->next;
			h->next=g;
			k->next=h;
		}
	}
}
void xemmh(list l){
	for(node *k=l.phead;k!=NULL;k=k->next){
		k->info.xuat(); cout<<endl;
	}
}
main(){
	list l; danhsach(l); int n; sinhvien x;
	cout<<"nhap so node cam them :"; cin>>n;
	for(int i=0;i<n;i++){
		x.nhap();
		node *p=khoitaonode(x);
		themcuoi(l,p);
	}
	xemmh(l); 	
}

Trong hàm void themcuoi(list &l, node *p). Có thêm vào đâu mà in ra được bạn. :slight_smile:

3 Likes

mình cảm ơn nhiều nha

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?