#include <bits/stdc++.h>
struct
Node {
int
data;
struct
Node* next;
};
int
Length(
struct
Node* head)
{
struct
Node* current = head;
int
count = 0;
if
(head == NULL) {
return
0;
}
else
{
do
{
current = current->next;
count++;
}
while
(current != head);
}
return
count;
}
void
Display(
struct
Node* head)
{
struct
Node* current = head;
if
(head == NULL) {
printf
(
"\nDisplay List is empty\n"
);
return
;
}
else
{
do
{
printf
(
"%d "
, current->data);
current = current->next;
}
while
(current != head);
}
}
void
Insert(
struct
Node** head,
int
data)
{
struct
Node* current = *head;
struct
Node* newNode =
new
Node;
if
(!newNode) {
printf
(
"\nMemory Error\n"
);
return
;
}
newNode->data = data;
if
(*head == NULL) {
newNode->next = newNode;
*head = newNode;
return
;
}
else
{
while
(current->next != *head) {
current = current->next;
}
newNode->next = *head;
current->next = newNode;
}
}
void
deleteNode(
struct
Node* head_ref,
struct
Node* del)
{
struct
Node* temp = head_ref;
if
(head_ref == del) {
head_ref = del->next;
}
while
(temp->next != del) {
temp = temp->next;
}
temp->next = del->next;
free
(del);
return
;
}
void
DeleteFirst(
struct
Node** head)
{
struct
Node *previous = *head, *next = *head;
if
(*head == NULL) {
printf
(
"\nList is empty\n"
);
return
;
}
if
(previous->next == previous) {
*head = NULL;
return
;
}
while
(previous->next != *head) {
previous = previous->next;
next = previous->next;
}
previous->next = next->next;
*head = previous->next;
free
(next);
return
;
}
void
DeleteAllOddNode(
struct
Node** head)
{
int
len = Length(*head);
int
count = 0;
struct
Node *previous = *head, *next = *head;
if
(*head == NULL) {
printf
(
"\nDelete Last List is empty\n"
);
return
;
}
if
(len == 1) {
DeleteFirst(head);
return
;
}
while
(len > 0) {
if
(count == 0) {
DeleteFirst(head);
}
if
(count % 2 == 0 && count != 0) {
deleteNode(*head, previous);
}
previous = previous->next;
next = previous->next;
len--;
count++;
}
return
;
}
void
DeleteAllEvenNode(
struct
Node** head)
{
int
len = Length(*head);
int
count = 1;
struct
Node *previous = *head, *next = *head;
if
(*head == NULL) {
printf
(
"\nList is empty\n"
);
return
;
}
if
(len < 2) {
return
;
}
previous = *head;
next = previous->next;
while
(len > 0) {
if
(count % 2 == 0) {
previous->next = next->next;
free
(next);
previous = next->next;
next = previous->next;
}
len--;
count++;
}
return
;
}
int
main()
{
struct
Node* head = NULL;
Insert(&head, 99);
Insert(&head, 11);
Insert(&head, 22);
Insert(&head, 33);
Insert(&head, 44);
Insert(&head, 55);
Insert(&head, 66);
printf
(
"Initial List: "
);
Display(head);
printf
(
"\nAfter deleting Odd position nodes: "
);
DeleteAllOddNode(&head);
Display(head);
printf
(
"\n\nInitial List: "
);
Display(head);
printf
(
"\nAfter deleting even position nodes: "
);
DeleteAllEvenNode(&head);
Display(head);
return
0;
}