The program also includes the Josephus Problem
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int d;
struct node *link;
}Node;
Node *head=NULL;
void append()
{
Node *p, *q;
p = (struct node *)malloc(sizeof(struct node));
p->link = NULL;
printf("\nEnter a value to add at the end:");
scanf("%d", &p->d);
if(head==NULL)
{
head=p;
p->link=head;
return;
}
q = head;
while(q->link!=head)
{
q = q->link;
}
q->link = p;
p->link = head;
}
void add_beg()
{
struct node *p, *r=head;
p = (struct node *)malloc(sizeof(struct node));
printf("\nEnter a value to add at the beginning: ");
scanf("%d", &p->d);
while(r->link!=head)
{
r = r->link;
}
if(head==NULL)
{
head = p;
p->link = head;
return;
}
p->link=head;
head=p;
r->link=head;
}
void del_beg()
{
Node *q, *r=head;
if(head==NULL)
{
printf("\nThere is no node to delete.");
return;
}
while(r->link!=head)
{
r = r->link;
}
printf("\nDeleted Node is: %d", head->d);
q = head;
head = head->link;
r->link=head;
free(q);
}
void josephus()
{
Node *q = head->link, *r=head;
int i=2;
while(count()!=1)
{
if(i%2==0)
{
r->link = q->link;
if(q==head)
head = head->link;
i=1;
}
else
r = r->link;
q = q->link;
}
}
void del_end()
{
Node *q, *r;
if(head==NULL)
{
printf("\nCannot delete. Linked List is empty.");
return;
}
if(head->link==head)
{
printf("\nDeleted value: %d", head->d);
q = head;
head=NULL;
free(q);
return;
}
r = head;
q = head->link;
while(q->link!=head)
{
r = q;
q = q->link;
}
printf("\nDeleted value: %d", q->d);
r->link=head;
free(q);
}
int count()
{
Node *q=head;
int c=0;
if(head==NULL)
return c;
while(q->link!=head)
{
c=c+1;
q=q->link;
}
return c+1;
}
void display()
{
Node *q;
int i, c=count();
q = head;
printf("\nThe values in the linked list are: \n");
for(i=1;i<=c;i++)
{
printf("%d\n", q->d);
q = q->link;
}
}
void add_any_pos()
{
Node *q, *p;
int pos, i;
printf("\nEnter the position where you want to insert the node: ");
scanf("%d", &pos);
if(pos<1||pos>count()+1)
{
printf("\nInvalid position.");
return;
}
if(pos==1)
{
add_beg();
return;
}
if(pos==count()+1)
{
append();
return;
}
p = (Node *)malloc(sizeof(Node));
printf("Enter a value: ");
scanf("%d", &p->d);
q = head;
for(i=2;i<pos;i++)
{
q = q->link;
}
p->link=q->link;
q->link=p;
}
void del_any_pos()
{
Node *q, *r;
int pos, i;
if(head==NULL)
{
printf("\nThere is no node to delete.");
return;
}
printf("\nEnter the position of the node you want to delete: ");
scanf("%d", &pos);
if(pos<1||pos>count())
{
printf("\nInvalid position.");
return;
}
if(pos==1)
{
del_beg();
return;
}
if(pos==count())
{
del_end();
return;
}
r = head;
q = head->link;
for(i=2;i<pos;i++)
{
r = q;
q = q->link;
}
printf("\nDeleted Node: %d", q->d);
r->link=q->link;
free(q);
}
void searchNdel()
{
Node *q, *r;
int pos=0, val, i=0, c=count();
if(head==NULL)
{
printf("\nThere is no node to delete.");
return;
}
printf("\nEnter the value you want to delete: ");
scanf("%d", &val);
q=head;
for(i=1;i<=c;i++)
{
if(q->d==val)
{
pos=i;
break;
}
q = q->link;
}
if(pos<1)
{
printf("\nInvalid Value.");
return;
}
if(pos==1)
{
del_beg();
return;
}
if(pos==count())
{
del_end();
return;
}
r = head;
q = head->link;
for(i=2;i<pos;i++)
{
r = q;
q = q->link;
}
printf("\nDeleted Node: %d", q->d);
r->link=q->link;
free(q);
}
void main()
{
int ch;
clrscr();
do
{
printf("\nMENU");
printf("\n1. Add a node at the end of the Linked List.");
printf("\n2. Add a node at the start of the Linked List.");
printf("\n3. Delete a node at the end of the Linked List.");
printf("\n4. Delete a node at the start of the Linked List.");
printf("\n5. Display the values in the linked list.");
printf("\n6. Enter a node at any position.");
printf("\n7. Delete a node from any position of the linked list.");
printf("\n8. Search a value in the linklist and delete it.");
printf("\n9. Josephus");
printf("\n0. Terminate the program.");
printf("\nEnter your choice:");
ch = getche()-48;
switch(ch)
{
case 1:
append();
break;
case 2:
add_beg();
break;
case 3:
del_end();
break;
case 4:
del_beg();
break;
case 5:
display();
break;
case 0:
printf("\nThank you for using the program.");
break;
case 6:
add_any_pos();
break;
case 7:
del_any_pos();
break;
case 8:
searchNdel();
break;
case 9:
josephus();
break;
default:
printf("\nInvalid Input Enter Again.");
}//End of Switch-Case
printf("\nPress any key to continue.");
getch();
}while(ch!=0);
getch();
}