In this article Circular singly linked list we give the information about operation on a Circular singly linked list such as deletion in circular linked list, insert in circular linked list etc.
Inserting an element in the list
- Insert at the beginning
n->next=start;
start=n;
last->next=start;
- Insert at the end
Last->next=n;
n->next=start;
last=n;
- Insert at given position
This operation is same in linear singly linked list.
Program: Following program shows the operations insert at beginning, at the end and in between in circular singly linked list and displays the same.
In this example, ‘start’ node stores the address of first node and ‘last’ node stores the address of last node
// Program for circular singly linked list
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
NODE *start=NULL, *last;
void main()
{
int ch;
void addatbeg();
void addatend();
void addinbet();
void display();
clrscr();
while(1)
{
clrscr();
printf(“\n 1. Insert at beginnnig”);
printf(“\n 2. Insert at end”);
printf(“\n 3. Insert in between”);
printf(“\n 4. Display”);
printf(“\n 5. Exit”);
printf(“\n Enter your choice: ”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: addatbeg();
break;
case 2: addatend();
break;
case 3: addinbet();
break;
case 4: display();
break;
case 5: exit(0);
default: printf(“\n Please enter correct choice…”);
break;
}
}
getch();
}
// Insert at beginning
void addatbeg()
{
int data;
NODE *n;
printf(“\n Enter data to store: ”);
scanf(“%d”,&data);
n=(NODE*)malloc(sizeof(NODE));
n->info=data;
if(start==NULL)
{
start=last=n;
n->next=start;
}
else
{
n->next=start;
start=n;
last->next=start;
}
getch();
}
// Insert at end
void addatend()
{
int data;
NODE *n, *temp;
printf(“\n Enter data to store: ”);
scanf(“%d”,&data);
n=(NODE*)malloc(sizeof(NODE));
n->info=data;
n->next=start;
last->next=n;
last=n;
getch();
}
// Insert in between
void addinbet()
{
int i,pos,data;
NODE *n, *temp;
printf(“\n Enter position to insert: ”);
scanf(“%d”,&pos);
temp=start;
for(i=1;i<pos-1;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf(“\n There are less than %d elements… ”,pos);
}
}
printf(“\n Enter data to store: ”);
scanf(“%d”, &data);
n=(NODE*)malloc(sizeof(NODE));
n->next=temp->next;
n->info=data;
temp->next=n;
getch();
}
// Display linked list
void display()
{
NODE *temp;
if(start==NULL)
{
printf(“\n Linked list is empty…”);
}
printf(“\n Linked list is: ”);
temp=start->next;
printf(“%d->”,start->info);
while(temp!=start)
{
printf(“%d->”,temp->info);
temp=temp->next;
}
getch();
}
Deleting an element
Deletion of an element can be done at three positions, at beginning, last and at given position.
Delete at Beginning
temp=start;
start=temp->next;
last->next=start;
free(temp);
Delete the last element
temp1=temp->next;
temp->next=start;
free(temp1);
last=temp;
Deleting element from given position
temp1=temp->next;
temp->next=temp->next;
free(temp1);
Program: Following program shows the operations delete at beginning, at end and in between in circular singly linked list and displays the same.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
NODE *start=NULL, *last;
void main()
{
int ch;
void delatbeg();
void delatend();
void delinbet();
void create();
void display();
clrscr();
printf(“\n Create circular linked list: ”);
create();
while(1)
{
clrscr();
display();
printf(“\n 1. Delete at beginning”);
printf(“\n 2. Delete at end”);
printf(“\n 3. Delete in between”);
printf(“\n 4. Display”);
printf(“\n 5. Exit”);
printf(“\n Enter your choice: ”);
scanf(“%d”, &ch);
switch(ch)
{
case 1: delatbeg();
break;
case 2: delatend();
break;
case 3: delinbet();
break;
case 4: display();
break;
case 5: exit(0);
default: printf(“\n Please enter correct choice…”);
break;
}
}
getch();
}
// Create linked list
void create()
{
char ch=’Y’;
int data;
NODE *n;
while(ch==’y’ || ch==’Y’)
{
n=(NODE*)malloc(sizeof(NODE));
printf(“\n Enter data to store: ”);
scanf(“%d”, &data);
n->info=data;
if(start==NULL)
{
start=last=n;
n->next=start;
}
else
{
n->next=start;
last->next=n;
last=n;
}
fflush(stdin);
printf(“\n Do you want to continue ? (Y / N): ”);
ch=getchar();
}
getch();
}
// Delete at beginning
void delatbeg()
{
NODE *temp;
if(start==NULL)
return;
else
if(start->next==start)
{
temp=start;
start=NULL;
free(temp);
}
else
{
temp=start;
start=temp->next;
last->next=start();
free(temp);
}
getch();
}
// Delete at end
void delatend()
{
NODE *temp, *temp1;
if(start==NULL)
return;
else
if(start->next==start)
{
temp=start;
start=NULL;
free(temp);
}
else
{
temp=start;
while(temp->next->next!=start)
temp=temp->next;
temp1=temp->next;
last=temp;
temp->next=start;
free(temp1);
}
getch();
}
// Delete in between
void delinbet()
{
int i,pos;
NODE *temp, *temp1;
printf(“\n Enter position of delete: ”);
scanf(“%d”,&pos);
temp=start;
for(i=1;i<pos-1;i++)
{
temp=temp->next;
if(temp->next==start)
{
printf(“\n There are less than %d elements…”,pos);
}
}
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
getch();
}
// Display linked list
void display()
{
NODE *temp;
if(start==NULL)
{
printf(“\n Linked list is empty…”);
}
printf(“\n Linked list is: ”);
temp=start;
while(temp!=last)
{
printf(“%d->”,temp->info);
temp=temp->next;
}
printf(“%d \n \n”,temp->info);
getch();
}
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining