- #1
- 2,593
- 5
I'm writing a c++ program which simulates the behaviour of notepad in this sense. Suppose we input (keyboard) 'high<<<<^^^^11'. Expected outout is '11'
Here '<' represents pressing left arrow key once, and ^ represents the delete key. '>' represents the right arrow key. To achieve this I made use of a doubly-linked list, whose implementation was done from scratch and is included within the same driver file.
I've checked the insertion operation and for some reason the program is not able to exit the "while" loop to print the result after processing the input. The "Test" line was included to see if it could exit, but so far it hasn't exited the while loop. I'm wonderin how cin works here.
Here is the code:
Suppose I enter "12345" as the input. After hitting the enter key, nothing happens as though the program is expecting some more input. How do I make it such that it recognises the return key as a input to terminate the while loop?
Here '<' represents pressing left arrow key once, and ^ represents the delete key. '>' represents the right arrow key. To achieve this I made use of a doubly-linked list, whose implementation was done from scratch and is included within the same driver file.
I've checked the insertion operation and for some reason the program is not able to exit the "while" loop to print the result after processing the input. The "Test" line was included to see if it could exit, but so far it hasn't exited the while loop. I'm wonderin how cin works here.
Here is the code:
Code:
#include <iostream>
#include <list>
using namespace std;
class ListNode {
private:
char c;
ListNode *prev;
ListNode *next;
public:
Default argument for *p and *n is NULL
ListNode(char character, ListNode *p = NULL, ListNode *n = NULL) : c(character), prev(p), next(n) {}
// define accessor methods
ListNode* getprev(){
return prev;}
ListNode* getnext(){
return next; }
// define mutator methods
void setprev(ListNode *cur){
prev = cur;}
void setnext(ListNode *cur){
next = cur;}
// method to print the list starting from current node
void printList() {
cout<<c;}
// complete this method
};
int main() {
char curr; //current character from input
ListNode *head = NULL;
ListNode *cursor = NULL;
while (cin>>curr) {
switch (curr) {
case '<' : if(cursor!=head)
cursor = cursor->getprev();
//this checks that the linked list is not
//already at the start before moving
break;
case '>' : if(cursor->getnext()!=NULL)
//This checks the list has entries and that
//cursor is not already at the end before moving
//right
cursor = cursor->getnext();
break;
case '^' : if(cursor->getnext()!=NULL){//over here deletion
//deletes the node after the one pointed to
//by cursor. Note that the cursor does not
//move at all after deletion.
ListNode *delnode = cursor->getnext();
delnode->getnext()->setprev(cursor);
cursor->setnext(delnode->getnext());
delete delnode;
delnode = NULL;}
break;
default : if(head==NULL){ //This part of the code initialises
//the linked list upon input of the first valid
// non space character. When this is executed
// the linked list is no longer empty. The head
// is effectively the dummy node since nothing
// is stored in it
head = new ListNode('\0');
ListNode *newPtr = new ListNode(curr,head,NULL);
//The head's next ptr now points to the first
//valid data node
head->setnext(newPtr);
//Here cursor points to the first valid entry
cursor = newPtr;
newPtr = NULL;
}
else {
//Insertion is done after the node which is
//pointed to by cursor
ListNode *newPtr;
newPtr = new ListNode(curr,cursor,cursor->getnext());
if (cursor->getnext()!=NULL)
//This part assumes that insertion is not done
//at the tail.
{ (newPtr->getnext())->setprev(newPtr); }
cursor->setnext(newPtr);
cursor = newPtr;
newPtr = NULL;}
break;
}
}
cout<<"End of while"<<endl;
//Prints the List
cout<<"Test"<<endl;
ListNode *printnode = head->getnext();
while (printnode!=NULL)
{ printnode->printList();
printnode = printnode->getnext();}
return 0;
}
Suppose I enter "12345" as the input. After hitting the enter key, nothing happens as though the program is expecting some more input. How do I make it such that it recognises the return key as a input to terminate the while loop?