#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;
void
grow(
int
k,
int
position)
{
char
a[size];
for
(
int
i = position; i < size; i++) {
a[i - position] = buffer[i];
}
for
(
int
i = 0; i < k; i++) {
buffer[i + position] =
'_'
;
}
for
(
int
i = 0; i < position + k; i++) {
buffer[position + k + i] = a[i];
}
size += k;
gap_right += k;
}
void
left(
int
position)
{
while
(position < gap_left) {
gap_left--;
gap_right--;
buffer[gap_right + 1] = buffer[gap_left];
buffer[gap_left] =
'_'
;
}
}
void
right(
int
position)
{
while
(position > gap_left) {
gap_left++;
gap_right++;
buffer[gap_left - 1] = buffer[gap_right];
buffer[gap_right] =
'_'
;
}
}
void
move_cursor(
int
position)
{
if
(position < gap_left) {
left(position);
}
else
{
right(position);
}
}
void
insert(string input,
int
position)
{
int
len = input.length();
int
i = 0;
if
(position != gap_left) {
move_cursor(position);
}
while
(i < len) {
if
(gap_right == gap_left) {
int
k = 10;
grow(k, position);
}
buffer[gap_left] = input[i];
gap_left++;
i++;
position++;
}
}
void
deleetion(
int
position)
{
if
(position + 1 != gap_left) {
move_cursor(position + 1);
}
gap_left -= 1;
buffer[gap_left] =
'_'
;
}
int
main()
{
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;
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;
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;
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;
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;
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;
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;
}