fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. struct tablica
  6. {
  7. int roz;
  8. signed int *tab;
  9. tablica():roz(0),tab(0) {}
  10. void dodaj(int nr,signed int wartosc);
  11. void usun(int nr);
  12. };
  13.  
  14. void tablica::dodaj(int nr,signed int wartosc)
  15. {
  16. signed int *buff=new signed int[roz+1];
  17. memcpy(buff,tab,nr*sizeof(signed int));
  18. buff[nr]=wartosc;
  19. memcpy(buff+nr+1,tab+nr,(roz-nr)*sizeof(signed int));
  20. delete[] tab;
  21. tab=buff;
  22. ++roz;
  23. }
  24.  
  25. void tablica::usun(int nr)
  26. {
  27. signed int *buff=new signed int[roz-1];
  28. memcpy(buff,tab,nr*sizeof(signed int));
  29. memcpy(buff+nr,tab+nr+1,(roz-nr-1)*sizeof(signed int));
  30. delete[] tab;
  31. tab=buff;
  32. --roz;
  33. }
  34.  
  35. int main()
  36. {
  37. tablica T;
  38. T.dodaj(0,3);
  39. for(int i=0;i<T.roz;++i) cout<<T.tab[i]<<' ';
  40. cout<<endl;
  41. T.dodaj(1,5);
  42. T.dodaj(0,1);
  43. for(int i=0;i<T.roz;++i) cout<<T.tab[i]<<' ';
  44. cout<<endl;
  45. T.dodaj(3,6);
  46. T.dodaj(2,4);
  47. T.dodaj(1,2);
  48. T.dodaj(0,0);
  49. for(int i=0;i<T.roz;++i) cout<<T.tab[i]<<' ';
  50. cout<<endl;
  51. T.usun(0);
  52. T.usun(1);
  53. T.usun(2);
  54. T.usun(3);
  55. for(int i=0;i<T.roz;++i) cout<<T.tab[i]<<' ';
  56. cout<<endl;
  57. return 0;
  58. }
Success #stdin #stdout 0s 3428KB
stdin
stdout
3 
1 3 5 
0 1 2 3 4 5 6 
1 3 5