Gap Buffer | Deletion Operation
We have discussed Gap buffer and insert operations. In this post, delete operation is discussed. When we want to delete a character these three cases may arise.
- Characters to be deleted is at cursor position: Let’s assume we want to delete “FOR” from “FORGEEKS”, since the cursor (gap_left) is at desired position the character is deleted i.e. the gap takes the character inside itself and the character gets deleted.
- Characters to be deleted is left of cursor position: For this case since “FOR” is left to the cursor position we must reach to the desired position by using left() as in previous article. Now, we can delete “FOR” as in case 1.
- Characters to be deleted is right of cursor position: For this case also the cursor position is right to the desired position we must reach there by using right() as in previous article. Now, we can delete “FOR” as in case 1.
Implementing Gap Buffer with Deletion
// C++ program of implementation // of gap buffer with Deletion #include <bits/stdc++.h> using namespace std; char buffer[50]; int gap_size = 10; int gap_left = 0; int gap_right = gap_size - gap_left - 1; int size = 10; // Function that is used to grow the gap // at index position and return the array void grow(int k, int position) { char a[size]; // Copy characters of buffer to a[] // after position for (int i = position; i < size; i++) { a[i - position] = buffer[i]; } // Insert a gap of k from index position // gap is being represented by '-' for (int i = 0; i < k; i++) { buffer[i + position] = '_'; } // Reinsert the remaining array for (int i = 0; i < position + k; i++) { buffer[position + k + i] = a[i]; } size += k; gap_right += k; } // Function that is used to move the gap // left in the array void left(int position) { // Move the gap left character by character // and the buffers while (position < gap_left) { gap_left--; gap_right--; buffer[gap_right + 1] = buffer[gap_left]; buffer[gap_left] = '_'; } } // Function that is used to move the gap // right in the array void right(int position) { // Move the gap right character by character // and the buffers while (position > gap_left) { gap_left++; gap_right++; buffer[gap_left - 1] = buffer[gap_right]; buffer[gap_right] = '_'; } } // Function to control the movement of gap // by checking its position to the point of // insertion void move_cursor(int position) { if (position < gap_left) { left(position); } else { right(position); } } // Function to insert the string to the buffer // at point position void insert(string input, int position) { int len = input.length(); int i = 0; // If the point is not the gap check // and move the cursor to that point if (position != gap_left) { move_cursor(position); } // Insert characters one by one while (i < len) { // If the gap is empty grow the size if (gap_right == gap_left) { int k = 10; grow(k, position); } // Insert the character in the gap and // move the gap buffer[gap_left] = input[i]; gap_left++; i++; position++; } } // Function to delete the character buffer // at point position void deleetion(int position) { // If the point is not the gap check // and move the cursor to that point if (position + 1 != gap_left) { move_cursor(position + 1); } // Reduce the gap_left gap_left -= 1; buffer[gap_left] = '_'; } // Driver code int main() { // Initializing the gap buffer with size 10 for (int i = 0; i < 10; i++) { buffer[i] = '_'; } cout << "Initializing the gap buffer " << "with size 10" << endl; for (int i = 0; i < size; i++) { cout << buffer[i] << " "; } cout << endl; // Inserting a string to buffer string input = "GEEKSGEEKS"; int position = 0; insert(input, position); cout << endl; cout << "Inserting a string to buffer" << ": GEEKSGEEKS" << endl; cout << "Output: "; for (int i = 0; i < size; i++) { cout << buffer[i] << " "; } input = "FOR"; position = 5; // Inserting a string to buffer insert(input, position); cout << endl; cout << endl; cout << "Inserting a string to buffer" << ": FOR" << endl; cout << "Output: "; for (int i = 0; i < size; i++) { cout << buffer[i] << " "; } position = 5; // Deletion a character from buffer deleetion(position); cout << endl; cout << endl; cout << "Deleting character at position 5" << endl; cout << "Output: "; for (int i = 0; i < size; i++) { cout << buffer[i] << " "; } position = 6; // Deletion a character from buffer deleetion(position); cout << endl; cout << endl; cout << "Deleting character at position 6" << endl; cout << "Output: "; for (int i = 0; i < size; i++) { cout << buffer[i] << " "; } position = 5; // Deletion a character from buffer deleetion(position); cout << endl; cout << endl; cout << "Deleting character at position 5" << endl; cout << "Output: "; for (int i = 0; i < size; i++) { cout << buffer[i] << " "; } input = "HELLO"; position = 0; // Inserting a string to buffer insert(input, position); cout << endl; cout << endl; cout << "Inserting a string to buffer" << ": HELLO" << endl; cout << "Output: "; for (int i = 0; i < size; i++) { cout << buffer[i] << " "; } return 0; } |
chevron_right
filter_none
Output:
Initializing the gap buffer with size 10 _ _ _ _ _ _ _ _ _ _ Inserting a string to buffer: GEEKSGEEKS Output: G E E K S G E E K S _ _ _ _ _ _ _ _ _ _ Inserting a string to buffer: FOR Output: G E E K S F O R _ _ _ _ _ _ _ G E E K S Deleting character at position 5 Output: G E E K S _ _ _ _ _ _ _ _ O R G E E K S Deleting character at position 6 Output: G E E K S O _ _ _ _ _ _ _ _ _ G E E K S Deleting character at position 5 Output: G E E K S _ _ _ _ _ _ _ _ _ _ G E E K S Inserting a string to buffer: HELLO Output: H E L L O _ _ _ _ _ G E E K S G E E K S
Recommended Posts:
- AVL Tree | Set 2 (Deletion)
- Insert Operation in B-Tree
- Delete Operation in B-Tree
- Skip List | Set 3 (Searching and Deletion)
- Ternary Search Tree (Deletion)
- Find the number of different numbers in the array after applying the given operation q times
- Van Emde Boas Tree | Set 4 | Deletion
- Gap Buffer Data Structure
- Proto Van Emde Boas Trees | Set 4 | Deletion
- Number of subarrays with GCD = 1 | Segment tree
- Smallest subarray with GCD as 1 | Segment Tree
- Range maximum query using Sparse Table
- Number of triplets in array having subarray xor equal
- Maximise the number of toys that can be purchased with amount K using min Heap
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.






