Bài đăng

danh sách liên kết đơn***

#include<iostream> #include <string> #include <iomanip> using namespace std; struct sinhvien{ string ten; int tuoi; string diachi; }; struct Node{ sinhvien * data; Node * next ; }; struct Danhsach{ Node * dau; Node * cuoi; }; Danhsach * ds; void khoitao(){ ds = new Danhsach(); ds -> dau = ds -> cuoi = NULL; } void ktrarong(){ if (ds -> dau = ds -> cuoi = NULL) cout << "danh sach nay rong " ; else cout << "k rong " ; } void themcuoi(Node * node){ if (ds -> dau == NULL){ ds -> dau = ds -> cuoi = node; } else { ds -> cuoi -> next = node; ds -> cuoi = node; } } Node * getNode(string ten, int tuoi, string diachi){ Node * p = new Node(); p -> data = new sinhvien(); p -> data -> ten = ten; p -> data -> tuoi = tuoi; p -> data -> diachi = diachi; p -> next = NULL; return p; } void input (){ int n; co

tổng hợp sxep

/* Sap xep tang dan danh sach ke theo giai thuat selection sort, bubblesort, quicksort, insertion sort, heapsort, mergesort */ // Danh sach cai dat bang mang tinh . #include <stdio.h> #include <conio.h> #include <iostream> using namespace std; #define true 1 #define false 0 //================================================== void swap( float & x, float & y) { float tmp = x;x = y;y = tmp; }; //================================================== // Nhap danh sach . void nhap( float a[], int & N) {cout << " \n So phan tu can nhap N = " ;cin >> N; for ( int i = 0 ;i < N;i ++ ) { cout << "a[" << i << "]= " ; cin >> a[i];} } //================================================== // Hien danh sach tren man hinh . void xem( float a[], int N) { int i; cout << " \n Cac phan tu danh sach:" ; for (i = 0 ;i < N;i ++ ) cout << a[i] <

sắp xếp nổi bọt

Đề bài: Đoc ghi trên file Sắp xếp theo thuật toán nổi bọt #include<iostream> #include<fstream> using namespace std; bool doctep( int a[ 100 ], int & n) { ifstream Doc( "output.txt" ); if (Doc . fail()) { return false; } Doc >> n; for ( int i = 0 ; i < n; i ++ ) { Doc >> a[i]; } return true; } int sapxeptang( int a[ 100 ], int n) { for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( a[i] > a[j]) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } } return 0 ; } int ghi( int a[ 100 ], int n) { ofstream ghi( "sapxeptang.txt" ); ghi << "sap xep tang dan la: " ; for ( int i = 0 ; i < n; i ++ ) { ghi << a[i] << " " ; } return 0 ; } int main() { int a[ 100 ]; int n; if (doctep(a,n)) { sapxeptang(a,n); ghi(a,n); } }

cấu trúc struct

Đề bài: Đọc và ghi ra file Danh sách sinh viên đăng kí học cho từng sinh viên và tính điểm trung bình cho từng sinh viên. // khai bao sinh vien // khai bao mon hoc // khai bao sinh vien hoc mon hoc gi #include<iostream> #include<cstring> #include<string> #include<fstream> #include<iomanip> using namespace std; typedef struct sinhvien{ string ten; string namsinh; }; typedef struct monhoc{ string ten; }; typedef struct dangkyhoc{ sinhvien * sv; monhoc * ds[ 100 ]; int somonhoc; }; typedef struct diem{ int sodiem; monhoc * mh; }; typedef struct bangDiem{ sinhvien * sv; diem * dsDiem[ 100 ]; int doDaiBang; }; bangDiem * dsBangDiem[ 100 ]; int soSv; sinhvien * dssv[ 100 ]; int soMonhoc; monhoc * dsMonhoc[ 100 ]; dangkyhoc * dsDangky[ 100 ]; int vitri; void input (){ fstream f( "dssv.cpp" ); f >> soSv; f . ignore(); for ( int i = 0 ; i < soSv; i ++ ){ dssv[i] = new sinhvien(); // khoi tao doi tu

chèn/xóa vào đầu hoặc cuối / bất kỳ trong danh sách

// A -> B -> C -> D // [ 0 ] -> [ 1 ] -> [ 2 ] -> [ 3 ] #include <iostream> using namespace std; // 2 1 struct Node{ int data; Node * next ; }; Node * dau; Node * cuoi; // chen vao cuoi danh sach lien ket void insertCuoi(Node * node){ cuoi -> next = node; cuoi = node; } void inputChenVaoCuoi(){ // 1 - 10 dau = new Node(); dau -> data = 1 ; dau -> next = NULL; cuoi = dau; for ( int i = 2 ; i <= 10 ; i ++ ){ Node * b = new Node(); b -> data = i; b -> next = NULL; insertCuoi(b); } } void insertDau(Node * node){ // 2 -> 1 // a = b; node -> next = dau; dau = node; } void inputChenVaoDau(){ // 1 - 10 dau = new Node(); dau -> data = 1 ; dau -> next = NULL; cuoi = dau; for ( int i = 2 ; i <= 10 ; i ++ ){ Node * b = new Node(); b -> data = i; b -> next = NULL; insertDau(b); } } // 1 -> 2 -> 4 -> 5 // 3 //

sắp xếp danh sách liên kết đơn

Bài làm: #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

danh sách sinh viên (ds liên kết đơn)

Đề bài: tao menu nhap vao danh sach sinh vien. -> chen vao dau danh sach ->chen vao cuoi danh sach -> chen vao 1 vi tri bat ky -> xoa sinh vien o dau -> xoa sinh vien o cuoi -> xoa sinh vien o 1 vi tri bat ky -> sap xep sinh vien theo ten Bài giải: #include<iostream> #include <string> using namespace std; struct sinhvien{ string ten; }; struct Node{ sinhvien * 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(string x){ Node * p = new Node(); p -> data = new sinhvien(); p -> data -> ten = x; p -> next = NULL; return p; } void input (){ int n; cout << "nhap so sinhvien " ; cin >> n; cin . ignore(); khoitao(); for ( int i = 1 ; i <= n; i ++ ){ string x; cout << "nhap