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: 3750014 • 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

/*

* C++ Program to Implement Singly Linked List

*/

#include<iostream>

#include<cstdio>

#include<cstdlib>

using namespace std;

/*

* Node Declaration

*/

struct node

{

string info;

struct node *next;

}*start;

/*

* Class Declaration

*/

class single_llist

{

public:

/*

* Creating Node

*/

node *create_node(string value)

{

struct node *temp, *s;

temp = new(struct node);

if (temp == NULL)

{

cout<<"Memory not allocated "<<endl;

return 0;

}

else

{

temp->info = value;

temp->next = NULL;

return temp;

}

}

/*

* Inserting Node at last

*/

void insert_last()

{

string value;

cout<<"Enter the value to be inserted: ";

cin>>value;

cout<<value<<endl;

struct node *temp, *s;

temp = create_node(value);

s = start;

if(s==NULL)

{

s=temp;

}

else

{

while (s->next != NULL)

{

s = s->next;   

}

s->next = temp;

}

temp->next = NULL;

cout<<"Element Inserted at last"<<endl;  

}

/*

* Insertion of node at a given position

*/

void insert_pos()

{

int pos, counter = 0;

string value;

cout<<"Enter the value to be inserted: ";

cin>>value;

struct node *temp, *s, *ptr;

temp = create_node(value);

cout<<"Enter the postion at which node to be inserted: ";

cin>>pos;

int i;

s = start;

while (s != NULL)

{

s = s->next;

counter++;

}

if (pos == 1)

{

if (start == NULL)

{

start = temp;

start->next = NULL;

}

else

{

ptr = start;

start = temp;

start->next = ptr;

}

}

else if (pos > 1 && pos <= counter)

{

s = start;

for (i = 1; i < pos; i++)

{

ptr = s;

s = s->next;

}

ptr->next = temp;

temp->next = s;

}

else

{

cout<<"Positon out of range"<<endl;

}

}

/*

* Delete element at a given position

*/

void delete_pos()

{

int pos, i, counter = 0;

if (start == NULL)

{

cout<<"List is empty"<<endl;

return;

}

cout<<"Enter the position of value to be deleted: ";

cin>>pos;

struct node *s, *ptr;

s = start;

if (pos == 1)

{

start = s->next;

}

else

{

while (s != NULL)

{

s = s->next;

counter++;  

}

if (pos > 0 && pos <= counter)

{

s = start;

for (i = 1;i < pos;i++)

{

ptr = s;

s = s->next;

}

ptr->next = s->next;

}

else

{

cout<<"Position out of range"<<endl;

}

free(s);

cout<<"Element Deleted"<<endl;

}

}

/*

* Update a given Node

*/

void update()

{

int pos, i;

string value;

if (start == NULL)

{

cout<<"List is empty"<<endl;

return;

}

cout<<"Enter the node postion to be updated: ";

cin>>pos;

cout<<"Enter the new value: ";

cin>>value;

struct node *s, *ptr;

s = start;

if (pos == 1)

{

start->info = value;

}

else

{

for (i = 0;i < pos - 1;i++)

{

if (s == NULL)

{

cout<<"There are less than "<<pos<<" elements";

return;

}

s = s->next;

}

s->info = value;  

}

cout<<"Updated"<<endl;

}

/*

* Searching an element

*/

void search()

{

string value;

if (start == NULL)

{

cout<<"List is empty"<<endl;

return;

}

cout<<"Enter text to be searched: ";

cin>>value;

struct node *s;

s = start;

int pos=0;

while (s != NULL)

{

pos++;

bool flag=false;

for(int i=0; i<s->info.length() && i<value.length(); i++)

{

if(s->info.at(i)==value.at(i))

flag=true;

}

if (flag==true)

{

cout<<"Element "<<value<<" is found at position "<<pos<<endl;

return;

}

s = s->next;

}

cout<<value<<" not found in the list"<<endl;  

}

/*

* Display Elements of a link list

*/

void display()

{

struct node *temp;

if (start == NULL)

{

cout<<"The List is Empty"<<endl;

return;

}

int count=1;

temp = start;

cout<<"Elements of list are: "<<endl;

while (temp != NULL)

{

cout<<count<<" "<< temp->info<<endl;

count++;

temp = temp->next;

}

}

single_llist()

{

start = NULL;

}

};

/*

* Main :contains menu

*/

main()

{

int choice, nodes, element, position, i;

single_llist sl;

start = NULL;

while (1)

{

cout<<endl<<"---------------------------------"<<endl;

cout<<endl<<"Operations on singly linked list"<<endl;

cout<<endl<<"---------------------------------"<<endl;

cout<<"1.Insert node at last"<<endl;

cout<<"2.Insert node at position"<<endl;

cout<<"3.Delete a Particular Node"<<endl;

cout<<"4.Update Node Value"<<endl;

cout<<"5.Search Element"<<endl;

cout<<"6.Display Linked List"<<endl;

cout<<"7.Exit "<<endl;

cout<<"Enter your choice : ";

cin>>choice;

switch(choice)

{

case 1:

cout<<"Inserting Node at Last: "<<endl;

sl.insert_last();

cout<<endl;

break;

case 2:

cout<<"Inserting Node at a given position:"<<endl;

sl.insert_pos();

cout<<endl;

break;

case 3:

cout<<"Delete a particular node: "<<endl;

sl.delete_pos();

break;

case 4:

cout<<"Update Node Value:"<<endl;  

sl.update();

cout<<endl;

break;

case 5:

cout<<"Search element in Link List: "<<endl;

sl.search();

cout<<endl;

break;

case 6:

cout<<"Display elements of link list"<<endl;

sl.display();

cout<<endl;

break;

case 7:

cout<<"Exiting..."<<endl;

exit(1);

break;  

default:

cout<<"Wrong choice"<<endl;

}

}

}