#include <iostream>
using namespace std;
struct node {
int num;
struct node * next;
}*head;
void build(int n)//function to build nodes of circular linked list
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
head = (struct node *)malloc(sizeof(struct node));
cout<<"Enter data of the list:\n";
cin>>num;
head->num = num;
head->next = NULL;
preptr = head;
for(i=2; i<=n; i++) { newnode = (struct node *)malloc(sizeof(struct node)); cin>>num;
newnode->num = num;
newnode->next = NULL;
preptr->next = newnode;
preptr = newnode;
}
preptr->next = head; //linking last node with head node
}
}
void display()//function to display list
{
struct node *tmp;
int n = 1;
if(head == NULL)
{
cout<<"List is empty";
}
else
{
tmp = head;
cout<<"\nCircular linked list data:\n";
do {
cout<num<<" "; tmp = tmp->next;
n++;
}while(tmp != head);
}
}
void deleteBegin()//function to delete beginning node from the circular linked list
{
struct node *p,*temp;
p=head;
while(p->next!=head)
{
p=p->next;
}
temp=head;
head=head->next;
p->next=head;
free (temp);
}
int main()//main function
{
int n,pos;
head = NULL;
cout<<"Enter the size of circular linked list: ";
cin>>n;
build(n);
display();
cout<<"\nAfter deleting node from beginning, new list is:";
deleteBegin();
display();
return 0;
}
Output:
Enter the size of circular linked list: 6
Enter data of the list:
11
12
13
14
15
16
Circular linked list data:
11 12 13 14 15 16
After deleting node from beginning, new list is:
Circular linked list data:
12 13 14 15 16
Login/Signup to comment