các anh có thể giải thích dùm em vì sao:
-Trong khi em chỉ gán l.ptail->next=p;
- thì l.phead->next chuyển từ không có giá trị thành có giá trị
em cảm ơn rất nhiều ạ
#include<iostream>
#include<conio.h>
using namespace std;
struct node{
int data;
node *pNext;
};
typedef struct node NODE;
struct list{
NODE *pHead;
NODE *ptail;
};
typedef struct list LIST;
NODE* getNode(int x){
NODE *p=new NODE;
if(p==NULL)return NULL;
p->data=x;
p->pNext=NULL;
return p;
}
void addHead(LIST &l,NODE *p){
if(l.pHead==NULL){
l.ptail=l.pHead=p;
}
else{
p->pNext=l.pHead;
l.pHead=p;
}
}
void init(LIST &l){
l.ptail=NULL;
l.pHead=NULL;
}
void addTail(LIST &l,NODE *p){
if(l.ptail==NULL){
l.ptail=l.pHead=p;
cout<<"gia tri cua l.phead->next \t\t\t"<<l.pHead->pNext;
}
else{
cout<<"l.head->next luc tail chua gan \t\t\t"<<l.pHead->pNext<<endl;
l.ptail->pNext=p;
cout<<"l.head->next luc tail da gan \t\t\t"<<l.pHead->pNext<<endl;
l.ptail=p;
}
}
void insert(LIST &l,NODE *p){
char c;
int x;
do{
cout<<"nhap x \t";fflush(stdin);cin>>x;
p=getNode(x);
cout<<"gia tri p la \t\t\t"<<p<<endl;
// addHead(l,p);
addTail(l,p);
c=getch();
cout<<endl;
}while(c!=27);
}
void xem(LIST l){
NODE* pTam=l.pHead;
cout<<"phead \t \t \t"<<l.pHead<<endl;
cout<<"l.head->next \t\t"<<l.pHead->pNext;
cout<<endl;
while(pTam!=NULL){
cout<<pTam->data<<"\t";
pTam=pTam->pNext;
}
}
void xoaCuoi(LIST &l){
NODE *p=l.pHead;
while(p->pNext->pNext!=NULL){
p=p->pNext;
}
delete p->pNext;
p->pNext=NULL;
}
main(){
LIST l;
NODE *p;
init(l);
insert(l,p);
xoaCuoi(l);
// xemdc(l);
xem(l);
}

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