C++ Coding Practice Assignment, not sure how to do this, this opens up the next
ID: 3747018 • Letter: C
Question
C++ Coding Practice Assignment, not sure how to do this, this opens up the next section of my learning, could someone please help!!
Please leave notes within the project so I can follow through and understand where I went wrong
Implement a line editor with a linked list. Include your code that implements a linked list. Include your code for the line editor. Edit the main method to invoke your line editor implementation. Use std cin statements to read in the test cases. 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 Sample Input 2: Sample Input 3: insertEnd "now is the time" insertEnd "for all good men" insertEnd "to come to the aid of their country" print edit 2 "for all good people print quit insertEnd "now is the time insertEnd for all good people" insertEnd "to come to the aid of their country" delete 2 print insert 2 "for all good people" print quit Sample Output 2: Sample Output 3: 1 now is the time 2 for all good men 3 to come to the aid of their country 1 now is the time 2 for all good people 3 to come to the aid of their country 1 now is the time 2 to come to the aid of their country 1 now is the time 2 for all good people 3 to come to the aid of their countryExplanation / Answer
/*
* C++ Program to Implement Linked List Through String
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
/*
* Node Declaration
*/
struct node
{
string info;
struct node *next;
}*start;
/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node(string);
void insert_begin();
void delete_pos();
void search();
void update();
void display();
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 Strings"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Delete a Particular Node"<<endl;
cout<<"3.Update Node Value"<<endl;
cout<<"4.Search Element"<<endl;
cout<<"5.Display Linked List"<<endl;
cout<<"6.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Delete a particular node: "<<endl;
sl.delete_pos();
break;
case 3:
cout<<"Update Node Value:"<<endl;
sl.update();
cout<<endl;
break;
case 4:
cout<<"Search element in Link List: "<<endl;
sl.search();
cout<<endl;
break;
case 5:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 6:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
/*
* Creating Node
*/
node *single_llist::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 element in beginning
*/
void single_llist::insert_begin()
{
string value;
cout<<"Enter the value to be inserted: ";
getline(cin, value);
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
/*
* Delete element at a given position
*/
void single_llist::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 single_llist::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: ";
getline(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<<"Node Updated"<<endl;
}
/*
* Searching an element
*/
void single_llist::search()
{
int pos = 0;
string value;
bool flag = false;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the value to be searched: ";
getline(cin, value);
struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->info == value)
{
flag = true;
cout<<"Element "<<value<<" is found at position "<<pos<<endl;
}
s = s->next;
}
if (!flag)
cout<<"Element "<<value<<" not found in the list"<<endl;
}
/*
* Display Elements of a link list
*/
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<" ";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.