Linked List Help! How would I implement a Linked list with this prompt? The C++
ID: 673114 • Letter: L
Question
Linked List Help! How would I implement a Linked list with this prompt?
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++ 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.
• If L is entered, then list all lines of text.
• If H is entered, then display the instruction menu.
• If Q is entered, save the text into the original input file and quit the program.
Explanation / Answer
Answer:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct List_Node
{
char *textLine;
struct List_Node *nextEle;
}*startList;
class listTextEditor
{
public:
List_Node* create_node(char *);
void insertLineFromFile(char *);
void insertLineList(int linPos);
void deleteLineList(int linPos);
void displayElement();
listTextEditor()
{
startList = NULL;
}
};
List_Node *listTextEditor::create_node(char *value)
{
struct List_Node *tempNode, *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, 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;
}
main()
{
char choice;
int linNo;
char *text;
listTextEditor sl;
startList = NULL;
int linPos;
ifstream inRFile;
inRFile.open("Sample.txt");
while(inRFile>>linPos>>text)
{
sl.insertLineFromFile(text);
}
sl.displyElement();
while (1)
{
cout<<endl<<"SIMPLE TEXT EDITORUSINGSIGLY LINKED LIST"<<endl;
cout<<"I insert line at given position"<<endl;
cout<<"D delete node"<<endl;
cout<<"L List all lines of text"<<endl;
cout<<"H Display instruction menu"<<endl;
cout<<"Q Save thetext into original file "<<endl;
cout<<"ENTER YOUR CHOICE : ";
cin>>choice;
switch(choice)
{
case 'I':
cout<<"Enter the line Number where you want to insert:"<<endl;
cin>>linNo;
sl.insertLineList(linNo);
cout<<endl;
break;
case 'D':
cout<<"Enter the line Number where you want to insert:"<<endl;
cin>>linNo;
sl.deleteLineList(linNo);
break;
case 'L':
cout<<"Display elements of link list"<<endl;
sl.displayElement();
cout<<endl;
break;
case 'H':
cout<<"Simple textEditor:"<<endl;
COUT<<" 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"<<endl;
cout<<" If D is entered with a number n, then delete line n. If n is not a valid line number, then do nothing"<<endl;
cout<<" If L is entered, then list all lines of text"<<endl;
cout<<" If H is entered, then display the instruction menu"<<endl;
cout<<" If Q is entered, save the text into the original input file and quit the program"<<endl;
break;
case 'Q':
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.