#include<iostream>
using namespace std;
// khai báo 1 node
struct node
{
int data;
struct node *pnext;
};
//kh?i t?o 1 node
node* getnode(int x)/// x là d? li?u c?n ??a vào
{
node *p = new node;
if (p == NULL)
{
return NULL;
}
else
{
p->data = x;
p->pnext = NULL;
return p;
}
}
/// khai báo 1 list
struct list
{
node *phead;
node *ptail;
};
// kh?i t?o 1 node
void init(list &l)
{
l.phead = l.ptail = NULL;
}
/// thêm vào ??u danh sách
void addhead(list &l,node *p )
{
if (l.phead == NULL)
{
l.phead = l.ptail = p;
}
else
{
p->pnext = l.phead;
l.phead = p;
}
}
// thêm vào cu?i danh sách
void addtail(list &l, node *p)
{
if (l.phead == NULL)
{
l.phead = l.ptail = p;
}
else
{
l.ptail->pnext = p;
l.ptail = p;
}
}
// nh?p danh sách nek2
void input(list &l, int n)
{
/// ??u tiên mình ph?i kh?i t?o 1 danh sách
init(l);// kh?i t?o danh sách r?ng r?i m?i thêm vô
for (int i = 1; i <= n; i++)
{
int x;
cout << "\nNhap du lieu cho 1 node :"; cin >> x;
node *p = getnode(x);
addtail(l, p);
}
}
void output(list &l)
{
for (node *p = l.phead; p!=NULL; p = p->pnext)
{
cout << p->data<<" ";
}
}
// bây gi? ta tìm max cho danh sach b?n nhé
int tim_mad(list l)
{
int k=0;
for (node *p = l.phead; p; p = p->pnext)
{
if (p->data > k)
{
k = p->data;
}
}
return k;
}
// bây gi? thì x?p x?p l?i danh sach b?n nhe
void hoanvi(int &a, int &b)
{
int item = a;
a = b;
b = item;
}
// hoán v? danh sách b?n nhe
void hoanvilist(list &l)
{
for (node *p = l.phead; p != l.ptail; p = p->pnext)
{
for (node *k = p->pnext; k!=NULL; k = k->pnext)
{
if (p->data > k->data)
{
hoanvi(p->data, k->data);
}
}
}
}
// tính trung bình c?ng c?a 1 danh sách b?n nhé
int medium(list l,int n)
{
int diem = 0;
for (node *p = l.phead; p; p = p->pnext)
{
diem+=p->data;
}
return diem / n;
}
// thêm node vào sau node q
void themsau1node(list &l, node *p, node *q)
{
for (node *k = l.phead; k; k= k->pnext)
{
if (k->data == q->data)
{
node *g = k->pnext;
k->pnext = p;
p->pnext = g;
return;
}
}
}
// xóa ??u
void deletehead(list &l)
{
if (l.phead == NULL)
{
return;
}
else
{
node *g = l.phead;
l.phead = l.phead->pnext;
delete g;
}
}
// xóa cu?i
void deletetail(list &l)
{
node *k=new node;
for (node *p = l.phead; p!=NULL; p = p->pnext)
{
if (p == l.ptail)
{
l.ptail=k;
l.ptail->pnext = NULL;
delete p;
//break;
return;
}
k = p;
}
}
void giaiphong(list &l)
{
node *p;
while (l.phead!=NULL)
{
p = l.phead;
l.phead = l.phead->pnext;
delete p;
}
}
// xóa sau 1 node
void xoasau1node(list &l, node *p)
{
for (node *k = l.phead; k; k = k->pnext)
{
if (k->data == p->data)
{
node *g = k->pnext;
k->pnext = g->pnext;
delete g;
return;
}
}
}
// xoa 1 khóa k b?t k?
void xoabatky(list &l, int a)
{
node *g = new node;;
for (node *p = l.phead; p; p = p->pnext)
{
if (p->data == a)
{
//g = getnode(a);
xoasau1node(l, g);
return;
}
g = p;
}
}
int main()
{
int n;
list l;
cout << "\nNhap so luong cho danh sach: "; cin >> n;
input(l, n);
output(l);
cout << "\nSo max trong danh sach nay la: " << tim_mad(l);
cout << "\nXap xep danh sach lai ban nhe: ";
hoanvilist(l);
output(l);
cout << "\ntrung binh cong cua danh sach se la: "<<medium(l,n);
//------------------------------------------------------------//
// thêm 1 node vào sau 1 node
cout << "\n//------------------------------------------------------------//";
cout << "\n// thêm 1 node vào sau 1 node ";
int x1, x2;
cout << "\nNhap vitri can them sau :"; cin >> x1;
cout << "\nNhap phan tu can them vao: "; cin >> x2;
node *p1 = getnode(x1);
node *p2 = getnode(x2);
themsau1node(l, p2, p1);
output(l);
//////////////////////////////
cout << "\nXoa dau :";
deletehead(l);
output(l);
/////////////////////////////
cout << "\nXoa cuoi ne";
deletetail(l);
output(l);
//////////////////////////////
// xóa sau 1 node
node *chuc = getnode(2);// xóa sau s? 3 nhé b?n
cout << "\nXoa sau 1 node: nek";
xoasau1node(l, chuc);
output(l);
////////////////////////////////////
/// xóa b?t k? nek
cout << "\nXoa bat ky nek: ";
xoabatky(l,3);
output(l);
cout << "\nGiai phong list :";
giaiphong(l);
output(l);
system("pause");
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.