0% found this document useful (0 votes)
27 views1 page

Duyệt Linked-list C++

Uploaded by

Chu Văn Nam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

Duyệt Linked-list C++

Uploaded by

Chu Văn Nam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<iostream>

using namespace std;


struct Node
{
int data;
Node* next;
};
struct List
{
Node* head;
Node* tail;
};
void Khoitao(List& l) // khoi tao List rong
{
l.head = l.tail = NULL;
}
Node* CreateNode(int x) // tao thong tin cho Node
{
Node* p = new Node();
if (p == NULL) exit(1);
p->next = NULL;
p->data = x;
return p;
}
void ThemVaodau(List& l, Node* p)
{
if (l.head == NULL) l.head = l.tail = p;
else
{
p->next = l.head; //cho con tro cua node can them la node p lien ket den
node head
l.head = p; // cap nhat lai node dau la p
}
}
void ThemVaocuoi(List& l, Node* p)
{
if (l.head == NULL) l.head = l.tail = p;
else
{
l.tail->next = p; //cho con tro cua tail lien ket voi node p
l.tail = p; //cap nhat p la node tail
}
}

int main()

List l;
Khoitao(l); //khoi tao danh sach lien ket don
int n;
cout << "Nhap so luong node can them: ";
cin >> n;
for (int i = 0; i <n; i++)
{
int x;
cout << "Nhap gia tri so nguyen: ";
cin >> x;
Node* p = CreateNode(x); // create node so nguyen
ThemVaodau(l, p); //them node p vao dau danh sach lien ket don
}
return 0;
}

You might also like