Objectives 1 Understand the design implementation usage and

Objectives 1. Understand the design, implementation, usage and limitation of a Linked List ADT 2. Gain experience implementing abstract data t types using already developed data structures Recognize and practice possible linked list applications. 3. Overview In this project, you should write a simple line editor. Keep the entire text on a linked list, one line in a separate node. Start the program with entering EDIT file, after which a prompt appears along with the line number. If the letter I is entered with a number n following it, then insert the text to be followed before line n. If I is not followed by a number, then insert the text before the current line. If D is entered with two numbers n and m, one n, or no number following it, then delete lines n through m, line n, or the current line. Do the same with the command L, which stands for listing lines. If A is entered, then append the text to the existing lines. Entry E signifies exit and saving the text in a file. Here is an Example: EDIT testfile 1> The first line 2> 3> And another line 4>I 3 3 The second line 4> One more line 5> L 1> The first line 3 The second line 4> One more line 5 And another line 5> D 2 4> L // This is now line 5, not 3; // line 5, since L was issued from line 5 //line 4, since one line was deleted; 1 The first line 2> The second line 3> One more line 4> And another line 4> E // this and the following lines // now have new numbers

Solution

Solution: See the code below:

----------------------------------------------------------

/**
* This program demostrates a line editor
*/
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

//node of storing a line in linked list
struct LINENODE {
   int line_no;
   string line_text;
   LINENODE *previous_line;
   LINENODE *next_line;
};

//pointers for first line and current line
LINENODE *first_line = NULL, *current_line = NULL, *last_line = NULL;

//insert a new line in list after current line or as a first line, if list is empty.
void insert_line(int line_no, string line) {
   //new line
   LINENODE *LINE = new LINENODE;
   LINE->line_no = line_no;
   LINE->line_text = line;
   LINE->previous_line = NULL;
   LINE->next_line = NULL;

   //linked list empty
   if (first_line == NULL) {
       //insert as first line
       first_line = LINE;
       current_line = LINE;
       last_line = NULL;
   } else {
       //insert after current line
       LINE->previous_line = current_line;
       LINE->next_line = current_line->next_line;
       if (current_line->next_line != NULL)
           current_line->next_line->previous_line = LINE;
       current_line->next_line = LINE;
       current_line = LINE; //updates current line
       last_line = current_line; //updates last line
   }
}

//inserts a line before a given line
void insert_line(int before_line_no, int line_no, string line) {
   //new line
   LINENODE *LINE = new LINENODE;
   LINE->line_no = line_no;
   LINE->line_text = line;
   LINE->previous_line = NULL;
   LINE->next_line = NULL;

   //locate line and insert
   LINENODE *curr = first_line;
   while (curr != NULL) {
       //if line found, break
       if (curr->line_no == before_line_no)
           break;
       curr = curr->next_line;
   }
   //now insert
   LINE->next_line = curr;
   LINE->previous_line = curr->previous_line;
   if (curr->previous_line != NULL)
       curr->previous_line->next_line = LINE;
   curr->previous_line = LINE;
   current_line = LINE; //updates current line
}

//append a line
void append_line(int line_no, string line) {
   //new line
   LINENODE *LINE = new LINENODE;
   LINE->line_no = line_no;
   LINE->line_text = line;
   LINE->previous_line = NULL;
   LINE->next_line = NULL;

   //insert after last line
   LINE->previous_line = last_line;
   last_line->next_line = LINE;
   last_line = LINE; //updates last line
   current_line = last_line; //updates current line
}

//list lines of text
void list_lines() {
   LINENODE *curr = first_line;
   while (curr != NULL) {
       cout << curr->line_no << \">\" << curr->line_text << endl;
       curr = curr->next_line;
   }
}

int main(int argc, char* argv[]) {
   //check if sufficient numbers of arguments are there.
   if (argc < 2) {
       cerr << \"Name of file not specified!\" << endl;
       cout << \"Usage: EDIT <filename>\" << endl;
       return -1;
   }

   string filename = argv[argc - 1]; //output filename
   string filepath = \"./\";

   //output file stream
   ofstream outfile((filepath + filename).c_str());

   //check if file can be created or opened
   if (!outfile) {
       cerr << \"Error creating or reading file! Exiting...\" << endl;
       return -1;
   }

   int line_no = 1; //line number
   string line; //line to be read
   //loop to read lines and do operations
   //int line_counter=0;
   while (1) {

       cout << line_no << \">\";
       getline(cin, line);
       if (line.length() == 0)
           line = \"\";
       //cout<<\"len:\"<<line.length()<<endl;
       char flag; //flag to be used for an operation
       if (line != \"\")
           flag = line.at(0);
       else
           flag = \'Y\'; //Y for empty
       //cout<<\"char:\"<<flag<<endl;
       switch (flag) {
       case \'I\':
           int before_line_no;
           before_line_no = line.at(2) - \'0\';
           cout << \"insert\" << before_line_no << endl;
           fflush(stdin);
           getline(cin, line);
           if (line.length() == 0)
               line = \"\";
           insert_line(before_line_no, line_no, line);
           break;
       case \'A\':
           fflush(stdin);
           getline(cin, line);
           if (line.length() == 0)
               line = \"\";
           append_line(line_no, line);
           break;
       case \'D\':
           cout << \"delete\" << endl;
           break;
       case \'L\':
           list_lines();
           break;
       case \'E\':
           exit(1);
       case \'Y\':
           insert_line(line_no, line);
           break;
       default:
           insert_line(line_no, line);
           break;
       }
       line_no++;
   }
   return 0;
}

----------------------------------------------------

 Objectives 1. Understand the design, implementation, usage and limitation of a Linked List ADT 2. Gain experience implementing abstract data t types using alre
 Objectives 1. Understand the design, implementation, usage and limitation of a Linked List ADT 2. Gain experience implementing abstract data t types using alre
 Objectives 1. Understand the design, implementation, usage and limitation of a Linked List ADT 2. Gain experience implementing abstract data t types using alre
 Objectives 1. Understand the design, implementation, usage and limitation of a Linked List ADT 2. Gain experience implementing abstract data t types using alre

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site