0% found this document useful (0 votes)
9 views3 pages

Lien Ket Don May Tinh

The document is a C++ program that defines a linked list structure to manage computer information, including attributes like ID, name, manufacturer, and price. It includes functions for initializing the list, adding nodes, displaying the list, searching for a computer by name, sorting by price, and deleting the first node. The main function allows user input for the number of computers and performs various operations on the list.

Uploaded by

tthanhlam728
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)
9 views3 pages

Lien Ket Don May Tinh

The document is a C++ program that defines a linked list structure to manage computer information, including attributes like ID, name, manufacturer, and price. It includes functions for initializing the list, adding nodes, displaying the list, searching for a computer by name, sorting by price, and deleting the first node. The main function allows user input for the number of computers and performs various operations on the list.

Uploaded by

tthanhlam728
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/ 3

#include<iostream>

#include<string>
using namespace std;
struct Maytinh{
int ma;
string ten;
string nhasanxuat;
float gia;
};
struct node{
Maytinh data;
node*next;
};
struct list{
node*head;
node*tail;
};
// khoi tao danh sach
void danhsach(list&l){
node*head=NULL;
node*tail=NULL;
}
// khoi tao node
node*ktaonode(Maytinh mt){
node*p=new node;
if(p==NULL){
cout<<"Cap phat loi bo nho "<<endl;
return NULL;
}else{
p->data=mt;
p->next=NULL;
return p;
}
}
// them dau
void themdau(list&l,node*p){
if(l.head==NULL){
l.head=l.tail=p;
}else{
p->next=l.head;
l.head=p;
}
}
// them cuoi
void themcuoi(list&l,node*p){
if(l.head==NULL){
l.head=l.tail=p;
}else{
l.tail->next=p;
l.tail=p;
}

}
// duyet danh sach
void nhap(Maytinh&mt){
cout<<"Ma may tinh: ";cin>>mt.ma;
cout<<"Ten may tinh: ";cin>>mt.ten;
cout<<"Nha san xuat: ";cin>>mt.nhasanxuat;
cout<<"Gia: ";cin>>mt.gia;
}
void nhapsv(list&l,Maytinh&mt){
nhap(mt);
node*p=ktaonode(mt);
themcuoi(l,p);
}
void xuat(list l,Maytinh mt){
int i=0;
for(node*p=l.head;p!=NULL;p=p->next){
cout<<"\n Ma may tinh: "<<p->data.ma;
cout<<"\n Ten may tinh: "<<p->data.ten;
cout<<"\n Nha san xuat: "<<p->data.nhasanxuat;
cout<<"\n Gia: "<<p->data.gia;
}
}
// liet ke
void lietke(list l,Maytinh mt){
for(node*p=l.head;p!=NULL;p=p->next){
if(p->data.gia<=12){
cout<<"\n Ma may tinh: "<<p->data.ma;
cout<<"\n Ten may tinh: "<<p->data.ten;
cout<<"\n Nha san xuat: "<<p->data.nhasanxuat;
cout<<"\n Gia: "<<p->data.gia;
}
}

}
//tim kiem
int timkiem(list l,Maytinh mt){
cout<<"Nhap ten ban muon tim kiem ";
string tentk;
cin>>tentk;
for(node*p=l.head;p!=NULL;p=p->next){
if(p->data.ten==tentk){
return 1;
}
}
}
//sap xep;giagiamdan<;tangdan>
void sapxep(list l,Maytinh mt){
for(node*p=l.head;p!=NULL;p=p->next){
for(node*k=p->next;k!=NULL;k=k->next){
if(p->data.gia<k->data.gia){
swap(p->data,k->data);
}
}
}
xuat(l,mt);
}
//xoa dau
void xoadau(list&l,Maytinh mt){
if(l.head==NULL)return;
node*temp=l.head;
if(l.head==l.tail){
l.head=NULL;
l.tail=NULL;
}else{
l.head=l.head->next;
}
delete temp;
}
int main(){
list(l);
node*p;
danhsach(l);
Maytinh mt;
int n;
cout<<"Nhap vao so luong may tinh n= ";
cin>>n;
for(int i=0;i<n;i++){
void nhapsv(list&l,Maytinh&mt);
}
cout<<"\n Danh sach may tinh vua nhap:\n";
void xuat(list&l,Maytinh&mt);
cout<<"\n Danh sach may tinh co gia <= 12:\n";
void lietke(list&l,Maytinh&mt);
cout<<"\n Danh sach may tinh sau khi xoa dau:\n";
void xoadau(list&l,Maytinh&mt);
return 0;
}

You might also like