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

I need help writing this program. We have to use linked list and I dont know how

ID: 673628 • Letter: I

Question

I need help writing this program. We have to use linked list and I dont know how to implement them. Could you atleast write the code that makes the list from a file and is able to insert delete and print it out

Problem Statement The C++ program that you will write is a simple line editor. Keep the entire text on a linked list structure, one line in a separate node. Execute the compiled C+t program with one command line argument, which represents the file name of an input text file. After the program starts, a prompt appears along with the line number, followed by the contents of each line of the input file If the letter I is entered with a number n following it, display a prompt along with the line number n, after user types a line of text, then insert the text at line n, and the original text at line n becomes line n+1. If n is larger than the total number of the current lines displayed from the editor, then you need insert a few lines of empty text in order to insert the user input text at line n. If D is entered with a number n, then delete line n. If n is not a valid line number . then do nothing. IfL is entered, then list all lines of text. If H is entered, then display the instruction menu. IfQ is entered, save the text into the original input file and quit the program You can assume that the user always input an integer number following the letter options of either I or D for this programming assignment, and you will handle input validation and provide more features of your editor in the programming assignment 3 Your program should check for file open errors. You can terminate the program when the first open error occurs and display "the file cannot be opened message". Do not use any directory names in your program. Your C+ program must compile and execute from your current directory. The executable file (named myEditor) should be generated after you compile your program $ g++ Now you can run your program from the command line by typing $ ./myeditor Then you will see the following sample output of running your program without command line argument: Missing command line argument! Usage: ./myEditor -o myEditor *.cpp

Explanation / Answer

CODE :

Header file

#include<iostream>

using namespace std;

struct List_Node
{
char *textLine;
struct List_Node *nextEle;

friend class listTextEditor;
}*startList;

class listTextEditor

{

public:

List_Node* create_node(char *);
void insertLineFromFile(char *);
void insertLineList(int linPos);
void deleteLineList(int linPos);
void displayElement();
void printInstruct();
listTextEditor()

{

       startList = NULL;

}

};

And source code

#include <cstdlib>

   #include <iostream>

#include <string>

#include "Textlist.h"

#include <fstream>

using namespace std;

List_Node *listTextEditor::create_node(char *value)
{

struct List_Node *tempNode;
struct List_Node *s;
tempNode = new(struct List_Node);

if (tempNode == NULL)
{
       cout<<"Memory not allocated "<<endl;
       return 0;
}

else
{
       tempNode->textLine = value;
       tempNode->nextEle = NULL;
       return tempNode;
}
}

void listTextEditor::insertLineFromFile(char *line )
{
struct List_Node *tempNode, *s;
tempNode = new(struct List_Node);
tempNode->textLine=line;
tempNode->nextEle=NULL;

if (startList == NULL)
{
       startList = tempNode;
       startList->nextEle = NULL;
}

else
{
       s = startList;
       while (s->nextEle != NULL)
       {
           s = s->nextEle;
       }
       tempNode->nextEle = NULL;
       s->nextEle = tempNode;
}

}

void listTextEditor::insertLineList(int linPos)
{

int value;
int cntLin = 0;

char line[200];

cout<<"Enter the line to be inserted: ";
cin>>line;

struct List_Node *tempNode, *s, *ptrNode;
tempNode = create_node(line);
int k1;
s = startList;

while (s != NULL)
{
s = s->nextEle;
cntLin++;
}

if (linPos == 1)
{
if (startList == NULL)
{
           startList = tempNode;
           startList->nextEle = NULL;
}

else
{
           ptrNode = startList;
           startList = tempNode;
           startList->nextEle = ptrNode;
}

}

else if (linPos > 1 && linPos <= cntLin)
{
s = startList;
for (k1 = 1; k1 < linPos; k1++)
{
ptrNode = s;
s = s->nextEle;
}

ptrNode->nextEle = tempNode;
tempNode->nextEle = s;
}

else
{
cout<<"Positon out of range"<<endl;
}

}

void listTextEditor::deleteLineList(int linPos)
{
int i, cntLin = 0;
if (startList == NULL)
{
cout<<"List is empty"<<endl;
return;
}

struct List_Node *s, *ptrNode;
s = startList;
if (linPos == 1)
{
startList = s->nextEle;
}

else
{
while (s != NULL)
{
           s = s->nextEle;
           cntLin++;
}

if (linPos > 0 && linPos <= cntLin)
{
           s = startList;
           for (i = 1;i < linPos;i++)
           {
               ptrNode = s;
               s = s->nextEle;
           }
           ptrNode->nextEle = s->nextEle;
}

else
{
           cout<<"Position out of range"<<endl;
}

free(s);
cout<<"Element Deleted"<<endl;
}
}

void listTextEditor::displayElement()
{
int linNo=1;
struct List_Node *tempNode;
if (startList == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}

tempNode = startList;
cout<<"Elements of list are: "<<endl;

while (tempNode != NULL)
{
cout<<linNo<<" "<<tempNode->textLine;
tempNode = tempNode->nextEle;
linNo++;
}

cout<<"NULL"<<endl;
}

void listTextEditor::printInstruct()
{
cout << "==============================================================" << endl;
cout << "Welcome to my text editor." << endl;
cout << " " << "To insert text at the end of the file, type a line and " << endl;
cout << "press enter." << endl;
cout << " " << "To insert text at a certain line number, type 'I'" << endl;
cout << "followed by a space and the desired line number." << endl;
cout << " " << "To delete a line, type 'D' followed by a space and the" << endl;
cout << "line number." << endl;
cout << " " << "To print all the lines, type 'L' and press enter." << endl;
cout << " " << "To exit, type 'E' and press enter." << endl;
cout << " " << "To display this introduction, type 'H' and press enter enter" << endl;
cout << "=============================================================="<< endl;
}

int main(int argc, char* argv[])
{
char choice;
int linNo;
listTextEditor command;
startList = NULL;
int linPos;
fstream inRFile;
char *text;

inRFile.open("CS216PA2.txt");

while(inRFile>>linPos>>text)
{
command.insertLineFromFile(text);
}

command.printInstruct();
command.displayElement();

do
{
cin>>choice;
cin.ignore(256, ' ');
switch(choice)
{
case 'I':
cout<<"Enter the line Number where you want to insert:"<<endl;
cin>>linNo;
command.insertLineList(linNo);      
cout<<endl;
break;

case 'D':
cout<<"Enter the line Number where you want to delete:"<<endl;
cin>>linNo;
command.deleteLineList(linNo);
break;

case 'L':
cout<<"Display elements of link list"<<endl;
command.displayElement();
cout<<endl;
break;

case 'H':
command.printInstruct();
break;

case 'Q':
break;

case 'E':
break;
  
default:
           cout << "Invalid choice! Please try again!. " << endl;
           break;
}
} while (choice != 'E');

cout << "Thank you for using the program!" << endl;
system("pause");
return 0;
}