Danh sách liên kết đơn với số nguyên

mn cop bài này về rồi chạy giúp mk với. mk k hiểu vì sao nó cứ báo rỗng

#include<iostream>
#include<string>
using namespace std;
struct Node{
	int data;
	struct Node *next;
}*start;
void Init(Node *p){
	start=NULL;
}
Node *getnode(int x){
	Node *p=new Node;
	p->data=x;
	p->next=NULL;
	return p;
}
void input(Node *p,int x){
	int n;
	Node *temp,*s;
	s=start;
	cout<<"\n Nhap so node: "; 	cin>>n;
	for(int i=1;i<=n;i++){
		cout<<"Nhap gia tri: ";	cin>>x;
		temp=new Node;
		temp->data=x;
		temp->next= NULL;
	}
}
void hienthi(Node *p){
	Node *temp;
	if(start==NULL){
		cout<<"\n danh sach rong.";
	}
	temp=start;
	cout<<"Danh sach: ";
	while(start!=NULL ){
		temp=start;
		cout<<temp->data <<"   -> ";
		temp=temp->next;
	}
	cout<<" NULL"<<endl;
}
int main(){
	Node *p;
	int x;
	Init(p);
	input(p,x);
	hienthi(p);
}

từ đầu đến cuối bạn không thay đổi start nên start luôn là NULL. Vậy là cứ báo rỗng thôi.


Mình sửa vòng for hàm input thế này:

	for(int i=1;i<=n;i++){
		cout<<"Nhap gia tri: ";	cin>>x;
		temp=new Node;
		temp->data=x;
		temp->next= start;
		start = temp;
	}

rồi hàm hiển thị bị lặp vô hạn nếu có nhiều hơn 1 phần tử. mình sửa vòng while thế này:

	while(temp!=NULL ){
		cout<<temp->data <<"   -> ";
		temp=temp->next;
	}

cảm ơn bạn nhiều…:blush::blush:

nhưng làm sao để hiển thị ngươc lại hả b.

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