Source:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}linklist;
// Create a one-way circular list
linklist *creatlist()
{
linklist *head, *p, *rear;
int x;
head = (linklist *)malloc(sizeof(linklist));
head->next = NULL;
rear = head;
printf ( "Please enter the list content (integer), ending in 0: \ n");
scanf("%d", &x);
while (x)
{
p = (linklist *)malloc(sizeof(linklist));
p->data = x;
rear->next = p;
rear = p;
scanf("%d", &x);
}
rear-> next = head; // pointer to the head node of the tail
return rear;
}
// delete the unidirectional circular list specified node
void delete_key(linklist *rear, int key)
{
linklist *p, *q;
int deleted = 0; // used to mark whether to delete too
p = rear->next;
q = p->next;
while (q != rear->next)
{
if (q->data == key && q != rear)
{
p->next = q->next;
free(q);
q = p->next;
deleted = 1;
}
else if (q->data == key && q == rear)
{
p->next = q->next;
free(q);
q = p->next;
deleted = 1;
}
p = q;
q = q->next;
/ * IF (Q-> Key Data == Q == && REAR) // if deleted is the last node
{
p->next=q->next;
free(q);
q=p->next;
deleted=1;
}
*/
}
if (deleted)
printf ( "deleted \ n!");
else
printf ( "I do not find the node \ n!");
}
// output round robin list
void print(linklist *rear)
{
linklist *p;
printf ( "\ n current list follows: \ n");
p = rear->next->next;
while (p != rear->next)
{
printf("%5d", p->data);
p = p->next;
}
printf("\n");
}
int main ()
{
linklist *rear;
creatlist rear = ();
print(rear);
// delete the value specified in the circular list
int k;
printf ( "value specified in the circular list delete");
scanf("%d", &k);
delete_key(rear, k);
print(rear);
system("pause");
return 1;
}
operation result: