Write a program to create and fill a circular singly linked list with computer s
ID: 3749077 • Letter: W
Question
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
mue^cec
round 2:cotsn
mue^cec prie
round 3:cos
mue^cec prie tn
round 4: co
mue^cec prie tn s
round 5: mue^cec 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
Explanation / Answer
PROGRAM:
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
// Defines a structure for Node
struct Node
{
// To store node data
char nodeData;
// Point to next node
struct Node *next;
}*head;// End of structure
// Defines a class for circular linked list
class circularLL
{
public:
// Prototype of member function
circularLL();
void create(char value);
void display();
};// End of class
// Default constructor definition
circularLL::circularLL()
{
head = NULL;
}// End of default constructor
// Function to create a circular linked list
void circularLL::create(char data)
{
// Dynamically allocates memory to a new node
struct Node *newNode = new(struct Node);
// Assigns data to new node
newNode->nodeData = data;
// Checks if head is null then first node to create
if (head == NULL)
{
// Head pointing to new node
head = newNode;
// New node next is pointing to head
newNode->next = head;
}// End of if condition
// Otherwise already nodes available
else
{
// New node next points to head next
newNode->next = head->next;
// Head next points to new node
head->next = newNode;
// Head pointing to new node
head = newNode;
}// End of else
}// End of function
// Function to display the circular linked list
void circularLL::display()
{
// Declares a temporary node
struct Node *temp;
// Checks if head is null then empty list
if (head == NULL)
{
cout<<"List is empty, nothing to display"<<endl;
return;
}// End of if condition
// Temporary node points to head next
temp = head->next;
cout<<"Circular Link List: "<<endl;
// Loops till temporary node not equals to head
while (temp != head)
{
// Displays the current node data
cout<<temp->nodeData<<" -> ";
// Move next
temp = temp->next;
}// End of while loop
cout<<temp->nodeData<<endl;
}// End of function
// main function definition
int main()
{
// Creates an object of class circularLL using default constructor
circularLL cl;
// Creates a string with data
string data;
cout<<" Enter a sentence: ";
getline(cin, data);
// Calculates the length of the string
int len = data.length();
// Loops till end of the string
for(int c = 0; c < len; c++)
// Calls the function to create the add the data to list
cl.create(data[c]);
// Calls the function to display the data
cl.display();
}// End of main function
Sample Output:
Enter a sentence: This is demo
Circular Link List:
T -> h -> i -> s -> -> i -> s -> -> d -> e -> m -> o
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.