In this quiz, you are asked to use linked list to simulate typing a string. You need to implement four operations, i.e., type, move, backspace and print.
Hint: it is easier to use a double linked list to do so.
Notice: <string> is allowed, but other STL are not allowed.
There are four operations to be implemented.
////////you can use the below code/////////
#include <iostream> #include <string> using namespace std; class LinkedList; class ListNode{ public: ListNode():data(' '), next(0), pre(0){}; ListNode(char c):data(c), next(0), pre(0){}; friend class LinkedList; private: char data; ListNode *next; ListNode *pre; }; class LinkedList{ public: LinkedList():head(0), cursor(0){}; void add_node(char c); void move_right(int p); void move_left(int p); void backspace(); void print(); private: ListNode *head; ListNode *cursor; }; void LinkedList::backspace(){ // your implement } void LinkedList::print(){ // your implement } void LinkedList::add_node(char c){ // your implement } void LinkedList::move_right(int p){ // your implement } void LinkedList::move_left(int p){ // your implement } int main(){ LinkedList list; string command; string input; int p; while(cin>>command){ if(command == "Type"){ cin >> input; if(input.empty()){ continue; }else{ for(int i=0; i<input.length(); i++){ list.add_node(input[i]); } } }else if(command == "Move"){ cin >> p; if(p>0){ list.move_right(p); }else if(p<0){ list.move_left(p); }else{ continue; } }else if(command == "Backspace"){ list.backspace(); }else if(command == "Print"){ list.print(); }else{ continue; } } return 0; }
There are N operations specified in the input lines and N<300000.
For every operation “Print”, you need to output all characters stored in the linked list and shift a newline.