0% found this document useful (0 votes)
21 views3 pages

Insert Element in Circular Linked List

The document describes an experiment to insert elements into a singly circular linked list. It includes code for a circular linked list class with methods to add nodes to an empty list, add a node at the beginning, add a node at the end, and add a node after a specified element. The main function tests these methods by creating a list and calling each addition method before outputting the final list.

Uploaded by

Siddarth Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Insert Element in Circular Linked List

The document describes an experiment to insert elements into a singly circular linked list. It includes code for a circular linked list class with methods to add nodes to an empty list, add a node at the beginning, add a node at the end, and add a node after a specified element. The main function tests these methods by creating a list and calling each addition method before outputting the final list.

Uploaded by

Siddarth Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EXPERIMENT-5

AIM - Wap to insert an element in singly circular linked list.

Code –

#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
class circular
{
public:
struct Node *addToEmpty(struct Node *last, int data)
{
if (last != NULL)
return last;
struct Node *temp =
(struct Node*)malloc(sizeof(struct Node));
temp -> data = data;
last = temp;
last -> next = last;
return last;
}
struct Node *addBegin(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
struct Node *temp =
(struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = last -> next;
last -> next = temp;
return last;
}
struct Node *addEnd(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
struct Node *temp =
(struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = last -> next;
last -> next = temp;
last = temp;
return last;
}
struct Node *addAfter(struct Node *last, int data, int item)
{
if (last == NULL)
return NULL;
struct Node *temp, *p;
p = last -> next;
do
{
if (p ->data == item)
{
temp = (struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = p -> next;
p -> next = temp;
if (p == last)
last = temp;
return last;
}
p = p -> next;
} while(p != last -> next);
cout << item << " not present in the list." << endl;
return last;
}
void traverse(struct Node *last)
{
struct Node *p;
if (last == NULL)
{
cout << "List is empty." << endl;
return;
}
p=last->next;
cout<<"element is:";
do
{
cout <<p -> data << " ";
p = p -> next;
}
while(p != last->next);
}
};
int main()
{
circular a;
struct Node *last = NULL;
last = [Link](last, 6);
last = [Link](last, 4);
last = [Link](last, 2);
last = [Link](last, 8);
last = [Link](last, 12);
[Link](last);
return 0;
}

OUTPUT-:

You might also like