xxxxxxxxxx
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
void insertData();
void printData();
};
node *head=0;
void node::insertData()
{
int d;
node *top;
cout<<"Enter Data : ";
cin>>d;
head = new node();
head->data=d;
head->next=0;
top=head;
int c=0;
cout<<"Do you want to continue If yes press 1 else press 2 : ";
cin>>c;
node *newnode;
while(c==1)
{
cout<<"Enter Data : ";
newnode=new node();
cin>>newnode->data;
newnode->next=0;
top->next=newnode;
top=top->next;
cout<<"Do you want to continue If yes press 1 else press 2 : ";
cin>>c;
}
newnode->next=head;
}
void node::printData()
{
node *top;
top=head;
while (top!=0)
{
cout<<top->data<<" ";
top=top->next;
}
}
int main()
{
node obj;
obj.insertData();
obj.printData();
}
xxxxxxxxxx
#include<iostream>
// Node class for creating the nodes of the circular linked list
class Node {
public:
int data;
Node* next;
};
// Function to insert a new node at the beginning of the circular linked list
void insertAtBeginning(Node** head_ref, int data) {
// Create a new node
Node* new_node = new Node();
new_node->data = data;
// If the circular linked list is empty, make the new node as the head
if (*head_ref == NULL) {
new_node->next = new_node;
*head_ref = new_node;
}
else {
// Traverse to the last node of the circular linked list
Node* temp = *head_ref;
while (temp->next != *head_ref)
temp = temp->next;
// Point the next of the new node to the head of the circular linked list
new_node->next = *head_ref;
// Change the next of the last node to the new node
temp->next = new_node;
// Make the new node as the head of the circular linked list
*head_ref = new_node;
}
}
// Function to display the circular linked list
void display(Node* head) {
if (head == NULL)
return;
Node* temp = head;
do {
// Print the data of the current node
std::cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
}
int main() {
Node* head = NULL;
// Inserting nodes into the circular linked list
insertAtBeginning(&head, 4);
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);
// Displaying the circular linked list
std::cout << "Circular Linked List: ";
display(head);
return 0;
}
xxxxxxxxxx
#include<iostream>
using namespace std;
class node{
private:
int data;
node *next;
public:
void insertData(int);
void printData(int);
};
node *head=0;
void node::insertData(int size)
{
int d;
cout<<"Enter data : ";
cin>>d;
head=new node();
head->data=d;
head->next=NULL;
node *temp;
temp=head;
node *newnode;
for(int i=1; i<size; i++)
{
newnode= new node();
cin>>newnode->data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
newnode->next=head;
}
}
void node::printData(int size)
{
node *temp;
temp=head;
for(int i=0; i<size; i++)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
node obj;
int size;
cout<<"Enter size for create a node : ";
cin>>size;
obj.insertData(size);
obj.printData(size);
}