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

File Type : cpp Add the following to your linked list of strings program: 1. all

ID: 3920971 • Letter: F

Question

File Type: cpp

Add the following to your linked list of strings program:

1. allow the user to search for an item (indicate the result of the search)

2. allow the user to insert an item (print the linked list after insertion)

3. allow the user to delete an item (print the linked list after deletion)

Here's the program I need to have remodified for this assignment as shown below.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
   struct nodeType
   {
       string info;
       nodeType *link;
   };
   nodeType *first, *newNode, *last;
   string x; //string input variable
   cout << "Enter a string of some kind. Enter 'Done' when finished." << endl;
   getline(cin, x);
   first = NULL;
   last = NULL;
  


   while (x != "Done")
   {
       newNode = new nodeType;
       newNode->info = x;
       newNode->link = NULL;

       if (first == NULL)
       {
           first = newNode;
           last = newNode;
       }
       else
       {
           last->link = newNode;
           last = newNode;
       }
       cout << newNode->info << " ";
       cout << "Enter a string of some kind. Enter 'Done' when finished." << endl;
       getline(cin, x);
   }
  
      
      
  
   return 0;


}

Explanation / Answer


In the main function, you need to enter the following:

main()

{

int ch, nodes, ele, posi;
sin_llist sl;
start = NULL;
while(1)

{

cout<<"1.Search for an item"<<endl;
cout<<"2.Inserting an item"<<endl;
cout<<"3.Deleting an item"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Searching an item:"<<endl;
sl.search();
cout<<endl;
break;
case 2:
cout<<"Inserting an item:"<<endl;
sl.ins_begin();
cout<<endl;
break;
case 3:
cout<<"Deleting an item:"<<endl;
sl.del_pos();
cout<<endl;
break;
case 4:
cout<<"Exiting"<<endl;
exit(1);
break;
}
}

}

/*
* To search an element
*/
void sin_llist::search()
{
int posi = 0;
string val;
bool flag = false;
if (start == NULL)
{
cout<<"Empty List"<<endl;
return;
}
cout<<"Enter the character to be searched: ";
cin>>val;
struct node *z;
z = start;
while (z != NULL)
{
posi++;
if (z->info == val)
{
flag = true;
cout<<"Element "<<val<<" is found at position "<<pos<<endl;
}
z = z->next;
}
if (!flag)
cout<<"Element "<<val<<" is not found in the list"<<endl;
}

/*
* To insert an element in beginning
*/
void s_llist::ins_begin()
{
string y;
cout<<"Enter the character to be inserted: ";
cin>>y;
struct node *tem, *k;
temp = create_node(y);
if (start == NULL)
{
start = tem;
start->next = NULL;
}
else
{
k = start;
start = tem;
start->next = k;
}
cout<<"Element Inserted at beginning"<<endl;
}

/*
* To delete an element at a given position
*/
void sin_llist::del_pos()
{
int posi, i, count = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of character to be deleted: ";
cin>>posi;
struct node *z, *ptr;
z = start;
if (posi == 1)
{
start = z->next;
}
else
{
while (z != NULL)
{
z = z->next;
count++;
}
if (posi > 0 && posi <= count)
{
z = start;
for (i = 1;i < pos;i++)
{
ptr = z;
z = z->next;
}
ptr->next = z->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element is Deleted"<<endl;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote