Con trỏ co ban part 9( Thêm chèn xóa mãng bằng con trỏ)

#include<iostream> using namespace std; #include<conio.h> #include<stdio.h> #include<stdlib.h> inline void nhapmang(int *a, int n) { for(int i=0;i<n;i++) { cout<<"nhapn vao a["<<i<<"] = ";cin>>*(a++); } } inline void xuatmang(int *a,int n) { for( int i=0;i<n;i++) { cout<<*(a++)<<" ";// có thể dùng cách này } } inline void themmang(int *a,int &n, int vt,int pt)// thuong luc nay phai co tham chieu vao nhung realloc ban chat da co tham thieu roi nen co & hay ko & cung ko sao { realloc(a,(n+1)*sizeof(int *) );// gia tang bo nho them 1 o; for(int i=n;i>=vt+1;i--) { a[i]=a[i-1]; } a[vt]=pt; n++; xuatmang(a,n); } inline void xoaphantu(int *a,int &n,int vt) { for(int i=vt;i<n-1;i++) { a[i]=a[i+1]; } n--; realloc(a,n*sizeof(int *)); } int main() { int n; do{ cout<<"nhap vao so luong phan tu trong mang";cin>>n; if(n<0) { cout<<"\nso luon phan tu ban nhap bi sai moi kiem tra lai ! "; } }while(n<0); int *a=new int[n]; nhapmang(a,n); cout<<endl; xuatmang(a,n); int vt,pt; cout<<"\nnhap vao vi tri can them ";cin>>vt; cout<<"\n nhap vao phan tu can them";cin>>pt; cout<<"\n mang sau khi them la :\n"; themmang(a,n,vt,pt); cout<<"\nnhap vi tri can xoa ";cin>>vt; cout<<"\nmang sau khi xoa"<<"tai vi tri "<<vt<<" la:\n"; xoaphantu(a,n,vt); xuatmang(a,n); cout<<"gia tri o cuoi cung chua duoc xoa la"<<n; delete[] a; getch(); 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.