30/09/2018, 16:45
Lỗi Danh sách liên kết đơn trong c++
<#include<iostream>
using namespace std;
struct tree{
int item;
struct tree *next;
};
tree *creat_tree(int );
void insert_top(tree *,int);
void show(tree *);
tree *creat_tree(int x){
tree *p;
p=new tree;
p->item=x;
p->next=NULL;
return p;
}
void insert_top(tree *p,int x){
tree *q;
q=creat_tree(x);
q->next=p;
p=q;
return;
}
void insert_bottom(tree *p,int x){
tree *q,*r;
q=creat_tree(x);
r=p;
while(r->next!=NULL){
r=r->next;
}
r->next=q;
}
void show(tree *p){
tree *q;
q=new tree;
q=p;
while(q->next!=NULL){
cout<<q->item;
q=q->next;
}
}
main(){
tree *p;
p=creat_tree(0);
insert_top(p,1);
insert_top(p,2);
insert_bottom(p,3);
show(p);
}
/>
Bài liên quan





lỗi là khi chèn thêm phần tử vào danh sách nhưng nó không xuất ra màn hình
Mình code bằng
C, bạn tham khảo thử.mình đang dùng danh sách liên kết còn code của bạn dùng hàng đợi rồi
while(p!=NULL) - hàm show nhé
không biết bạn có insert được không, nhưng mình nhớ là cái này không được. Cái p ở trong hàm insert_top khác vs p ở hàm main
Code sửa lại : http://ideone.com/y5GIhJ
tại sao hàm insert_bottom không có cái (tree *&p,int x) ?
Cả
(tree *&p,int x)vàinsert_bottom(tree *p,int x)đều dc. Vìpcũng không thay đổigiúp mình fix bài này với…chạy tới hàm output là bị lỗi