Thắc mắc về Linked List Dev C++

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct dslk
{
	int data;
	dslk *link;
};
//struct dslk *head;

void taorong (dslk *head)
{
	head=NULL;
}
void chendau (dslk *head, int x)
{
	dslk *p=new dslk;
	p->link=head;
	head=p;
	p->data=x;
}
void nhap (dslk *head)
{
	int n,x;
	cout<<"ban can nhap bao nhieu phan tu ";cin>>n;
	for(int i=1;i<=n;i++)
	{
		cout<<"nhap phan tu thu "<<i<<": ";cin>>x;
		chendau(head,x);
	}
}
void xuat (dslk *head)
{	dslk *p=head;
	while(p!=NULL)
		
		{
		cout<<p->data;
		p=p->link;}
}
int main ()
{
	
	dslk *head;
	taorong(head);
	nhap(head);
	xuat(head);
}

Trong Dev C++ link list chạy được nhưng chỉ nhập được mà xuất không được! tại code hay là tại dev c++ nó không hiểu
mong mọi người giúp giùm mình với

Trình biên dịch thì làm sao mà nó không hiểu được hả bạn :sweat_smile:

2 Likes

nó chạy cho nhập bình thường nhưng tới xuât thì bị lỗi

1 Like

Mình đang debug cho bạn để tìm lỗi, bạn chờ tí nhé

1 Like

cám ơn bạn nhiều nhé

1 Like

This post was flagged by the community and is temporarily hidden.

1 Like

đúng rồi, mình debug thấy sau khi hàm taorong() thì giá trị head không thay đôir

2 Likes

vậy minh nên sửa chỗ nào hả bạn

1 Like

This post was flagged by the community and is temporarily hidden.

2 Likes

This post was flagged by the community and is temporarily hidden.

2 Likes

chạy chưa được bạn ơi

1 Like

This post was flagged by the community and is temporarily hidden.

1 Like

tham chiếu là thay đổi
còn tham trị là không thay đổi đúng không

1 Like

máy mình debug không được là sao vậy ta

1 Like

chỉ dùm mình tí đi bạn code tí id mà xin bạn áh

1 Like

mình tìm không ra mới lên đây cầu cứu nè
chứ tìm ra thì lên đây hỏi làm gì

1 Like

Bạn nên tạo 1 cấu trúc danh sách gồm có đầu và đuôi nhé cho dễ nhìn, bạn đọc code này để nghiên cuuws đi:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

typedef struct node
{
	int data;
	struct node *link;
}Node;
//struct dslk *head;

typedef struct list
{
    Node *head;
    Node *tail;
}List;

void taorong (List *l)
{
	l->head=l->tail=NULL;
}

Node* get_node(int x)
{
    Node *p=new Node;
    p->data=x;
    p->link=NULL;

    return p;
}

void chendau (List *l, int x)
{
    if(l->head==NULL)
    {
        Node *p=get_node(x);
        l->head=l->tail=p;
    }
    else
    {
        Node *p=get_node(x);
        p->link=l->head;
        l->head=p;
    }
}
void nhap (List *l)
{
	int n,x;
	cout<<"ban can nhap bao nhieu phan tu ";cin>>n;
	for(int i=1;i<=n;i++)
	{
		cout<<"nhap phan tu thu "<<i<<": ";cin>>x;
		chendau(l,x);
	}
}
void xuat (List *l)
{
    Node *i=l->head;

    for(;i!=NULL;i=i->link)
        cout<<i->data<<endl;

}
int main ()
{

	List int_list;
	taorong(&int_list);
	nhap(&int_list);
	xuat(&int_list);
}

Bạn xem video về linked list của anh Sơn Đẹp Trai đề hiểu về nó né: https://www.youtube.com/watch?v=Q4Q7WNb8nW8

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