C++ Circular Linked Lists and Nodes Program takes user\'s input. Let\'s say \"Co
ID: 3746451 • Letter: C
Question
C++ Circular Linked Lists and Nodes
Program takes user's input. Let's say "Computer Science" in this case. Then the program removes every other letter from the user's input and puts it in a removal list. It keeps repeating the loop of removing every other letter from the user's input until there are no more letters left. Once that is done, it prints the removal list.
More details:
Write a program to create and fill a circular singly linked list with computer science (you can ask the user to enter a sentence e.g I Love C++)) Computer science Round 1: start from head C and select every other letter remove it and put it in a removal list (add at the end function) coptrsine mueAcec round 2:cotsn mue*cec prie round 3:cos mue cec prie trn round 4: co mueAcec prie tn s round 5: muencec prie tn s co Count the number of rounds needed to empty the circular list into removal single list Print the contents of the removal list Advanced option: Try your program with different sentences and record the time For example Computer science I love programming in c++ 12 sec 10 secExplanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
class node
{
public:
char c;
node *next;//single link
//constructor.
node(char cc)
{
c =cc;
next=NULL;
}
};
//class for circular single linked list
class CircularSingle
{
public:
node *head,*tail;//main list
node *removal_list,*rtail;//removal list
int count;
//constructor.
CircularSingle()
{
head=NULL;
removal_list=NULL;
count=0;
}
//method to insert at end
node* insert(char c,node *head)
{
node *n = new node(c);
//cout<<c<<endl;
if(head==NULL){
head=n;
head->next=head;//circular link
tail = n;
}
else
{
tail->next = n;
tail=n;
}
count++;
// cout<<head->c<<endl;
return head;
}
//method to insert into removal list
node* insert_r(char c,node *head)
{
node *n = new node(c);
//cout<<c<<endl;
// cout<<c<<endl;
if(head==NULL){
head=n;
head->next=head;//circular link
rtail = n;
//cout<<c<<endl;
}
else
{
rtail->next = n;
rtail=n;
// cout<<c<<endl;
}
// cout<<head->c<<endl;
return head;
}
//method to delete
void DeleteAll()//removes all contents from third letter and then every other letter...and insert it to removal list
{
if(head!=NULL)
{
node *temp =head->next;
removal_list=insert_r(head->c,removal_list);
int i=1;
while(temp!=head)
{
//inserting to removal lis
i++;
if(i%2==0)
removal_list=insert_r(temp->c,removal_list);
temp=temp->next;
if(i==count)break;
}
}
}
//method to display main list
void Display_List()
{
node *temp=head;
cout<<"List contents:";
// cout<<temp->c<<endl;
while(temp!=tail)
{
cout<<temp->c;
temp=temp->next;
}
cout<<temp->c<<endl;
cout<<endl;
}
void Display_removallist()
{
node *temp=removal_list;
cout<<" Removal List contents:";
while(temp->next!=removal_list)
{
cout<<temp->c;
temp=temp->next;
}
cout<<temp->c<<endl;
cout<<endl;
}
};
//testing method
int main()
{
string s;
cout<<"Enter line:";
//readingline
getline(cin,s);
cout<<s<<endl;
int i=0;
//creating circular linked list object
CircularSingle *c= new CircularSingle();
while(s[i]!='')
{
c->head=c->insert(s[i],c->head);//inserting to list
i++;
}
//displaying contents
c->Display_List();
//deleting from list and inserting to removal list
c->DeleteAll();
//displaying removal list contents
c->Display_removallist();
return 0;
}
output:
Enter line:computer science
computer science
List contents:computer science
Removal List contents:coptrsine
Process exited with return value 3221225477
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.