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

C++ - Circular Singly Linked Use template if possible. Please read carefully. I

ID: 3746339 • Letter: C

Question

C++ - Circular Singly Linked

Use template if possible.

Please read carefully. I will give you a thumbs up immediately when you answer the solution correctly.

EXAMPLE:

The user enters "Computer Science", the program fills it in a circular singly linked list. Here's what the program does next...

The program will start on the first letter, C, and take every other letter and remove it from the user's input and then add it to the removal list.

==============================

User enters the word: Computer Science

Start on C and remove every other letter. In this case, we remove Co[m]p[u]t[e]r[ ]S[c]i[e]n[c]e

Removal List: mue cec

==============================

New word: coptrsine (The program removed "mue cec" from Computer Science, so now we get coptrsine)

The program then repeats itself. Start on C and remove every other letter. In this case, we remove Co[p]t[r]s[i]n[e].

New Removal List: mue cec prie

==============================

New word: cotsn (The program removed "prie" from "coptrsine" so we got "cotsn")

The program then repeats itself. Start on C and remove every other letter. In this case, we remove co[t]s[n].

New Removal List: mue cec prie tn

==============================

Repeat this until there are no letters left in user's word. Count how many times the program repeats the loop, use int count or whatever you like.

Once finished, print everything in the removal list.

Use template if possible.

Explanation / Answer

#include<iostream>

#include<cstdio>

#include<cstdlib>

using namespace std;

struct nodes

{

int data;

struct nodes *nxt;

}*lastt;

class circular_LInked_List

{

public:

void creation(int value);

void add_front(int value);

void add_Lastt(int value, int position);

void del(int value);

void find_element(int value);

void displayy();

void update();

void sort();

circular_LInked_List()

{

lastt = NULL;

}

};

/*

* Main :contains menu

*/

int main()

{

int choice, element, position;

circular_LInked_List cl;

while (1)

{

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

cout<<endl<<"Circular singly linked list"<<endl;

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

cout<<"1.Create Node"<<endl;

cout<<"2.Add at beginning"<<endl;

cout<<"3.Add after"<<endl;

cout<<"4.Delete"<<endl;

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

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

cout<<"7.Update"<<endl;

cout<<"8.Sort"<<endl;

cout<<"9.Quit"<<endl;

cout<<"Enter your choice : ";

cin>>choice;

switch(choice)

{

case 1:

cout<<"Enter the element: ";

cin>>element;

cl.creation(element);

cout<<endl;

break;

case 2:

cout<<"Enter the element: ";

cin>>element;

cl.add_front(element);

cout<<endl;

break;

case 3:

cout<<"Enter the element: ";

cin>>element;

cout<<"Insert element after position: ";

cin>>position;

cl.add_Lastt(element, position);

cout<<endl;

break;

case 4:

if (lastt == NULL)

{

cout<<"List is empty, nothing to delete"<<endl;

break;

}

cout<<"Enter the element for deletion: ";

cin>>element;

cl.del(element);

cout<<endl;

break;

case 5:

if (lastt == NULL)

{

cout<<"List Empty!! Can't search"<<endl;

break;

}

cout<<"Enter the element to be searched: ";

cin>>element;

cl.find_element(element);

cout<<endl;

break;

case 6:

cl.displayy();

break;

case 7:

cl.update();

break;

case 8:

cl.sort();

break;   

case 9:

exit(1);

break;

default:

cout<<"Wrong choice"<<endl;

}

}

return 0;

}

void circular_LInked_List::creation(int val)

{

struct nodes *tmp;

tmp = new(struct nodes);

tmp->data = val;

if (lastt == NULL)

{

lastt = tmp;

tmp->nxt = lastt;

}

else

{

tmp->nxt = lastt->nxt;

lastt->nxt = tmp;

lastt = tmp;

}

}

void circular_LInked_List::add_front(int val)

{

if (lastt == NULL)

{

cout<<"First Create the list."<<endl;

return;

}

struct nodes *tmp;

tmp = new(struct nodes);

tmp->data = val;

tmp->nxt = lastt->nxt;

lastt->nxt = tmp;

}

void circular_LInked_List::add_Lastt(int val, int position)

{

if (lastt == NULL)

{

cout<<"First Create the list."<<endl;

return;

}

struct nodes *tmp, *s;

s = lastt->nxt;

for (int i = 0;i < position-1;i++)

{

s = s->nxt;

if (s == lastt->nxt)

{

cout<<"There are less than ";

cout<<position<<" in the list"<<endl;

return;

}

}

tmp = new(struct nodes);

tmp->nxt = s->nxt;

tmp->data = val;

s->nxt = tmp;

if (s == lastt)

{

lastt=tmp;

}

}

void circular_LInked_List::del(int val)

{

struct nodes *tmp, *s;

s = lastt->nxt;

if (lastt->nxt == lastt && lastt->data == val)  

{

tmp = lastt;

lastt = NULL;

free(tmp);

return;

}

if (s->data == val) /*First Element Deletion*/

{

tmp = s;

lastt->nxt = s->nxt;

free(tmp);

return;

}

while (s->nxt != lastt)

{

/*Deletion of Element in between*/

if (s->nxt->data == val)   

{

tmp = s->nxt;

s->nxt = tmp->nxt;

free(tmp);

cout<<"Element "<<val;

cout<<" deleted from the list"<<endl;

return;

}

s = s->nxt;

}

if (s->nxt->data == val)   

{

tmp = s->nxt;

s->nxt = lastt->nxt;

free(tmp);

lastt = s;

return;

}

cout<<"Element "<<val<<" not found in the list"<<endl;

}

void circular_LInked_List::find_element(int val)

{

struct nodes *s;

int counter = 0;

s = lastt->nxt;

while (s != lastt)

{

counter++;

if (s->data == val)   

{

cout<<"Element "<<val;

cout<<" found at position "<<counter<<endl;

return;

}

s = s->nxt;

}

if (s->data == val)   

{

counter++;

cout<<"Element "<<val;

cout<<" found at position "<<counter<<endl;

return;

}

cout<<"Element "<<val<<" not found in the list"<<endl;

}

void circular_LInked_List::displayy()

{

struct nodes *s;

if (lastt == NULL)

{

cout<<"List is empty, nothing to display"<<endl;

return;

}

s = lastt->nxt;

cout<<"Circular Link List: "<<endl;

while (s != lastt)

{

cout<<s->data<<"->";

s = s->nxt;

}

cout<<s->data<<endl;

}

void circular_LInked_List::update()

{

int val, position, i;

if (lastt == NULL)

{

cout<<"List is empty, nothing to update"<<endl;

return;

}

cout<<"Enter the nodes position to be updated: ";

cin>>position;

cout<<"Enter the new val: ";

cin>>val;

struct nodes *s;

s = lastt->nxt;

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

{

if (s == lastt)

{

cout<<"There are less than "<<position<<" elements.";

cout<<endl;

return;

}

s = s->nxt;

}

s->data = val;  

cout<<"Node Updated"<<endl;

}

void circular_LInked_List::sort()

{

struct nodes *s, *ptr;

int tmp;

if (lastt == NULL)

{

cout<<"List is empty, nothing to sort"<<endl;

return;

}

s = lastt->nxt;

while (s != lastt)

{

ptr = s->nxt;

while (ptr != lastt->nxt)

{

if (ptr != lastt->nxt)

{

if (s->data > ptr->data)

{

tmp = s->data;

s->data = ptr->data;

ptr->data = tmp;

}

}

else

{

break;

}

ptr = ptr->nxt;   

}

s = s->nxt;

}

}