Hello I am trying to make a notepad through linked list.
Following is my code (this is a 4D Linked list however, for a start, I am just implementing a Doubly Linked List)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
struct node
{
char data;
node* right;
node* left;
node* up;
node* down;
};
class List
{
node* head, * tail;
public:
List();
void Create_Node(char);
void Delete_Node();
void Display_List();
};
List::List()
{
head = NULL;
tail = NULL;
}
void List::Create_Node(char data)
{
node* new_node = new node;
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
new_node->up = NULL;
new_node->down = NULL;
if (head == NULL)
{
head = new_node;
tail = new_node;
return;
}
tail->right = new_node;
new_node->left = tail;
tail = new_node;
}
void List::Delete_Node()
{
if (tail == NULL)
return;
node* temp = tail->left;
temp->right = NULL;
free(tail);
tail = temp;
}
void List::Display_List()
{
node* temp = head;
while (temp != NULL)
{
cout << temp->data;
temp = temp->right;
}
}
int main()
{
char input;
List obj;
bool fh = true;
while (fh)
{
input = _getch();
if (input == 8 || input == 127)
{
obj.Delete_Node();
}
system("cls");
obj.Create_Node(input);
obj.Display_List();
if (input == '~')
fh = false;
}
cout << endl;
return 0;
}
|
Now, what I am trying to do here is to remove the actual functionality of backspace. For example, here's one outpout of mine,
My name is this
Now, If I press backspace, it does remove "s" character but it goes back to the character "i" and when I type the next character, it replaces the character 'i' which is not what I am intending to do.
All I want is it to only remove "s" and then add characters after it.
Moreover, I can only remove backspace once apparently. If after removing 's', I again press backspace, the mouse stays at the 'i' character like it is in some infinite while loop, and I am unable to find the reasoning behind this as well. If I, however, press some keys then the backspace does work agaie (but only once like before)
I think this would also be fixed if I somehow manage to remove the normal functionality of backspace.
I want my program to properly handle the backspace (that is, remove the last character and don't replace the second last character, PLUS allow removing more than one characters without having to add another character first)
(Also note that I have tried using the following code as well however it hasn't helped either)
can someone help me in this regard? Thank you!