📜  间隙缓冲|删除操作

📅  最后修改于: 2021-04-17 11:25:20             🧑  作者: Mango

我们已经讨论了间隙缓冲区和插入操作。在这篇文章中,讨论了删除操作。当我们想删除一个字符,可能会出现这三种情况。

  • 要删除的字符位于光标位置:假设我们要从“ FORGEEKS”中删除“ FOR”,因为光标(gap_left)位于所需的位置,因此该字符被删除,即,空格将其内部的字符带走并且该字符被删除。

  • 要删除的字符留在光标位置的左边:在这种情况下,由于“ FOR”留在了光标位置,因此我们必须像上一篇文章一样使用left()到达所需的位置。现在,我们可以像情况1一样删除“ FOR”。

  • 要删除的字符是正确的光标位置:对于这种情况也光标位置是正确的理想的位置,我们必须使用正确的()作为以前的文章中到达那里。现在,我们可以像情况1一样删除“ FOR”。

通过删除实现间隙缓冲区

// C++ program of implementation
// of gap buffer with Deletion
  
#include 
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;
}
输出:
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