fork download
  1. #include <bits/stdc++.h>
  2.  
  3. /* cai dat danh sach bang mang*/
  4.  
  5. #define MAXLEN 6
  6.  
  7. typedef struct LIST
  8. {
  9. int elements[MAXLEN];
  10. int last; // vi tri cua phan tu cuoi cung
  11. }list_type;
  12. list_type L;
  13. // initialization
  14. void initialization() {
  15. // ban dau danh sach co 1 phan tu 0 o dau tien
  16. L.last = 0;
  17. for(int i = 0 ; i < MAXLEN ; i++) {
  18. L.elements[i] = 0;
  19. }
  20. }
  21. // insert value x into index p
  22. void insertion(int x, int p) {
  23. if(L.last >= MAXLEN - 1) {
  24. printf("array overflow\n");
  25. // return; // de ham ket thuc "dot tu"
  26. } else {
  27. if(p > L.last || p < 0) {
  28. printf("index not valid\n");
  29. // return;
  30. } else {
  31. L.last++;
  32. for(int q = L.last; q > p ; q--) {
  33. L.elements[q] = L.elements[q-1];
  34. }
  35. L.elements[p] = x;
  36. }
  37. }
  38. }
  39. // delete
  40. // vi tri p
  41. void deletion(int p) {
  42. if( p >= MAXLEN || p > L.last || p < 0) {
  43. printf("index is not valid\n");
  44. } else {
  45. for(int q = p ; q <= L.last - 1 ; q++) {
  46. L.elements[q] = L.elements[q+1];
  47. }
  48. L.last--;
  49. }
  50. }
  51.  
  52. // display
  53. void display() {
  54. for(int i = 0 ; i <= L.last ; i++) {
  55. printf("%d ",L.elements[i]);
  56. }
  57. printf("\n");
  58. }
  59.  
  60. int location(int value) {
  61. for(int i = 0 ; i <= L.last ; i++) {
  62. if( L.elements[i] == value) return i;
  63. }
  64. return -1;
  65. }
  66.  
  67. int main(int argc, char const *argv[])
  68. {
  69. initialization();
  70. insertion(1,0);
  71. insertion(2,0);
  72. insertion(3,0);
  73. insertion(4,0);
  74. insertion(5,0);
  75. display();
  76. // deletion(5);
  77. display();
  78. printf("%d\n",location(-1));
  79. return 0;
  80. }
Success #stdin #stdout 0s 4356KB
stdin
Standard input is empty
stdout
5 4 3 2 1 0 
5 4 3 2 1 0 
-1