Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct Node{
- int data;
- Node *next;
- };
- Node * dau;
- Node * cuoi;
- void khoitao(){
- dau = cuoi = NULL;
- }
- void themcuoi(Node * node){
- if(dau == NULL){
- dau = cuoi = node;
- }
- else{
- cuoi -> next = node;
- cuoi = node;
- }
- }
- Node *getNode(int x){
- Node *p = new Node();
- p -> data = x;
- p ->next = NULL;
- return p;
- }
- void input(){
- int n;
- cout << "nhap so node ";
- cin >> n;
- khoitao();
- for(int i = 1; i <= n; i++){
- int x;
- cout << "nhap vao data: ";
- cin >> x;
- Node *p = getNode(x);
- themcuoi(p);
- }
- }
- void in(){
- Node *p = dau;
- cout << endl;
- while( p != NULL){
- cout << p -> data << " ";
- p = p -> next;
- }
- }
- void hoanvi(int &a, int &b){
- int tmp = a;
- a = b;
- b = tmp;
- }
- void sapxep(){
- for( Node *p = dau ; p != cuoi; p = p -> next){
- for(Node *q = p ->next; q != NULL; q = q -> next){
- if(p -> data > q -> data){
- hoanvi(p -> data, q -> data);
- }
- }
- }
- }
- int main(){
- input();
- cout << "danh sach lien ket la: ";
- print();
- cout << endl << "danh sach sau sap xep la: ";
- sapxep();
- print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement