Advertisement
samu14792

Insert, Delete in Arrays

Feb 22nd, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5.  
  6. struct node{
  7.     int x;
  8. };
  9.  
  10. void print_array(struct node* a, int indx){
  11.     cout<<"Array: ";
  12.     for(int i=0;i<=indx;i++){
  13.         cout<<a[i].x<<" ";
  14.     }
  15.     cout<<endl;//for new line
  16. }
  17.  
  18. void array_insert_last(struct node* a, int indx, int v){
  19.     a[indx].x = v;
  20. }
  21.  
  22. void array_insert_first(struct node* a, int indx, int v){
  23.  
  24.     for(int i=indx;i>0;i--){
  25.         a[i].x = a[i-1].x; // shift every value to the right
  26.     }
  27.     a[0].x = v;
  28. }
  29.  
  30. void array_delete_first(struct node* a, int indx){
  31.     for(int i=0;i<indx;i++){
  32.         a[i].x = a[i+1].x; // shift every value to the left
  33.     }
  34. }
  35.  
  36. int main(){
  37.     struct node* arr;
  38.     int max_item = 10;//static change of max_item
  39.     arr=new node[max_item];
  40.     //arr = (struct node*)malloc(10*sizeof(struct node));
  41.     string s;
  42.     int value;
  43.     int index = -1;
  44.     while(1){
  45.         cin>>s; //s takes input like "insert-first"/"insert-last"/"delete-first"/"delete-last"
  46.         if(s=="insert-last"){
  47.             cin>>value;
  48.             if(index<(max_item-1)){ // check whether you can understand why max_item-1 is used here!!
  49.                 index++;
  50.                 array_insert_last(arr, index, value);
  51.             }
  52.             else{
  53.                 cout<<"Memory overflow!"<<endl; // number of items will be greater than max_item if this insert happens
  54.             }
  55.         }
  56.         else if(s=="insert-first"){
  57.             cin>>value;
  58.             if(index<(max_item-1)){ // check whether you can understand why max_item-1 is used here!!
  59.                 index++;
  60.                 array_insert_first(arr, index, value);
  61.             }
  62.             else{
  63.                 cout<<"Memory overflow!"<<endl; // number of items will be greater than max_item if this insert happens
  64.             }
  65.         }
  66.         else if(s=="delete-last"){
  67.             if(index>-1){
  68.                 index--;
  69.             }
  70.             else{
  71.                 cout<<"Memory underflow!"<<endl; // number of items will be lesser than 0 if this delete happens
  72.             }
  73.         }
  74.         else if(s=="delete-first"){
  75.             if(index>-1){
  76.                 array_delete_first(arr, index);
  77.                 index--; // check why I did this!!
  78.             }
  79.             else{
  80.                 cout<<"Memory underflow!"<<endl; // number of items will be lesser than 0 if this delete happens
  81.             }
  82.         }
  83.         else if(s=="exit"){
  84.             break;
  85.         }
  86.         else{
  87.             cout<<"Operations not identified!"<<endl;
  88.         }
  89.         print_array(arr, index); //for printing the whole array after this operation
  90.     }
  91.  
  92.     return 0;
  93. }
  94. /*
  95. input:
  96. insert-first 1
  97. insert-first 3
  98. insert-last 2
  99. insert-last 4
  100. insert-first 5
  101. insert-last 6
  102. insert-first 8
  103. insert-last 7
  104. insert-first 1
  105. insert-last 9
  106. insert-last 10
  107. insert-first 11
  108. delete-last
  109. delete-last
  110. delete-first
  111. delete-first
  112. delete-first
  113. delete-last
  114. delete-first
  115. delete-last
  116. delete-first
  117. delete-first
  118. delete-last
  119. insert-first 1
  120. exit
  121. */
  122. /*
  123. output:
  124. Array: 1
  125. Array: 3 1
  126. Array: 3 1 2
  127. Array: 3 1 2 4
  128. Array: 5 3 1 2 4
  129. Array: 5 3 1 2 4 6
  130. Array: 8 5 3 1 2 4 6
  131. Array: 8 5 3 1 2 4 6 7
  132. Array: 1 8 5 3 1 2 4 6 7
  133. Array: 1 8 5 3 1 2 4 6 7 9
  134. Memory overflow!
  135. Array: 1 8 5 3 1 2 4 6 7 9
  136. Memory overflow!
  137. Array: 1 8 5 3 1 2 4 6 7 9
  138. Array: 1 8 5 3 1 2 4 6 7
  139. Array: 1 8 5 3 1 2 4 6
  140. Array: 8 5 3 1 2 4 6
  141. Array: 5 3 1 2 4 6
  142. Array: 3 1 2 4 6
  143. Array: 3 1 2 4
  144. Array: 1 2 4
  145. Array: 1 2
  146. Array: 2
  147. Array:
  148. Memory underflow!
  149. Array:
  150. Array: 1
  151. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement