Untitled
unknown
c_cpp
3 years ago
1.3 kB
11
Indexable
#include<iostream> using namespace std; struct Node { int info; struct Node *Next; }; typedef struct Node NODE; NODE *KhoiTaoNODE(int x) { NODE *p= new NODE; p->info=x; p->Next=NULL; return p; } struct list { NODE *Head; }; typedef struct list LIST; void KhoiTao(list &l) { l.Head=NULL; } void ThemVaoDau(LIST &l, NODE *p) { if(l.Head==NULL) l.Head=p; else { p->Next=l.Head; l.Head=p; } } void XuatDanhSach(LIST l) { for(NODE *i=l.Head; i != NULL; i= i->Next) { cout << i->info << "\t"; } } int Tongdanhsach(LIST l) { int sum = 0; for(NODE *i=l.Head; i != NULL; i= i->Next) { sum += i->info; } return sum; } void Sapxep(LIST l) { for(NODE *i=l.Head; i->Next != NULL; i= i->Next) { for (NODE *j=i->Next; j != NULL; j=j->Next) { if (j->info < i->info) swap (j->info,i->info); } } } int main() { LIST l; KhoiTao(l); while (true) { int x; cin >> x; if (x <= 0) break; NODE *p=KhoiTaoNODE(x); ThemVaoDau(l,p); } //XuatDanhSach(l); // Xuat danh sach cout <<"Tong gia tri danh sach = " << Tongdanhsach(l) << endl; // Tinh tong danh sach Sapxep(l); //Sap xep thu tu tang return 0; }
Editor is loading...