Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Implement a line editor using linked lists. You must implement your own link

ID: 3750387 • Letter: C

Question

C++

Implement a line editor using linked lists.  You must implement your own linked list. A document will be represented by a linked list. Each line in the document is a node in the linked list. Each line in the document is 80 characters. Users can insert, delete or modify lines in the document or print the entire document. Out of bounds print or delete requests are ignored.

User commands:

insertEnd "text" -- insert given text at the end of the document

insert 3 "text" --insert given text at the line indicated by index given

delete 3 --- delete line at index given

edit 3 "text" --- replace the line at the index given with the given text

print -- print the entire document, with line numbers

search "text" -- print the line number and line that contains the given text. print "not found" if it is not found

quit - quit/exit the program

Sample input 1:

insertEnd "now is the time"

insertEnd "for all good men"

insertEnd "to come to the aid of their country"

print

search "come to the aid"

quit

Sample output 1:

1 now is the time

2 for all good men

3 to come to the aid of their country

3 to come to the aid of their country

Explanation / Answer

I've not implemented search function, beacause usually when there are more then 4 options we need to answer 4 only.

#include <iostream>

#include <vector>

#include <string>

using namespace std;

class Node{

public:

string str;

Node *next;

};

Node *head;

void insertNode(string str, int pos){

Node *node = new Node;

node->str = str;

node->next = NULL;

Node *temp = head;

if(head==NULL){

head=node;

}

else{

if(pos==-1){

while(temp->next!=NULL){

temp = temp->next;

}

temp->next = node;

}

else{

if(pos==0){

node->next = temp;

head = node;

}

else{

int i=0;

while(i<pos-1){

temp = temp->next;

}

node->next = temp->next;

temp->next = node;

}

}

}

}

void deleteNode(int pos){

Node *temp = head;

Node *prev = NULL;

if(pos==0){

head = head->next;

}

else{

int i = 0;

while(i<pos){

prev = temp;

temp = temp->next;

i++;

}

prev->next=temp->next;

//cout<<"come"<<endl;

//cout<<head->str<<endl;

//cout<<head->next->str<<endl<<endl;

//head = head->next;

}

//head = temp;

}

void replaceNode(int pos, string str){

Node *temp = head;

while(pos){

temp = temp->next;

pos--;

}

temp->str = str;

}

void printList(){

Node *temp = head;

int i=1;

while(temp!=NULL){

cout<<i<<"::"<<temp->str<<endl;

temp=temp->next;

i++;

}

}

vector<string> split(string str){

vector<string> vec;

string s = "";

int state = 0;

for(int i; i<str.length(); i++){

switch(state){

case 0:

if(str[i]==' '){

vec.push_back(s);

s = "";

}

else if(str[i]=='"'){

state = 1;

}

else{

s+=str[i];

}

break;

case 1:

if(str[i]=='"'){

state = 0;

vec.push_back(s);

s = "";

}

else{

s+=str[i];

}

}

//cout<<s<<endl;

}

if(s.length()>0){

vec.push_back(s);

}

return vec;

}

int main() {

head=NULL;

string inp;

while(1){

//cin>>inp;

getline(cin, inp);

if(inp=="quit"){

break;

}

//cout<<inp<<endl;

vector<string> vec = split(inp);

//cout<<vec.size()<<endl;

if(vec[0]=="insertEnd"){

insertNode(vec[1], -1);

}

else if(vec[0]=="print"){

printList();

}

else if(vec[0]=="delete"){

deleteNode(stoi(vec[1]));

}

}

//insertNode("test", -1);

////cout<<"come2"<<endl;

//printList();

//cout<<endl;

//insertNode("test1", -1);

//printList();

//cout<<endl;

//insertNode("text3", 1);

//printList();

//cout<<endl;

//insertNode("text4", 0);

//printList();

//cout<<endl;

//deleteNode(0);

//printList();

//cout<<endl;

//deleteNode(2);

//printList();

//cout<<endl;

//replaceNode(0, "this is one text");

//printList();

//cout<<endl;

//// head->str = "text";

//// head->next = NULL;

return 0;

}